Kubernetes : Kubectl Commands

  • Kubectl can be pronounced as “cube CTL”, “kube control”, “cube cuttle”
  • It is a is a robust CLI that runs commands against the Kubernetes cluster and controls the cluster manager
  • kubectl is known as the swiss army knife of container orchestration and management
  • kubectl is designed to make this process more comfortable and straightforward
  • kubectl allows users to create, inspect, update, and delete Kubernetes objects
  • Every Kubernetes command has an API endpoint, and kubectl’s primary purpose is to carry out HTTP requests to the API.
  • kubectl cluster-info : Display endpoint information about the master and services in the cluster
  • kubectl version : Display the Kubernetes version running on the client and server
  • kubectl config view : Get the configuration of the cluster
  • kubectl api-resource : List the API resources that are available
  • kubectl api-versions : List the API versions that are available
  • kubectl get all –all -namespaces : List everything
  • kubectl get namespaces : Generate a plain-text list of all namespaces:
  • kubectl get pods : Generate a plain-text list of all pods
  • kubectl get pods -o wide : Generate a detailed plain-text list of all pods
  • kubectl get pods–field-selector=spec. nodeName=[server-name] : Generate a list of all pods running on a particular node server
  • kubectl get replicationcontroller [replication-controller-name] : List a specific replication controller in plain text
  • kubectl get replicationcontroller, services : Generate a plain-text list of all replication controllers and services
  • kubectl get daemonset : List one or more daemonsets
  • kubectl edit daemonset <daemonset_name> : Edit and update the definition of one or more daemonset
  • kubectl delete daemonset <daemonset_name> : Delete a daemonset
  • kubectl create daemonset <daemonset_name> : Create a new daemonset
  • kubectl rollout daemonset : Manage the rollout of a daemonset
  • kubectl describe ds <daemonset_name> -n <namespace_name> : Display the detailed state of daemonsets within a namespace
  • kubectl get deployment : List one or more deployments
  • kubectl describe deployment <deployment_name> : Display the detailed state of one or more deployments
  • kubectl edit deployment <deployment_name> : Edit and update the definition of one or more deployment on the server
  • kubectl create deployment <deployment_name> : Create one a new deployment
  • kubectl delete deployment <deployment_name> : Delete deployments
  • kubectl rollout status deployment <deployment_name> : See the rollout status of a deployment
  • kubectl get events : List recent events for all resources in the system
  • kubectl get events –field-selector type=Warning : List Warnings only
  • kubectl get events –field-selector involvedObject.kind!=Pod : List events but exclude Pod events
  • kubectl get events –field-selector involvedObject.kind=Node, involvedObject.name=<node_name> : Pull events for a single node with a specific name
  • kubectl get events –field-selector type!=Normal : Filter out normal events from a list of events
  • kubectl logs <pod_name> : Print the logs for a pod
  • kubectl logs –since=1h <pod_name> : Print the logs for the last hour for a pod
  • kubectl logs –tail=20 <pod_name> : Get the most recent 20 lines of logs
  • kubectl logs -f <service_name> [-c <$container>] : Get logs from a service and optionally select which container
  • kubectl logs -f <pod_name> : Print the logs for a pod and follow new logs
  • kubectl logs -c <container_name> <pod_name> : Print the logs for a container in a pod
  • kubectl logs <pod_name> pod.log : Output the logs for a pod into a file named ‘pod.log’
  • kubectl logs –previous <pod_name> : View the logs for a previously failed pod
  • kubectl create namespace <namespace_name> : Create namespace <name>
  • kubectl get namespace <namespace_name> : List one or more namespaces
  • kubectl describe namespace <namespace_name> : Display the detailed state of one or more namespace
  • kubectl delete namespace <namespace_name> : Delete a namespace
  • kubectl edit namespace <namespace_name> : Edit and update the definition of a namespace
  • kubectl top namespace <namespace_name> : Display Resource (CPU/Memory/Storage) usage for a namespace
  • kubectl taint node <node_name> : Update the taints on one or more nodes
  • kubectl get node : List one or more nodes
  • kubectl delete node <node_name> : Delete a node or multiple nodes
  • kubectl top node : Display Resource usage (CPU/Memory/Storage) for nodes
  • kubectl describe nodes | grep Allocated -A 5 : Resource allocation per node
  • kubectl get pods -o wide | grep <node_name> : Pods running on a node
  • kubectl annotate node <node_name> : Annotate a node
  • kubectl cordon node <node_name> : Mark a node as unschedulable
  • kubectl uncordon node <node_name> : Mark node as schedulable
  • kubectl drain node <node_name> : Drain a node in preparation for maintenance
  • kubectl label node : Add or update the labels of one or more nodes
  • kubectl get pod : List one or more pods
  • kubectl delete pod <pod_name> : Delete a pod
  • kubectl create pod <pod_name> : Create a pod
  • kubectl exec <pod_name> -c <container_name> <command> : Execute a command against a container in a pod
  • kubectl exec -it <pod_name> /bin/sh : Get interactive shell on a a single-container pod
  • kubectl top pod : Display Resource usage (CPU/Memory/Storage) for pods
  • kubectl describe pod <pod_name> : Display the detailed state of a pods
  • kubectl annotate pod <pod_name> <annotation> : Add or update the annotations of a pod
  • kubectl label pod <pod_name> : Add or update the label of a pod
  • kubectl get rc : List the replication controllers
  • kubectl get rc –namespace=”<namespace_name>” : List the replication controllers by namespace
  • kubectl get replicasets : List ReplicaSets
  • kubectl describe replicasets <replicaset_name> : Display the detailed state of one or more ReplicaSets
  • kubectl scale –replicas=[x] : Scale a ReplicaSet
  • kubectl create secret : Create a secret
  • kubectl get secrets : List secrets
  • kubectl describe secrets : List details about secrets
  • kubectl delete secret <secret_name> : Delete a secret
  • kubectl get services : List one or more services
  • kubectl describe services : Display the detailed state of a service
  • kubectl expose deployment [deployment_name] : Expose a replication controller, service, deployment or pod as a new Kubernetes service
  • kubectl edit services : Edit and update the definition of one or more services
  • kubectl get serviceaccounts : List service accounts
  • kubectl describe serviceaccounts : Display the detailed state of one or more service accounts
  • kubectl replace serviceaccount : Replace a service account
  • kubectl delete serviceaccount <service_account_name> : Delete a service account
  • kubectl get pod |grep -P ‘\s+([1–9]+)\/\1\s+’ : List Pods in Ready Status
  • kubectl get pod |grep -Pv ‘\s+([1–9]+)\/\1\s+’ : List Pods which are not Ready/Pending status
  • kubectl get pods -n namespacesort - -sort by=.metadata.creationTimestamp : List Pods by Deployment timestamp (add -A for live listing)
  • kubectl get deployment -n namespace -o=jsonpath=”{range .items[*]}{‘\n’}{.metadata.name}{‘:\t’}{range .spec.template.spec.containers[*]}{.image}{‘, ‘}{end}{end}” : List deployment Artifact/ Images in current namespace.
  • kubectl get deployment <deployment_name> -o=jsonpath=’{$.spec.template.spec.containers[:1].image}’ : Get Image version of current deployment.
  • kubectl get pod <mutlti_conatiner_pod> -o go-template=’{{range .status.containerStatuses}}{{printf “%s:\n%s\n\n” .name .lastState.terminated.message}}{{end}}’ : List last terminated Status
  • kubectl logs <pod_name> <conatiner_name> -n namespace - -previous / kubectl logs - -previous ${POD_NAME} ${CONTAINER_NAME} : List logs of previous state
  • kubectl logs - -selector app=<app_name> - -container <conatiner_name> : Multi Pod logs
  • kubectl -n logs -f deployment/ - -all-containers=true - -since=10m : list logs for last 10 mis
  • kubectl rollout history deployment/app : Inspect the history of your Deployment
  • kubectl rollout undo deployment/app - -to-revision=2 : Rollback to a specific version
  • kubectl rollout undo deployment/<deployment_name> : Rollback deployment to last version.
  • kubectl rollout restart -n namespace deployment/<deployment_name>: Restart deployment.
  • kubectl describe pod | grep ‘Name:\| Limits\| Requests\| cpu:\| memory’ : List Memory and CPU limit of pod, replace pod with deployment to list deployment wise resource limits.
  • kubectl delete pod - -selector=project.app=<app_name> -n namespace : Delete multiple pods with single command for a deployment.
  • kubectl delete pod <pod_name> -n namespace - -grace-period=0 - -force : Force delete the pod
  • Kubectl top pod : List down Pod resources CPU and Memory
  • kubectl top pod <pod_name> - -containers : List Container wise resource usage
  • Kubectl top node : List Node resource usage.
  • kubectl exec -it <pod_name> sh : Shell into the pod
  • kubectl exec -it <pod_name> - -/bin/bash : bash into the pod
  • kubectl edit ingress <ingress_name> -n namespace : edit ingress file
  • kubectl create secret generic <secret_name> - -from-literal=Key=value - -from-literal=Key=User: create a secret file at cluster level and keep adding - -from=literal for all Key values. example kubectl create secret generic <secret_name> - -from-literal=DB_name=ExampleDB - -from-literal=DB_password=Ahdgakah

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store