Updated on 2024-11-21 GMT+08:00

Logging In to a Container

Scenario

If you encounter unexpected problems when using a container, you can log in to the container to debug it.

Logging In to a Container Using CloudShell

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. In the navigation pane on the left, choose Workloads. Click the name of the target workload to view its pods.
  3. Locate the target pod and choose More > Remote Login in the Operation column.

    Figure 1 Accessing a container

  4. In the displayed dialog box, select the container you want to access and the command, and click OK.

    Figure 2 Selecting a container and login command

  5. You will be automatically redirected to CloudShell. Then, kubectl is initialized and runs the kubectl exec command to log in to the container.

    Wait for 5 to 10 seconds until the kubectl exec command is automatically executed.

    Figure 3 CloudShell page

Logging In to a Container Using kubectl

  1. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Run the following command to view the created pod:

    kubectl get pod
    The example output is as follows:
    NAME                               READY   STATUS    RESTARTS       AGE
    nginx-59d89cb66f-mhljr             1/1     Running   0              11m

  3. Query the container name in the pod.

    kubectl get po nginx-59d89cb66f-mhljr -o jsonpath='{range .spec.containers[*]}{.name}{end}{"\n"}'
    The example output is as follows:
    container-1

  4. Run the following command to log in to the container-1 container in the nginx-59d89cb66f-mhljr pod:

    kubectl exec -it nginx-59d89cb66f-mhljr -c container-1 -- /bin/sh

  5. To exit the container, run the exit command.