What is the Difference Between kubectl attach and kubectl exec?

adil
4 min readSep 22, 2023

--

A running Kubernetes pod may be reached through several different ways. Kubectl provides two methods for interacting with a Pod: kubectl exec and kubectl attach

Before starting the article, I would like to point out that I created a container image for this article: ailhan/control

Here is the source code

The container image has four different tags:

ailhan/control:latest -> Date time is printed every second
ailhan/control:datetime -> Date time is printed every second
ailhan/control:time -> Only time is printed every second
ailhan/control:hostname -> Container hostname is printed every second

Each container image handles CTRL+C and CTRL+Z interrupts. It does not kill the container when you press CTRL+C. When you press CTRL+Z, the container will be exited.

Example output:

Photo by Anne Nygård on Unsplash

Most likely, you are already familiar with kubectl exec . You may log in to a container running inside a pod:

hello-pod.yaml

---
apiVersion: v1
kind: Pod
metadata:
name: hello-pod
spec:
containers:
- image: ailhan/control:hostname
name: hello-container

Apply:

kubectl attach

There is another command to interact with a Pod: kubectl attach

You can access the container’s stdin and stdout via kubectl attach.

An example pod:

00-pod-datetime.yaml

---
apiVersion: v1
kind: Pod
metadata:
name: print-datetime
spec:
containers:
- image: ailhan/control:datetime
name: print-datetime-container

Apply:

We can see the logs. Each second, the date and time are written to the standard output. But as of yet, we are unable to engage with the main process of the container directly.

You can attach your terminal to the main process of the container inside the pod

kubectl attach:

I’ve attached my terminal to the first container of the print-datetime pod.

I had to send CTRL+C. Otherwise, I would see the date and time every second in my terminal.

At the beginning of the article, I said the container will handle your CTRL+C and CTRL+Z interrupts. But the container doesn’t seem to catch my interrupt.

Why isn’t the container handling my CTRL+C interrupt?

Because we have to make it clear that stdin and tty of the container must be accessible.

01-pod-datetime.yaml

---
apiVersion: v1
kind: Pod
metadata:
name: print-datetime-with-stdin
spec:
containers:
- image: ailhan/control:datetime
name: print-datetime-container
tty: true
stdin: true

Apply:

Oops! My terminal won’t connect to the pod I just made. Because I set tty and stdin variables in the yaml file.

I should attach my terminal to the pod with -it parameters:

As I said at the beginning of the article, the script running within the container handles CTRL+C and CTRL+Z interrupts. Since I could access the container’s stdin, I could send interrupts to the container itself.

How do you attach if there are multiple containers in a pod?

02-pod-datetime-hostname.yaml

---
apiVersion: v1
kind: Pod
metadata:
name: print-datetime-hostname-with-stdin
spec:
containers:
- image: ailhan/control:datetime
name: print-datetime-container
tty: true
stdin: true
- image: ailhan/control:hostname
name: print-hostname-container
tty: true
stdin: true

Apply:

Two containers are running in the pod.

Try to attach to the pod:

The terminal is attached to the first container in the pod. I can attach my terminal to the second container via the -c parameter

--

--