Kubernetes Cheat Sheet Tutorial

Kubernetes Cheat Sheet Tutorial

Last updated on 08th Oct 2020, Blog, Tutorials

About author

Senthil (Sr Project Manager )

He is a Proficient Technical Expert for Respective Industry & Serving 7+ Years. Also, Dedicated to Imparts the Informative Knowledge to Freshers. He Share's this Blogs for us.

(5.0) | 12457 Ratings 3269

This Kubernetes cheat sheet is prepared by our experts keeping in mind those learners who have started learning Kubernetes as a tool. This cheat sheet will be a handy reference for them when they start implementing what they have learned in real-life problems. In this Kubernetes cheat sheet, you get a quick reference to all the basics that you must know about Kubernetes.

What is Kubernetes?

Kubernetes is an open-source platform used to automate deployment and to scale containers across clusters of hosts providing container-centric infrastructure.

  • It is a container orchestrator.
  • It can run a Linux container.
    • Launches a container
    • Maintains and monitors the container site
    • Performs container-oriented networking
Subscribe For Free Demo

Error: Contact form not found.

Architecture/Interaction

Key Concepts

Now, let’s discuss the key points of this architecture.

  • Pod: Group of containers
  • Label: Used to identify pods
  • Kubelet: Container agents, responsible for maintaining the set of pods
  • Proxy: The load balancer for pods, helping in distributing tasks across them
  • Etcd: A metadata service
  • CAdvisor: Used for monitoring resource usage and performance
  • Replication controller: Manages pod replication
  • Scheduler: Used for pod scheduling in worker nodes
  • API server: Kubernetes API server

Now, let’s understand the roles master and worker nodes play in the Kubernetes architecture.

Master Node

  • It is responsible for maintaining the desired state of the cluster we are working on.
  • The term ‘master‘ indicates a set of processes that are used to manage a cluster.
  • It contains Kubelet service info, API, scheduler, replication controller, and Kubernetes master.

Worker Nodes/Minions

Also called a ‘minion,’ a worker node contains the services necessary to run the pods that are managed by the master.

  • Services it provides are Container Runtime, Kubelet, Kube-proxy, etc.
  • It contains Kubelet, cAdvisor, Services, Pods, and Containers.

Moving forward, let’s check out the important commands used in Kubernetes.

Kubectl Commands

For Pods and Container Introspection

CommandFunction
Kubectl get podsLists all current pods
Kubectl describe pod<name>Describes pod names
Kubectl get rcLists all replication controllers
Kubectl get rc –namespace=”namespace”Lists replication controllers in a namespace
Kubectl describe rc <name>Shows the replication controller name
Kubectl get cvcLists services
Kubectl describe svc<name>Shows a service name
Kubectl delete pod<name>Deletes a pod
Kubectl get nodes -wWatches nodes continuously

For Debugging

FunctionCommand
To execute the command on service by selecting a containerKubectl exec<service><commands>[-c< $container>]
To get logs from the service for a containerKubectl logs -f<name>>[-c< $container>]
To watch kubelet logsWatch -n 2 cat/var/log/kublet.log
To show metrics for a nodeKubectl top node
To show metrics for a podKubectl top pod

For Cluster Introspection

FunctionCommand
To get version-related informationKubectl version
To get cluster-related informationKubectl cluster-info
To get configuration detailsKubectl config g view
To get information about a nodeKubectl describe node<node>

Objects

Some of the common objects used in Kubernetes are mentioned in the below table:

Allcluster role bindingsclusterroles
cm= conf gmapscontroller revisionscrd=custom resource definition
Cronjobscs=component statusescsr= certificate signing requests
Deploy=deploymentsds= daemon setsep=end points
ev= eventshpa= horizontal pod autoscalinging= ingress
jobslimits=limit rangesNetpol- network policies
No = nodesns= namespacespdb= pod distribution budgets
po= podsPod presetPod templates
Psp= pod security policiesPv= persistent volumespvc= persistent volume claims
quota= resource quotasrc= replication controllersRole bindings
rolesrs= replica setssa= service accounts
sc= storage classessecretssts= stateful sets

Other Quick Commands

Additionally, we do have some quick commands that are often very useful:

