Day 11: Kubernetes Commands

For Cluster Management

CommandDescription
kubectl cluster-infokubectlGet cluster information.
kubectl get nodesViews all the nodes present in the cluster.

For Node Management

CommandDescription
kubectl get nodesList all nodes in the cluster.
kubectl describe node <node-name>Describe a specific node.
kubectl drain <node-name>Drain a node for maintenance.
kubectl uncordon <node-name>Uncordon a node after maintenance.
kubectl label node <node_name> <key>=<value>You can label the node by using key-value pair.
kubectl label node <node_name> <label_key>-You can remove the label which is already attached to the node.

For Namespace Management

CommandDescription
kubectl describe namespace <namespace-name>Describe a namespace.
kubectl create namespace <namespace-name>Create a namespace.
kubectl get namespacesList all namespaces.
kubectl config set-context –current –namespace=<namespace-name>Switch to a different namespace.
kubectl delete namespace <namespace-name>Delete a namespace.
kubectl edit namespace <namespace_name>Edit and update the namespace definition.

For Creating Resources

CommandDefinition
kubectl apply -f <resource-definition.yaml>Create or Update a resource from a YAML file.
kubectl createCreate an object imperatively.
kubectl apply -f url-to-resource-definition.yamlCreate a resource by using the URL.

For Viewing and Finding Resources

CommandDescription
kubectl get <resource-type>List all resources of a specific type.
kubectl get <resource-type> -o wideList all resources with additional details.
kubectl describe <resource-type> <resource-name>Describe a specific resource.
kubectl get <resource-type> -l <label-key>=<label-value>List all resources with a specific label.
kubectl get <resource-type> –all-namespacesList all resources in all namespaces.
kubectl get <resource-type> –sort-by=<field>List all resources sorted by a specific field.
kubectl get <resource-type> -l <label-selector>List resources with a specific label selector.
kubectl get <resource-type> –field-selector=<field-selector>List resources with a specific field selector.
kubectl get <resource-type> -n <namespace>List all resources in a specific namespace.

For Deleting Resources

CommandDescription
kubectl delete <resource-type> <resource-name>Delete a resource.
kubectl delete <resource-type1> <resource-name1> <resource-type2> <resource-name2>Delete multiple resources.
kubectl delete <resource-type> –allDelete all resources of a specific type.
kubectl delete -f <resource-definition.yaml>kubectl delete -f url-to-resource-definition.yamlDelete the resource by using url.
kubectl delete <resource-type> –all -n <namespace>Delete all resources in a specific namespace.

For Copying Files and Directories

CommandDescription
kubectl cp <local-path> <namespace>/<pod-name>:<container-path>Copy files and directories to a container.
kubectl cp <namespace>/<pod-name>:<container-path> <local-path>Copy files and directories from a container.
kubectl cp <namespace>/<pod-name>:<source-container-path> <destination-namespace>/<destination-pod-name>:<destination-container-path>Copying files from one container to another within the same pod.
kubectl cp <namespace>/<source-pod-name>:<source-container-path> <destination-namespace>/<destination-pod-name>:<destination-container-path>Copying files from one container to another in a different pod.

For Patching Resources

CommandDescription
kubectl patch <resource-type> <resource-name> -p ‘<patch-document>Patch a resource using a JSON or YAML document.
kubectl patch <resource-type> <resource-name> –patch-file=<patch-file>Patch a resource using a JSON or YAML file.

For Scaling Resources

CommandDescription
kubectl scale deployment <deployment-name> –replicas=<replica-count>Scale a deployment.
kubectl scale statefulset <statefulset-name> –replicas=<replica-count>Scale a statefulset.
kubectl scale replicaset <replicaset-name> –replicas=<replica-count>Scale a replica set.

For Pod Management

CommandDescription
kubectl create -f <pod-definition.yaml>Create a pod from a YAML file.
kubectl get podsList all pods in the cluster.
kubectl describe pod <pod-name>Describe a specific pod.
kubectl logs <pod-name>Get logs from a pod.
kubectl logs -f <pod-name>Stream logs from a pod.
kubectl logs -l <label-key>=<label-value>Get logs with a specific label.
kubectl exec -it <pod-name> — <command>Exec into a pod.
kubectl delete pod <pod-name>Delete a pod.
kubectl create pod <pod-name>Create a pod with the name.
kubectl get pod -n <namespace_name>List all pods in a namespace.

