kubectl is the common CLI tool that we use to query and manage a Kubernetes cluster. kubectl uses the API interface of Kubernetes to view, control, and manage the cluster. It is supported across different platforms and can be easily set up to manage a cluster. In this article, we’ll be covering some common kubectl commands that help in day to day administration of Kubernetes.
Getting kubectl
kubectl is already installed as part of the Kubernetes cluster setup. In case you’re managing a cluster from a remote system, you can easily install it to work with any cluster setup. On a Linux system, you can use the below command to get the latest version of kubectl: Make sure to place the downloaded binary to a fixed location, add the location to your PATH variable and make it executable with chmod +x command. On a Windows box, you can download the latest available kubectl version by first getting the latest stable release version from https://dl.k8s.io/release/stable.txt And then download it by replacing {version} with the latest release using curl as: For v 1.20.0, here’s the curl command: If you don’t have curl on your system, you can also download the kubectl executable using your browser like any other download. For other supported methods and platforms, you can find the official guide to get kubectl here.
Verify kubectl Setup
To check your kubectl setup, you can run the version command as: The general syntax for kubectl usage is:
Set Context and Configuration
Before using kubectl commands on a Kubernetes cluster, we have to set the configuration and context first. It can be done with kubectl command itself. To view kubectl current configuration, use: To list all available contexts: To get current context for kubectl: We can change the context in use by using: To authorize a new user to be added in kubeconf: For example, to set only the “client-key” field on the “cluster-admin” without touching other values, we can use: Or, as another example, to set basic auth for, say “cluster-admin” entry, you can specify username and password as: If you want to embed client certificate data in the “cluster-admin” entry, syntax changes to: If you want kubectl to use a specific namespace and save it for all subsequent kubectl commands in that context:
Creating Objects
kubectl is used to deploy different objects supported in a Kubernetes cluster. Its manifest can be defined in a YAML or JSON file with extension .yaml or .yml and .json respectively. Using the given manifest file, we can create defined resources using the following command: Or to specify multiple YAML files, use: To create a resource(s) in all manifest files present in a directory: Or to create resources from a URL: Or directly from image name from the repository as: For example, to deploy a single instance of Nginx web server: Finally, to deploy resources directly by typing the YAML contents in CLI without referring to an actual saved manifest file, try something like:
View/Find Resources
kubectl provides get command to list down the deployed resources, get their details, and find out more about them. To list all services in the default namespace, use: Similarly, for listing pods in all the namespaces, the syntax will be: If we need to list down more details of deployed pods, use -o wide flag as: Syntax to get deployment details goes like this: To get a pod’s YAML content, we can use -o yaml flag like: Often we need to get details on Kubernetes resources. kubectl’s describe command helps in getting those details. We can get more details about a node as: Or similarly for pods as: kubectl allows sorting the output based on a particular field. To list services sorted by service name, use: Or to get all running pods in the namespace, we can try: To fetch just the external IPs of all nodes, if assigned, we can use -o jsonpath flag with the below syntax: To fetch labels attached to a resource, say pods, try: For getting a list of events but which is sorted by timestamp, we can use -sort-by flag as: If we want to compares the current state of the cluster against the state that the cluster would be in if the manifest was applied, we use diff command as:
Modifying Resources
Deployed resources would often be modified for any configuration changes. To perform a rolling update of, say “www” containers of, say “frontend” deployment by updating their image, we can use: We can check the history of deployments including revision as: Or to rollback to a previous deployment, use: We can also rollback to a specific revision by specifying –to-revision flag as: And to check rolling update status, we use: For rolling restart of, say “frontend” deployment, use: We can specify a JSON manifest to replace a pod by passing it to standard input as shown below: It may happen that you need to force replace, delete, and then re-create a resource (NOTE: this will also cause a service outage) which can be done as: Labeling a resource (which supports labels) is easy and can be done using: Similarly, annotation can be added to a resource using: Autoscaling a deployment is possible with: Here, dep-name is the name of the deployment to be autoscaled and min-val and max-val denotes the minimum and maximum value to be used for auto-scaling.
Editing resources
It is possible to edit an API resource in your preferred editor with edit command. Or to use your own alternative editor, specify KUBE_EDITOR like:
Scaling resources
Scaling resource is one of the features supported by Kubernetes and kubectl makes it easy to do so. For scaling a replica set named, say foo to 3, we use: Or instead, we can refer to a manifest YAML file to specify the resource to be scaled as: We can additionally perform scaling based on the current state of deployment as:
Deleting resources
Created resources will eventually need some modifications or deletion. With kubectl, we can delete existing resources in several ways. To delete a pod using the specification from the JSON file, we use: We can delete pods and services with the same names pod-name and service-name as: If the resources are labeled and we need to delete resources with a specific label, say label-name, we can use: To delete every pods and service contained in a namespace, use:
Interacting with running Pods
We can use kubectl to get details about running pods that helps administer a Kubernetes cluster. One of the common commands is to get logs of a pod which can be done as: Or to dump pod logs with a specific label: Or to get logs for a specific container as: We can also stream logs as we do with Linux tail -f command with kubectl’s -f flag as well: Running a pod interactively can be done with kubectl as: Or to run a pod in specific namespace use: You can attach it to a running container with attach command: Port forwarding can be done for a pod at runtime with the below command: To execute something directly in a pod login and get the output use: The above command works if the pod contains a single container. For multi-container pods, use: To show performance metrics for a given pod and its containers, we can use: Or to sort it by a measure say CPU or memory, we can achieve it using:
Interacting with Nodes and cluster
kubectl can interact with the nodes and cluster. Here are some of the commands kubectl uses for the same. For marking a node as un-schedulable, use: To drain a node as part of the preparation for maintenance: To mark the node back as schedulable, use: For getting performance metrics related to a node, we can use: To get details about the current cluster: We can additionally dump the cluster state to standard output using: Or to dump to a file use:
Conclusion
Kubernetes is the buzzword in the industry and knowing how to manage it effectively will always boost your career. kubectl is the primary interface to interact with a Kubernetes cluster and this article demonstrated how powerful this tool is in the hands of an experienced user. Still, we are able to cover only a summarised view of what more can kubectl possibly do. To explore it in further detail and check out what all it supports, refer to its official documentation here.