skip to Main Content

I have one deployment which has two containers.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ol000dep
spec:
  selector:
    matchLabels:
      app: ol000kube
  replicas : 2
  template:
    metadata:
      labels:
        app: ol000kube
    spec:
      containers:
        - name: django
          image: django:local
          ports:
          - containerPort: 8011
      containers:
        - name: nginx
          image: nginx:local
          ports:
          - containerPort: 80

This makes two replicad pods, and on my understanding, each pod has two containers.

kubectl get pods      
NAME                       READY   STATUS    RESTARTS   AGE
ol000dep-bc96bfc98-r9lrj   1/1     Running   0          21m
ol000dep-bc96bfc98-t6flw   1/1     Running   0          21m

Now I can login

kubectl exec -it ol000dep-bc96bfc98-r9lrj  /bin/bash

Then, I wonder,

Is this login in a Pod not a Container??

If so ,how can I select the container I want to login?

2

Answers


  1. Try this:

    kubectl exec -it ol000dep-bc96bfc98-r9lrj  --container django(nginx)  -- /bin/bash
    
    Login or Signup to reply.
  2. Interesting. Even though the yaml you provided is incorrect (.spec.containers map key must be unique), k8s seems to be fine with that.

    In your case the pod is started with only the second container (name: nginx) in it though.

    Is this login in a Pod not a Container??

    Container.

    So, with kubectl exec -it ol000dep-bc96bfc98-r9lrj /bin/bash, you login/exec into the nginx container.

    After correcting the yaml, two containers would be started in the pod and you can log into the desired container via its name (e.g. name: django) using the -c / --container parameter.

    ...
          containers:
            - name: django
              image: django:local
              ports:
              - containerPort: 8011
            - name: nginx
              image: nginx:local
              ports:
              - containerPort: 80
    

    login:

    kubectl exec -it POD_NAME -c CONTAINER_NAME -- /bin/bash 
    

    Note that if you do not specify the name of the container (by omitting -c CONTAINER_NAME), you will login into the first defined container by default (in your case django).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search