• Home
  • DevOps
  • Deep dive into Kubectl command line utility

Deep dive into Kubectl command line utility

Last updated on May 28 2022
Sankalp Rai

Table of Contents

Deep dive into Kubectl command line utility

Kubernetes – Kubectl

Kubectl is the command line utility to interact with Kubernetes API. It is an interface which is employed to communicate and manage pods in Kubernetes cluster.
One needs to set up kubectl to local in order to interact with Kubernetes cluster.

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 withwithin the path of the system.
$ chmod +x kubectl
$ mv kubectl /usr/local/bin/kubectl

Configuring Kubectl

Subsequent 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 = default-cluster —
user = default-admin
$ kubectl config use-context default-system
• Replace ${MASTER_HOST} with the master node address or name employed withwithin the previous steps.
• Replace ${CA_CERT} with the absolute path to the ca.pem created withwithin the previous steps.
• Replace ${ADMIN_KEY} with the absolute path to the admin-key.pem created withwithin the previous steps.
• Replace ${ADMIN_CERT} with the absolute path to the admin.pem created withwithin 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

Kubernetes – Kubectl Commands

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 withwithin the cluster.
Kubectl commands are employed to interact and manage Kubernetes objects and the cluster. In this chapter, we will discuss a few commands employed 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 employed 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 withwithin 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>
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 –
Withwithin the equivalent 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 employed 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 employed 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 withwithin the container.
$ kubectl exec POD <-c CONTAINER > — COMMAND < args…>
$ kubectl exec tomcat 123-5-456 date
kubectl expose − This is employed 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 employed to get the logs of the container in a pod. Printing the logs can be defining the container name withwithin 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 employed 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
kubectl set resources − It is employed 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 equivalent command can be employed with a pod as well.
So, this brings us to the end of blog. This Tecklearn ‘Deep dive into Kubectl command line utility’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Kubernetes and build a career in DevOps domain, then check out our interactive, Continuous Orchestration using Kubernetes Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Continuous Orchestration using Kubernetes

Continuous Orchestration using Kubernetes Training

About the Course

Tecklearn has specially designed this Continuous Orchestration using Kubernetes Training Course to advance your skills for a successful career in this domain. Kubernetes training helps you master the container orchestration tool. As part of the training, you will learn detailed Kubernetes, architecture of Kubernetes, what are Kubernetes Pods, node, how to deploy Kubernetes, creating a Kubernetes cluster, what are the various services available and how Kubernetes makes container orchestration simple. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Kubernetes.

Why Should you take Continuous Orchestration using Kubernetes Training?

• The average salary for people who possess Kubernetes as a skill is $117,000. – PayScale.com
• Apple, Capital One, AT&T, Oracle, Raytheon & many other MNC’s worldwide use Kubernetes across industries.
• The Kubernetes orchestration engine powers some of the biggest and most complex deployments in the world.

What you will Learn in this Course?

Introduction to DevOps
• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation
Continuous Orchestration using Kubernetes
• Containers and Container Orchestration
• Introduction to Kubernetes
• Docker Swarm vs Kubernetes
• Kubernetes Architecture
• Deploying Kubernetes using Kubeadms
• Alternate ways of deploying Kubernetes
• Understanding YAML
• Creating a Deployment in Kubernetes using YAML
• Creating a Service in Kubernetes
• Installing Kubernetes Dashboard
• Deploying an App using Dashboard
• Using Rolling Updates in Kubernetes

0 responses on "Deep dive into Kubectl command line utility"

Leave a Message

Your email address will not be published. Required fields are marked *