Home / How To / Cheat sheet for Kubernetes commands

Cheat sheet for Kubernetes commands



When working with Kubernetes, it is very important that you know at least the basic commands. In this article, you will find the commands needed most of the time while working with the cluster. If you know commands, you can get rid of writing object definition files for simple objects and then create objects with these files. Instead, you can use the commands and see objects created in a fraction of seconds.

In this article, you will see not only the commands for getting details about the objects but also for creating the objects. This article focuses on commands only and not on a description of those commands. If you want to know each command in detail, you can visit the official documentation here.

This article will be a cheat sheet for you regarding the core objects of the Kubernetes Cluster.

Conditions

  1. Basic understanding of Kubernetes
  2. Kubernetes Cluster with at least 1 working node.
    Click here to learn how to create a Kubernetes cluster. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18.04 EC2 instances.

What will we see?

  1. Important commands
    1. cluster Information
    2. configuration information
    3. namespace
    4. During
    5. implementations
    6. services
    7. Manage items from .yaml / .yml files

Important commands

cluster Information

kubectl version #Print the client and server version information
kubectl api-resources #Print the supported API resources on the server
kubectl api-versions #Print the supported API versions on the server, in the form of "group/version"
kubectl cluster-info #Print the cluster information
kubectl  get nodes #Get the list of the nodes in the cluster
kubectl  get nodes master -o wide #Get information of the master node
kubectl  describe  nodes  master #Get detailed information of the master nodes

configuration information

kubectl  config view #Display merged kubeconfig settings
kubectl  config  current-context #View the current context
kubectl config  use-context [email protected] #Set the contextr, here [email protected] is the context name
kubectl  config get-clusters #Display clusters defined in the kubeconfig
kubectl  config get-contexts #Describe one or many contexts

namespaces

kubectl  get namespaces #Get all namespaces
kubectl  get namespaces -o yaml #Get namespace information in yaml format
kubectl  describe  namespace default #Describe the default namespace
kubectl  create namespace my-namespace #Create a new namespace
kubectl  delete namespace my-namespace #Delete the namespace

Pods

kubectl get pods #Get pods from the current namespace
kubectl get pods --all-namespaces #Get pods from all the namespaces
kubectl get pods -namespace=my-namespace #Get pods from the specified namespace
kubectl  run my-pod-1 --image=nginx:latest --dry-run #Create a pod 
kubectl  run my-pod-1 --image=nginx:latest --dry-run=client #see how the pod would be processed
kubectl  run my-pod-2 --image=nginx:latest --namespace=my-namespace #Create a pod in the specified namespace
kubectl  run nginx --image=nginx -l --labels=app=test #Create a pod with a label to it
kubectl get pods --show-labels #Get all pods with label output
kubectl  get pods -o wide #Get pods with exapanded/wide output
kubectl  get pods --sort-by=.metadata.name #List pods in a sorted order
kubectl  logs  my-pod-1 #Get logs of the pod
kubectl get pods my-pod-2 --namespace=my-namespace -o wide #Get pods within the specified namespace with exapanded/wide output
kubectl  logs  my-pod-2 --namespace=my-namespace #Get logs of the pod within the specified namespace
kubectl  describe  pod my-pod-1 #Describe the pod
kubectl describe  pods my-pod-1 --namespace=my-namespace #Describe the pod within the specified namespace
kubectl  delete pod my-pod-1 #Delete the pod from the current namespace
kubectl delete  pods my-pod-1 --namespace=my-namespace #Delete the pod from the specified namespace

implementations

kubectl  get deployments #Get a list of deployments from the current namespace
kubectl  get deployments --namespace=my-namespace #Get a list of deployments from the specified namespace
kubectl  create deployment my-deployment-1 --image=nginx #Create a deployment
kubectl  get deployment my-deployment-1 #Get the specified deployment
kubectl  get deployment my-deployment-1 --show-labels #Get the specified deployment with its labels
kubectl describe  deployments my-deployment-1 #Describe the specified deployment 
kubectl  get deployment my-deployment-1 -o yaml #Get details of the deployment in yaml format
kubectl  set image deployment my-deployment-1 nginx=nginx:1.16.1 #Change image in the existing deployment
kubectl rollout history deployment my-deployment-1 #View rollout history
kubectl rollout undo deployment my-deployment-1 #Undo a previous rollout
kubectl rollout undo deployment my-deployment-1 --to-revision=2 #Go back the specific version of the rollout history
kubectl rollout status deployment my-deployment-1 #Show the status of the rollout
kubectl rollout restart deployment my-deployment-1 #Restart a resource
kubectl scale --replicas=3 deployment my-deployment-1 #Scale deployment to 3
kubectl scale --current-replicas=3 --replicas=5 deployment my-deployment-1 #Scale from current count to the desired
kubectl autoscale deployment my-deployment-1 --min=2 --max=10 #This will create an HPA (Horizontal Pod Aotuscaler)

services

First create a pod with label-app = myapp.

kubectl run my-pod --image=nginx --labels=app=myapp #Create a pod with label
kubectl expose pod my-pod --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml #Create a service of type NodePort which will use pod's labels for selector but we have to specify the type, so create a definition file first and then create a service
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml #Create a Service which will have type type NodePort but this will not have selector as my-app
kubectl  get service #Get services from the current context
kubectl  get service -o wide #Get details of the services
kubectl  get service --show-labels #Get services with labels on them
kubectl  get services --all-namespaces #Get services from all the namespaces
kubectl  describe  service nginx-service #Describe the service to know more about it
kubectl  get  service nginx-service #Get a particular service
kubectl  delete service nginx-service #Delete the service 

Manage items from .yaml / .yml files

First, create a definition file for a pod

kubectl  run mypod --image=nginx --dry-run=client -o yaml > my-pod.yml #Create a definition file for pod 
kubectl  create -f my-pod.yml #Create an object 
kubectl  delete -f my-pod.yml #Delete the object 

Conclusion

In this article we saw the important commands needed when working with Kubernetes. Commands in Kubernetes are not limited to just these commands but these commands are like “must know“Commands.


Source link