How to log in with kubectl exec when multiple containers are running in a Kubernetes Pod

I'm Teraoka, an infrastructure engineer.
This time, I'll be talking about Kubernetes.

As described in the URL below,

we will introduce how to run kubectl exec on a pod running multiple containers and

https://kubernetes.io/ja/docs/tasks/debug-application-cluster/get-shell-running-container/

Start the Pod

First, let's start a Pod according to the following manifest.
It is a simple Pod that uses a deployment controller to start a Pod running an Nginx container.

apiVersion: apps/v1 kind: Deployment metadata: name: example-web namespace: default spec: selector: matchLabels: app: example-web replicas: 1 template: metadata: labels: app: example-web spec: containers: - name: nginx image: nginx:latest imagePullPolicy: Always ports: - containerPort: 80

Reflect the manifest

$ Kubectl apply -f deployment_example_web.yaml

Check if the Pod is up and running

$ kubectl get pods NAME READY STATUS RESTARTS AGE example-web-59fccdb6d4-slrkv 1/1 Running 0 13s

It's up and running.
To log in to the container running in this Pod,

$ kubectl exec -it example-web-59fccdb6d4-slrkv bash root@example-web-59fccdb6d4-slrkv:/# 

Then you should be able to log in.
Next, let's edit the manifest file a bit.

apiVersion: apps/v1 kind: Deployment metadata: name: example-web namespace: default spec: selector: matchLabels: app: example-web replicas: 1 template: metadata: labels: app: example-web spec: containers: - name: nginx image: nginx:latest imagePullPolicy: Always ports: - containerPort: 80 - name: php-fpm image: php:7.4-fpm imagePullPolicy: Always ports: - containerPort: 9000

I also added a PHP-FPM container.
The commands to reflect this are the same as before, so I will omit them, but
when the Pod starts, the READY part will become 2/2.

$ kubectl get pods NAME READY STATUS RESTARTS AGE example-web-85cd8dbd8d-24rx4 2/2 Running 0 69s

Let's run the same kubectl exec as before

$ kubectl exec -it example-web-85cd8dbd8d-24rx4 bash Defaulting container name to nginx. Use 'kubectl describe pod/example-web-85cd8dbd8d-24rx4 -n default' to see all of the containers in this pod.

A message was displayed.
It meant that "The default container is named nginx, so check all container information with kubectl describe."
In other words, if you want to log in to a PHP-FPM container, you cannot use the above command.
You can log in by using the --container option as follows:

$ kubectl exec -it example-web-85cd8dbd8d-24rx4 --container php-fpm bash root@example-web-85cd8dbd8d-24rx4:/var/www/html# 

I was able to log in!

summary

It may seem obvious, but I'll leave it here as a reminder.
I need to study Kubernetes more...

If you found this article useful, please click [Like]!
4
Loading...
4 votes, average: 1.00 / 14
10,443
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yuki Teraoka

He joined Beyond in 2016 and is currently in his sixth year
as an infrastructure engineer working for an MSP, where he troubleshoots issues while also designing
and building infrastructure using public clouds such as AWS. Recently, he has been

using Hashicorp tools such as Terraform and Packer as part of the construction and operation automation
of container infrastructure such as Docker and Kubernetes, and he also plays the role of evangelist, speaking at external study groups and seminars.

・GitHub
https://github.com/nezumisannn

・Speaking history
https://github.com/nezumisannn/my-profile

・Presentation materials (SpeakerDeck)
https://speakerdeck.com/nezumisannn

・Certification:
AWS Certified Solutions Architect - Associate
Google Cloud Professional Cloud Architect