How to K8s: Core Commands with Kubectl

Kubectl is one of the most popular and powerful ways of interacting with a Kubernetes cluster. Learn some core commands to get you started on the right foot in this blog post.

Kubernetes is controlled via an API that it exposes to users that can be interacted with in a variety of ways, but the most common means of controlling a K8s cluster is via the Kubernetes command line interface (CLI) with kubectl.

While the command syntax is generally pretty straightforward, there are a variety of tips and tricks that can make for much more efficient use of the K8s CLI. Below, we’ll cover some key commands that you will want to know, as well as some command variations that each follow a similar pattern in terms of their expression in the command line in order to help you move beyond just the basics quickly and easily.

Create or Update a Resource with a YAML Definition

When working with Kubernetes, you’ll be creating, modifying or deleting various resources in the system. While you can create resources entirely from the command line, it is considered best practice to define your API objects in YAML files that can be checked into version control and thus tracked easily over time, such as the following:

kind: Pod
apiVersion: v1
metadata:
name: myvolumes-pod
spec:
containers:
- image: alpine
imagePullPolicy: IfNotPresent
name: myvolumes-container
command: ['sh', '-c', 'echo Container 1 is Running ; sleep 3600']

With a file like we have above, we can simply execute the following to create a new resource, or update an existing resource such that it aligns with our current definition:

kubectl apply -f /path/to/YAML

List Resources of a Particular Type

In order to work with these resources once they have been created, you’ll need to be able to list them and their associated metadata. To do so, you will follow the following pattern on the command line:

kubectl get <resource type>

Put in practice, this can look like the following: kubectl get pods or kubectl get deployments, which will return a list of all pods or deployments, respectively, in the system.

Execute a Remote Command in a Deployed Pod

Although you will rarely need to manually execute a command in a running pod, there are certainly times that you will need to do so. For example, if your pod is not acting as expected, you will likely need to manually “SSH” into the running pod and its associated container for troubleshooting. To do so, we can use the exec command, like so:

kubectl exec -it <pod-name> -- /bin/bash

Pro tip: if you’re using Alpine containers, the command will be kubectl exec -it <pod name> -- /bin/sh, because bash is not included in the container image as a means of reducing its overall size.

We will know that we have successfully gained access, because the command line prompt will have a # at the end, like so:

root@<pod-name>/#

Note that you will have root access to the running pod by default.

Get Logs from a Running Pod

Because K8s is so completely self-contained in terms of its ability to provide high availability resources, it can sometimes be difficult to tell where a potential problem may lie. When in doubt, check the logs. You can access logs from a running pod in Kubernetes by running the following:

kubectl logs <pod-name>

TL;DR

The Kubernetes CLI (kubectl) is one of the most convenient and powerful ways to interact with the K8s API for commands to be executed by non-automated processes, such as a logged in user. Above, we have covered some core commands to create and interact with fundamental K8s API objects.