FunctionCommand
To launch a pod with a name and an imageKubectl run<name> — image=<image-name>
To create a service described in <manifest.yaml>Kubectl create -f <manifest.yaml>
To scale the replication counter to count the number of instancesKubectl scale –replicas=<count>rc<name>
To map the external port to the internal replication portExpose rc<name> –port=<external>–target-port=<internal>
To stop all pods in <n>Kubectl drain<n>– delete-local-data–force–ignore-daemonset
To create a namespaceKubectl create namespace <namespace>
To allow the master node to run podsKubectl Taint Nodes –all-node-role.kuernetes.io/master-

Features of Kubernetes

  • Automated scheduling: It provides an advanced scheduler that helps launch containers on cluster nodes.
  • Self-healing: Rescheduling, replacing, and restarting of dead containers are possible.
  • Automated rollouts and rollbacks: It supports rollback for systems in the case of failures and enables rollout and rollback for the desired state.
  • Horizontal scaling: It can be scaled up and down as required and can be automated with respect to the CPU usage.
  • Service discovery and load balancing: It uses a unique IP and DNS name for containers that helps in identifying them.

Kubectl is the command line utility to interact with Kubernetes API. It is an interface which is used to communicate and manage pods in Kubernetes cluster.

One needs to set up kubectl to local in order to interact with Kubernetes cluster.

Course Curriculum

Best In-Depth Practical Oriented Kubernetes Training By Expert Trainers

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

Setting Kubectl

Download the executable to the local workstation using the curl command.

On Linux

  • $ curl -O https://storage.googleapis.com/kubernetesrelease/
  • release/v1.5.2/bin/linux/amd64/kubectl

On OS X workstation

  • $ curl -O https://storage.googleapis.com/kubernetesrelease/
  • release/v1.5.2/bin/darwin/amd64/kubectl

After download is complete, move the binaries in the path of the system.

  • $ chmod +x kubectl
  • $ mv kubectl /usr/local/bin/kubectl

Configuring Kubectl

Following are the steps to perform the configuration operation.

  • $ kubectl config set-cluster default-cluster –server = https://${MASTER_HOST} —
  • certificate-authority = ${CA_CERT}
  • $ kubectl config set-credentials default-admin –certificateauthority = ${
  • CA_CERT} –client-key = ${ADMIN_KEY} –clientcertificate= ${
  • ADMIN_CERT}
  • $ kubectl config set-context default-system –cluster = defaultcluster —
  • user = default-admin
  • $ kubectl config use-context default-system
  • Replace ${MASTER_HOST} with the master node address or name used in the previous steps.
  • Replace ${CA_CERT} with the absolute path to the ca.pem created in the previous steps.
  • Replace ${ADMIN_KEY} with the absolute path to the admin-key.pem created in the previous steps.
  • Replace ${ADMIN_CERT} with the absolute path to the admin.pem created in the previous steps.

Verifying the Setup

To verify if the kubectl is working fine or not, check if the Kubernetes client is set up correctly.

  • $ kubectl get nodes
  • NAME LABELS STATUS
  • Vipin.com  Kubernetes.io/hostname = vipin.mishra.com    Ready

Kubectl controls the Kubernetes Cluster. It is one of the key components of Kubernetes which runs on the workstation on any machine when the setup is done. It has the capability to manage the nodes in the cluster.

Kubectl commands are used to interact and manage Kubernetes objects and the cluster. In this chapter, we will discuss a few commands used in Kubernetes via kubectl.

kubectl annotate −

