Assigning a shell to a Kubernetes Container

Filed Under: Random
Assigning Shell To A Kubernetes Container

Hello, readers! In this article, we will be focusing on Assigning a shell to a Kubernetes Container with examples.

So, let us begin!! 馃檪


Need of a shell in a Kubernetes Container

As we have already discussed, a Container is actually an encapsulated package of your application. The Pod is where the container as a packaged application resides. A Pod is a running instance of the application. When we have a container running as an application instance, it depicts all the configuration files that are a part of it.

Now, we may have some storage blocked at some location within the container for temporary or volatile purposes during the lifecycle of the application. Does the question arise that how are we going to reach that location within the container? Yes!

And, how will be going into the container and search for a particular file at a location?

The shell comes as a rescue to all the above-mentioned questions.

By attaching and assigning a shell to a container, we can get into the container while the application is in a running condition.

In the context of this topic, we will be unveiling the process to attach a shell to a running instance of a container.


1. Attaching a shell to a running Kubernetes Container

At first, we will be creating a pod with a single container inside it.

Have a look at the below YAML.

In this example, we have created a pod with Nginx as the base image for the container.

pod.YAML

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  volumes:
  - name: data-vol
    emptyDir: {}
  containers:
  - name: nginx-shell
    image: nginx
    volumeMounts:
    - name: data-vol
      mountPath: /usr/nginx/data

Now, it’s time to apply the above YAML for the creation of the pod.

kubectl apply -f pod.yaml -n demo_ns

Let us verify that the container/pod is running.

kubectl get po -n demo_ns

As the pod is running, we will now attach a shell to the running nginx container within the pod.

kubectl exec -it nginx-pod -- /bin/bash

When we execute the above command, it opens a shell and gets us inside the container. We can now run various commands to check and verify scenarios.

  1. At first, we look for the current working directory using pwd command.
  2. Then, ls command enables us to get a list of all the directories present in the specific path.
  3. We then create a file named hello.txt using touch command.
  4. Having created a file, we make use of echo command to attach text into the above created file.
  5. At last, we make use of cat command to get the content of the file.
root@nginx-pod:/# pwd

/

root@nginx-pod:/# ls

bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

root@nginx-pod:/# touch hello.txt

root@nginx-pod:/# ls

bin  boot  dev  etc  hello.txt  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

root@nginx-pod:/# echo "Welcome to JournalDev" > hello.txt

root@nginx-pod:/# cat hello.txt

Wellcome to JournalDev

2. Run individual commands separately in a container

Apart from running commands within a shell, we can trigger individual commands to run as a query to the running container.

For instance, we can list the environment variables that are a part of the running nginx container within the pod.

kubectl exec nginx-shell env

Output:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

HOSTNAME=nginx-pod

KUBERNETES_SERVICE_PORT=443

KUBERNETES_SERVICE_PORT_HTTPS=443

KUBERNETES_PORT_443_TCP_PROTO=tcp

KUBERNETES_PORT_443_TCP_PORT=443

HOME=/root

In another instance, we can have a look at the running processes within the container using the below command.

kubectl exec nginx-shell -- ps aux

Output:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root           1  0.0  0.0   7288  1836 ?        Ss   Jul29   0:00 tail -f /dev/null

root         120  1.0  0.0  37300  3480 ?        Rs   13:24   0:00 ps aux

We can even list the files within the container using the below command inside the shell.

kubectl exec nginx-shell -- ls /

Output:

bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

3. Connecting to a shell of a multi-container Pod

It is not necessary that every pod will have a single container running inside it. That is, a pod may have multiple containers running within it.

If a pod has more than one container, we can make use of -c and specify a particular container to which we want to attach the shell prompt.

In the below example, we have created a pod with two containers running into it: nginx and busybox.

apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
spec:
  volumes:
  - name: data-vol
    emptyDir: {}
  containers:
  - name: nginx-shell
    image: nginx
    volumeMounts:
    - name: data-vol
      mountPath: /usr/nginx/data
  - name: busybox-shell
    image: busybox
    volumeMounts:
    - name: data-vol1
      mountPath: /usr/busybox/data
kubectl apply -f pod.yaml -n demo_ns

Having created the pod, let us now try to attach the shell to busybox container.

kubectl exec -it multi-pod --c busybox-shell -- /bin/bash

Output:

root@busybox-shell:/# pwd
/

root@busybox-shell:/# ls

bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Kubernetes, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content