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 also mentioned at the following URL,
execute `kubectl exec` on a Pod running multiple containers and
this guide explains how to
https://kubernetes.io/ja/docs/tasks/debug-application-cluster/get-shell-running-container/
Start the Pod
First, let's try launching a Pod according to the following manifest.
It's a simple one that uses a deployment controller to launch 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 within this Pod:
$ kubectl exec -it example-web-59fccdb6d4-slrkv bash root@example-web-59fccdb6d4-slrkv:/#
That should allow you to log in.
Now, let's edit the manifest file a little.
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 command to apply the changes is the same as before, so I'll omit it here, but
when you start the Pod, the READY status will change to 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 appeared.
It meant, "The default container is named nginx, so please check all container information with kubectl describe."
In other words, the above command cannot be used if you want to log in to the PHP-FPM container.
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's obvious, but I'm writing it down as a memo.
I need to study Kubernetes more...
5