It updates the annotation on a resource.

  • $kubectl annotate [–overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 …
  • KEY_N = VAL_N [–resource-version = version]

For example,

kubectl annotate pods tomcat description = ‘my frontend’

kubectl api-versions −

It prints the supported versions of API on the cluster.

  • $ kubectl api-version;

kubectl apply −

It has the capability to configure a resource by file or stdin.

  • $ kubectl apply –f <filename>

kubectl attach −

This attaches things to the running container.

  • $ kubectl attach <pod> –c <container>
  • $ kubectl attach 123456-7890 -c tomcat-conatiner

kubectl autoscale −

This is used to auto scale pods which are defined such as Deployment, replica set, Replication Controller.

  • $ kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [–min = MINPODS] —
  • max = MAXPODS [–cpu-percent = CPU] [flags]
  • $ kubectl autoscale deployment foo –min = 2 –max = 10

kubectl cluster-info −

It displays the cluster Info.

  • $ kubectl cluster-info

kubectl cluster-info dump −

It dumps relevant information regarding cluster for debugging and diagnosis.

  • $ kubectl cluster-info dump
  • $ kubectl cluster-info dump –output-directory = /path/to/cluster-state

kubectl config −

Modifies the kubeconfig file.

  • $ kubectl config <SUBCOMMAD>
  • $ kubectl config –-kubeconfig <String of File name>

kubectl config current-context −

It displays the current context.

  • $ kubectl config current-context
  • #deploys the current context

kubectl config delete-cluster −

Deletes the specified cluster from kubeconfig.

  • $ kubectl config delete-cluster <Cluster Name>

kubectl config delete-context −

Deletes a specified context from kubeconfig.

  • $ kubectl config delete-context <Context Name>

kubectl config get-clusters −

Displays cluster defined in the kubeconfig.

  • $ kubectl config get-cluster
  • $ kubectl config get-cluster <Cluser Name>

kubectl config get-contexts −

Describes one or many contexts.

  • $ kubectl config get-context <Context Name>

kubectl config set-cluster −

Sets the cluster entry in Kubernetes.

  • $ kubectl config set-cluster NAME [–server = server] [–certificateauthority =

path/to/certificate/authority] [–insecure-skip-tls-verify = true]

kubectl config set-context −

Sets a context entry in kubernetes entrypoint.

  • $ kubectl config set-context NAME [–cluster = cluster_nickname] [–
  • user = user_nickname] [–namespace = namespace]
  • $ kubectl config set-context prod –user = vipin-mishra

kubectl config set-credentials −

Sets a user entry in kubeconfig.

  • $ kubectl config set-credentials cluster-admin –username = vipin —
  • password = uXFGweU9l35qcif

kubectl config set −

Sets an individual value in kubeconfig file.

  • $ kubectl config set PROPERTY_NAME PROPERTY_VALUE

kubectl config unset −

It unsets a specific component in kubectl.

  • $ kubectl config unset PROPERTY_NAME PROPERTY_VALUE

kubectl config use-context −

Sets the current context in kubectl file.

  • $ kubectl config use-context <Context Name>
Course Curriculum

Learn In-Demand Industry Experts Curated Kubernetes Certification Course to Buid Your Skills

Weekday / Weekend BatchesSee Batch Details

kubectl config view

  • $ kubectl config view
  • $ kubectl config view –o jsonpath='{.users[?(@.name == “e2e”)].user.password}’

kubectl cp −

Copy files and directories to and from containers.

  • $ kubectl cp <Files from source> <Files to Destinatiion>
  • $ kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>

kubectl create −

To create resource by filename of or stdin. To do this, JSON or YAML formats are accepted.

  • $ kubectl create –f <File Name>
  • $ cat <file name> | kubectl create –f –

In the same way, we can create multiple things as listed using the create command along with kubectl.

  • deployment
  • namespace
  • quota
  • secret docker-registry
  • secret
  • secret generic
  • secret tls
  • serviceaccount
  • service clusterip
  • service loadbalancer
  • service nodeport

kubectl delete −

Deletes resources by file name, stdin, resource and names.

  • $ kubectl delete –f ([-f FILENAME] | TYPE [(NAME | -l label | –all)])

kubectl describe −

Describes any particular resource in kubernetes. Shows details of resource or a group of resources.

  • $ kubectl describe <type> <type name>
  • $ kubectl describe pod tomcat

kubectl drain −

This is used to drain a node for maintenance purpose. It prepares the node for maintenance. This will mark the node as unavailable so that it should not be assigned with a new container which will be created.

  • $ kubectl drain tomcat –force

kubectl edit −

It is used to end the resources on the server. This allows to directly edit a resource which one can receive via the command line tool.

  • $ kubectl edit <Resource/Name | File Name)

Ex.

  • $ kubectl edit rc/tomcat

kubectl exec −

This helps to execute a command in the container.

  • $ kubectl exec POD <-c CONTAINER > — COMMAND < args…>
  • $ kubectl exec tomcat 123-5-456 date

kubectl expose −