For Deployment Management

CommandDescription
kubectl create deployment <deployment-name> –image=<image-name>Create a deployment.
kubectl get deploymentsList all deployments.
kubectl describe deployment <deployment-name>Describe a specific deployment.
kubectl scale deployment <deployment-name> –replicas=<replica-count>Scale a deployment.
kubectl set image deployment/<deployment-name> <container-name>=<new-image-name>Update a deployment’s image.
kubectl rollout status deployment/<deployment-name>Rollout status of a deployment.
kubectl rollout pause deployment/<deployment-name>Pause a deployment rollout.
kubectl rollout resume deployment/<deployment-name>Resume a deployment rollout.
kubectl rollout undo deployment/<deployment-name>Rollback a deployment to the previous revision.
kubectl rollout undo deployment/<deployment-name> –to-revision=<revision-number>Rollback a deployment to a specific revision.
kubectl delete deployment <deployment-name>Delete deployment name.

For ReplicaSets Management

CommandDescription
kubectl create -f <replicaset-definition.yaml>Create a ReplicaSet.
kubectl get replicasetsList all ReplicaSets.
kubectl describe replicaset <replicaset-name>Describe a specific ReplicaSet.
kubectl scale replicaset <replicaset-name> –replicas=<replica-count>Scale a ReplicaSet.

For Service Management

CommandDescription
kubectl create service <service-type> <service-name> –tcp=<port>Create a service.
kubectl get servicesList all services.
kubectl expose deployment <deployment-name> –port=<port>Expose a deployment as a service.
kubectl describe service <service-name>Describe a specific service.
kubectl delete service <service-name>Delete a service.
kubectl get endpoints <service-name>Get information about a service.

For Config Maps and Secrets

CommandDescription
kubectl create configmap <config-map-name> –from-file=<path-to-file>Create a config map from a file.
kubectl create secret <secret-type> <secret-name> –from-literal=<key>=<value>Create a secret.
kubectl get configmapsList all config maps.
kubectl get secretsList all secrets.
kubectl describe configmap <config-map-name>Describe a specific config map.
kubectl describe secret <secret-name>Describe a specific secret.
kubectl delete secret <secret_name>Delete a specific secret.
kubectl delete configmap <config-map-name>Delete a specific config map.

For Networking

CommandDescription
kubectl port-forward <pod-name> <local-port>:<pod-port>Port forward to a pod.
kubectl expose deployment <deployment-name> –type=NodePort –port=<port>Expose a deployment as a NodePort service.
kubectl create ingress <ingress-name> –rule=<host>/<path>=<service-name> –<service-port>Create an Ingress resource.
kubectl describe ingress <ingress-name>Get information about an Ingress.
kubectl get ingress <ingress-name> -o jsonpath='{.spec.rules[0].host}’Retrieves the most value from the first rule of the specified Ingress resource.

For Storage

CommandDescription
kubectl create -f <persistent-volume-definition.yaml>Create a PersistentVolume.
kubectl get pvList all PersistentVolumes.
kubectl describe pv <pv-name>Describe a specific PersistentVolume.
kubectl create -f <persistent-volume-claim-definition.yaml>Create a PersistentVolumeClaim.
kubectl get pvcList all PersistentVolumeClaims.
kubectl describe pvc <pvc-name>Describe a specific PersistentVolumeClaim.

For StatefulSets

CommandDescription
kubectl create -f <statefulset-definition.yaml>Create a StatefulSet.
kubectl get statefulsetsList all StatefulSets.
kubectl describe statefulset <statefulset-name>Describe a specific StatefulSet.
kubectl scale statefulset <statefulset-name> –replicas=<replica-count>Scale a StatefulSet.

For Monitoring and Troubleshooting

CommandDescription
kubectl get eventsCheck cluster events.
kubectl get component statusesGet cluster component statuses.
kubectl top nodesGet resource utilization of nodes.
kubectl top podsGet resource utilization of pods.
kubectl debug <pod-name> -it –image=<debugging-image>Enable container shell access debugging.