Kubernetes

Using Kubectl Exec Command: Connect to Kubernetes Containers

Connect to containers using Kubectl Exec

šŸš€ Level Up Your Infrastructure Skills

You focus on building. We’ll keep you updated. Get curated infrastructure insights that help you make smarter decisions.

In this article, we will look at theĀ kubectl exec command to show how to get a shell into a running container in your Kubernetes (K8S) cluster and how to run individual commands on a container with some useful examples.

How Does kubectl exec Work?

You can use kubectl exec to connect to a running container and also to execute single commands. Connecting to a container is useful to view logs, inspect processes, mount points, environment variables, and package versions, amongst other things.

kubectl exec will give you full shell access to the container, so modifying it and installing packages that are not part of the container image is possible but is not recommended unless for temporary troubleshooting purposes. If extra modifications or packages are required permanently for the container, the image should be modified, and the new version should be deployed to maintain immutability and reproducibility.

kubectl exec Syntax

Let’s take a look at the syntax of the kubectl exec command with an example.

kubectl exec --stdin --tty aks-helloworld-one-56c7b8d79d-xqx5t -- /bin/bash
  • aks-helloworld-one-56c7b8d79d-xqx5t is the name of the Pod with your container.
  • The double dash (--) separates the arguments you want to pass to the command from theĀ kubectlarguments.
  • /bin/bashĀ is the type of shell you want (it could also beĀ /bin/shĀ for example).
  • The--stdin option passes the stdin (or standard input) to the container. UseĀ -iĀ for short.
  • TheĀ --ttyĀ Stdin is a TTY. UseĀ -tĀ for short.
  • You can also specify theĀ --quietĀ orĀ -qĀ option to disable all output fromĀ kubectl itself. You’ll only see output produced by the process running in the container.
  • You can also specify the length of time to wait until at least one pod is running, the default being 1m usingĀ -pod-runnning-timeout. This is specified as the length of time (like 5s, 2m, or 3h, higher than zero), e.g.Ā --pod-running-timeout=2mĀ .

Example Setup

To get a shell to your container, first, find its name.

And to have all the commands in one place check out our Kubernetes Cheat Sheet with 15 Kubectl Commands & Objects.

kubectl get pods
kubectl exec - get pods

Note that in order to get a shell, your container image has to have that shell available.

I have one running on my Azure Kubernetes Service (AKS) cluster calledĀ aks-helloworld-one-56c7b8d79d-lkz6s,Ā which we will use for these examples. This uses theĀ Hello world image,Ā which is a simple Node.js web application used in Azure Container Instances for the examples on docs.microsoft.com.

Check out how to Provision Azure AKS Cluster Using Terraform.

This container is controlled by a deployment calledĀ aks-helloworld-one.

kubectl get deployments
kubectl exec - get deployments

MyĀ aks-helloworld-one.yamlĀ deployment file looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: aks-helloworld-one
spec:
  replicas: 1
  selector:
    matchLabels:
      app: aks-helloworld-one
  template:
    metadata:
      labels:
        app: aks-helloworld-one
    spec:
      containers:
      - name: aks-helloworld-one
        image: mcr.microsoft.com/azuredocs/aks-helloworld:v1
        ports:
        - containerPort: 80
        env:
        - name: TITLE
          value: "Welcome to Azure Kubernetes Service (AKS)"
---
apiVersion: v1
kind: Service
metadata:
  name: aks-helloworld-one
spec:
  type: ClusterIP
  ports:
  - port: 80
  selector:
    app: aks-helloworld-one

If you are following along, you can create the file above and use the command below to deploy the container on your cluster:

kubectl create deployment aks-helloworld-one.yaml

Connect to Your Container

To get a bash shell into the running container:

kubectl exec --stdin --tty aks-helloworld-one-56c7b8d79d-xqx5t -- /bin/bash

Once inside the container, you can run commands directly, e.g.Ā lsĀ to list the contents of the directory.

kubectl exec - ls inside container

OrĀ ps auxĀ to view the running processes:

kubectl exec - ps aux inside container

Once you are finished in the container, typeĀ exitĀ to return to your console shell.

Running Single Commands in a Container

You can also run single commands directly usingĀ kubectl exec. Some useful examples are shown below:

  • Get the time and date
kubectl exec <pod name> -- date
kubectl exec pod name - date
  • List the running environment variables
kubectl exec <pod name> -- env
kubectl exec pod name - env
  • Update packages
kubectl exec <pod name> -- apt-get update
kubectl exec pod name apt get update
  • View the mount points
kubectl exec shell-demo -- cat /proc/1/mounts

Connect to a Specified Container When a Pod Has More Than One Container

If a Pod has more than one container, useĀ --containerĀ orĀ -cĀ to specify a container in theĀ kubectl execĀ command.

kubectl exec -i -t <pod name> --container <container name> -- /bin/bash

Key Points

You can connect to a running container usingĀ kubectl exec and also use it to execute single commands. Connecting to a container is useful to view logs, inspect processes, mount points, environment variables, and package versions, amongst other things.

Also, check out how Spacelift helps you manage the complexities and compliance challenges of using Kubernetes. Anything that can be run via kubectl can be run within a Spacelift stack. Find out more about how Spacelift works with Kubernetes, and get started on your journey by creating a free trial account.

Cheers!

The most Flexible CI/CD Automation Tool

Spacelift is an alternative to using homegrown solutions on top of a generic CI. It helps overcome common state management issues and adds several must-have capabilities s for infrastructure management.

Start free trial

Frequently asked questions

  • What is the difference between kubectl run and kubectl exec?

    kubectl run creates and starts a new pod or container in the cluster, while kubectl exec runs a command inside an already running pod.

  • What is the difference between kubectl debug and kubectl exec?

    kubectl exec runs commands inside an existing container, while kubectl debug creates an ephemeral container or modified pod to troubleshoot without altering the original workload.

  • How to disable kubectl exec?

    You can disable kubectl exec by restricting the associated RBAC permissions in Kubernetes. Specifically, the pods/exec subresource must be denied in the roles or cluster roles assigned to users or service accounts.

Kubernetes Commands Cheat Sheet

Grab our ultimate cheat sheet PDF

for all the kubectl commands you need.

k8s book
Share your data and download the cheat sheet