This is used to expose the Kubernetes objects such as pod, replication controller, and service as a new Kubernetes service. This has the capability to expose it via a running container or from a yaml file.

  • $ kubectl expose (-f FILENAME | TYPE NAME) [–port=port] [–protocol = TCP|UDP]
  • [–target-port = number-or-name] [–name = name] [–external-ip = external-ip-ofservice]
  • [–type = type]
  • $ kubectl expose rc tomcat –-port=80 –target-port = 30000
  • $ kubectl expose –f tomcat.yaml –port = 80 –target-port =

kubectl get −

This command is capable of fetching data on the cluster about the Kubernetes resources.

  • $ kubectl get [(-o|–output=)json|yaml|wide|custom-columns=…|custom-columnsfile=…|
  • go-template=…|go-template-file=…|jsonpath=…|jsonpath-file=…]
  • (TYPE [NAME | -l label] | TYPE/NAME …) [flags]

For example,

  • $ kubectl get pod <pod name>
  • $ kubectl get service <Service name>

kubectl logs −

They are used to get the logs of the container in a pod. Printing the logs can be defining the container name in the pod. If the POD has only one container there is no need to define its name.

$ kubectl logs [-f] [-p] POD [-c CONTAINER]

Example

  • $ kubectl logs tomcat.
  • $ kubectl logs –p –c tomcat.8

kubectl port-forward −

They are used to forward one or more local port to pods.

  • $ kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT
  • […[LOCAL_PORT_N:]REMOTE_PORT_N]
  • $ kubectl port-forward tomcat 3000 4000
  • $ kubectl port-forward tomcat 3000:5000

kubectl replace −

Capable of replacing a resource by file name or stdin.

  • $ kubectl replace -f FILENAME
  • $ kubectl replace –f tomcat.yml
  • $ cat tomcat.yml | kubectl replace –f –

kubectl rolling-update − Performs a rolling update on a replication controller. Replaces the specified replication controller with a new replication controller by updating a POD at a time.

  • $ kubectl rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] —
  • image = NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC)
  • $ kubectl rolling-update frontend-v1 –f freontend-v2.yaml

kubectl rollout −

It is capable of managing the rollout of deployment.

  • $ Kubectl rollout <Sub Command>
  • $ kubectl rollout undo deployment/tomcat

Apart from the above, we can perform multiple tasks using the rollout such as −

  • rollout history
  • rollout pause
  • rollout resume
  • rollout status
  • rollout undo

kubectl run −

Run command has the capability to run an image on the Kubernetes cluster.

  • $ kubectl run NAME –image = image [–env = “key = value”] [–port = port] [–
  • replicas = replicas] [–dry-run = bool] [–overrides = inline-json] [–command] —
  • [COMMAND] [args…]
  • $ kubectl run tomcat –image = tomcat:7.0
  • $ kubectl run tomcat –-image = tomcat:7.0 –port = 5000

kubectl scale −

It will scale the size of Kubernetes Deployments, ReplicaSet, Replication Controller, or job.

  • $ kubectl scale [–resource-version = version] [–current-replicas = count] —
  • replicas = COUNT (-f FILENAME | TYPE NAME )
  • $ kubectl scale –-replica = 3 rs/tomcat
  • $ kubectl scale –replica = 3 tomcat.yaml

kubectl set image −

It updates the image of a pod template.

  • $ kubectl set image (-f FILENAME | TYPE NAME)
  • CONTAINER_NAME_1 = CONTAINER_IMAGE_1 … CONTAINER_NAME_N = CONTAINER_IMAGE_N
  • $ kubectl set image deployment/tomcat busybox = busybox ngnix = ngnix:1.9.1
  • $ kubectl set image deployments, rc tomcat = tomcat6.0 –all
Kubernates Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

kubectl set resources −

It is used to set the content of the resource. It updates resource/limits on object with pod template.

  • $ kubectl set resources (-f FILENAME | TYPE NAME) ([–limits = LIMITS & —
  • requests = REQUESTS]
  • $ kubectl set resources deployment tomcat -c = tomcat —
  • limits = cpu = 200m,memory = 512Mi

kubectl top node −

It displays CPU/Memory/Storage usage. The top command allows you to see the resource consumption for nodes.

  • $ kubectl top node [node Name]

The same command can be used with a pod as well.

Are you looking training with Right Jobs?

Contact Us

Popular Courses