Home > Article >

Article: Mastering your Kubernetes Cluster - the kubectl exec Command

The kubectl exec command allows you to execute a command in a container.

Published

kubectl exec command
Author: Mensah Alkebu-Lan

Pre-Requisites #

  • Some familiarity with environment variables
  • Some familiarity with Kuberetes clusters

Table of Contents #

Introduction #

It’s not difficult to get started locally with Kubernetes. Minikube is one of a number of ways. You even have an online Kubernetes playground called Play with Kubernetes.

For the following exercise, we will be using Microk8s. Microk8s can be easily installed on your macOS, Linux, or Windows desktop. On Microk8s, you can work with a single node or go so far as to use multipass to build and deploy a highly available Kubernetes cluster.

If you are using Ubuntu, you can use the following snap:

sudo snap install microk8s --classic

It’s as simple as that.

In order to verify it is up and running, you can do the following:

micork8s status –wait-ready

The following exercise will utililize kubectl-- the Kubernetes command-line tool. You’ll notice when using kubectl with Microk8s, you have to use microk8s kubectl … instead of the usual kubectl …. A small price for convenience. If you’d prefer to use the bare kubectl command you would just need to use microk8s config to setup your kubeconfig file.

Creating the Pod #

We will start by creating a pod with one container. For those new to Kubernetes pods, all we need to know about a pod right now is it is the smallest deployable unit of computing you can create and manage in Kubernetes.

Pods generally aren’t created directly as we will do in the following exercise, but once you understand the concept of a pod, it will be easier for you to understand the concept of something like a job in Kubernetes. The pod is one of Kubernetes’ most fundamental concepts.

Let’s create a pod with the following configuration:

apiVersion: v1
kind: Pod
metadata:
name: shell-demo
spec:
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: nginx
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
hostNetwork: true
dnsPolicy: Default

You can create the file yourself (in case you want to experiment with different configurations later), or just use a copy of this already available online by executing the following kubectl command.

kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml

You should get a prompt similar to “pod/shell-demo created." Further, you can run the following command to verify your container is running:

kubectl get pod shell-demo

In which case, the output should be a one row table entry for shell-demo with a status of “Running.”

The kubectl exec command #

We are now ready to learn a new command called the kubectl exec command. This command allows you to execute a command in a container. For example, we can execute the following to get a shell to our running “shell-demo” pod.

kubectl exec --stdin --tty shell-demo -- /bin/bash

From here, we should be able use a typical linux command like ls to see the directory.

Something else we could do with the exec command is to list the environment variables in our running container. For example, we can execute the following command:

kubectl exec shell-demo -– env

If the command is executed correctly, you should see a list of environment variables. The majority of these variables should be KUBERNETES_* environment variables.

We’re done. Feel free to experiment from here. If you want to stop your microk8s instance, just use the microk8s stop command. It may take several seconds to stop, but it will eventually.

References #

  1. Pods | Kubernetes.
  2. Install Tools | Kubernetes.
  3. Kubectl Reference Docs.