DevOps 2021: Kubernetes Cheatsheet for Advanced level
Kubernetes CheatSheet- Advanced
In continuation with the earlier post on “Kubernetes Cheatsheet for Beginners”, we bring another new article for you.. Here we will try to some of the more advanced Kubernetes commands which might be useful for the intermediate and advanced level users. Although there is no such thing as advanced commands we will try to get deeper into some of the commands.
Kubectl context and configuration setting
This command is used to set the kubernetes cluster and to modify the configuration information.
kubectl config set-credentials kubeuser/foo.kubernetes.com
This command is used to add a new user to your kubeconf that supports basic authorization.
kubectl config set-context –current –namespace=ggckad-s2
This command is used to permanently save the namespace for all subsequent kubectl commands in that context.
kubectl config unset users.foo
This command is used to delete a user. For e.g. foo
Creating Kubernetes objects through commands
The manifest file maintains the metadata for the application. The file extensions used are .yaml, .yml, .json.
kubectl apply -f ./my-manifest.yaml
This command is used to create single or multiple resources.
kubectl apply -f ./my1.yaml -f ./my2.yaml
This command is used to create objects from multiple files.
kubectl apply -f ./dir
This command is used to create single or multiple resources in all manifest files in directory.
kubectl apply -f https://git.io/vPieo
This command is used to create resources from a URL.
kubectl explain pods
This command is used to get the documentation for pod manifests.
Viewing, Finding resources using commands
These are some basic commands to get help find and view the resources.
kubectl get services
This command is used to get the list all services in the namespace.
kubectl get pods –all-namespaces
This command is used to List all pods in all namespaces.
kubectl get pods -o wide
This command is used to get the list all pods in the current namespace, with more details.
kubectl get pod my-pod -o yaml
This command is used to get a pod’s YAML file.
kubectl describe nodes my-node
kubectl describe pods my-pod
This command is used to describe commands with verbose output.
kubectl get services –sort-by=.metadata.name
This command is used to get the list of pods sorted by restart count.
kubectl get pods –field-selector=status.phase=Running
This command is used to get all running pods in the namespace.
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type==”ExternalIP”)].address}’
This command is used to get external IPs of all nodes.
kubectl get events –sort-by=.metadata.creationTimestamp
This command is used to list the events sorted by timestamp.
kubectl diff -f ./my-manifest.yaml
This command is used to compare the current state of the cluster against the state that the cluster would be in if the manifest was applied.
Moving further with the commands let’s see what all operations can be done with commands.
Updating Resources through commands
kubectl rollout history deployment/frontend
This command is used to check the history of deployments done including the revision.
kubectl rollout undo deployment/frontend
This command is used to roll back to the previous deployment.
kubectl rollout status -w deployment/frontend
This command is used to watch rolling update status of “frontend” deployment until completion.
kubectl replace –force -f ./pod.json
This command is used to force replace, delete and then re-create the resource.
Patching Resources through commands
kubectl patch node k8s-node-1 -p ‘{“spec”:{“unschedulable”:true}}’
This command is used to partially update a node.
kubectl patch pod valid-pod –type=’json’ -p='[{“op”: “replace”, “path”: “/spec/containers/0/image”, “value”:”new image”}]’
This command is used to update a container’s image using a json patch with positional arrays.
kubectl patch sa default –type=’json’ -p='[{“op”: “add”, “path”: “/secrets/1”, “value”: {“name”: “whatever” } }]’
This command is used to add a new element to a positional array.
Scaling Kubernetes resources through commands
kubectl scale –replicas=3 rs/foo
This command is used to scale a replicaset named ‘foo’ to 3.
kubectl scale –replicas=3 -f foo.yaml
This command is used to Scale a resource specified in “foo.yaml” to 3.
kubectl scale –replicas=5 rc/foo rc/bar rc/baz
This command is used to scale multiple replication controllers.
Deleting the resources through commands
kubectl delete -f ./pod.json
This command is used to delete a pod using the type and name specified in pod.json.
kubectl delete pod,service baz foo
This command is used to delete pods and services with same names “baz” and “foo”.
kubectl delete pods,services -l name=myLabel
This command is used to delete pods and services with label name ‘myLabel’.
Interacting with nodes and cluster through commands
kubectl cordon my-node
This command is used to mark my-node as unschedulable.
kubectl uncordon my-node
This command is used to mark my-node as schedulable.
kubectl top node my-node
This command is used to show some parameters and metrics for a given node.
kubectl cluster-info
This command is used to display addresses of the master and services.
kubectl cluster-info dump
This command is used to dump current cluster state to standard output.