How to create a single Ubuntu Pod in a Kubernetes or OpenShift cluster?In Kubernetes, Pod is a group of one or more Container, With shared storage/network resources, and specifications on how to run containers.
When a Pod runs a single container, you can think of it as the packaging of a single container. Kubernetes manages Pods instead of directly managing containers.
In this tutorial, we will study how to deploy Ubuntu Pod in a Kubernetes or OpenShift cluster. This can be used for debugging purposes, or just to test the network connection to other Pods and Services in the namespace.
Since Pods are designed as relatively short-lived and one-off entities, you should never create Pods directly to run production container workloads.Instead use something like Deployment method.
We will create a sleeping container from the Ubuntu docker image using the following command Latest tags. The following is the content of Pod to create YAML.
apiVersion: v1 kind: Pod metadata: name: ubuntu labels: app: ubuntu spec: containers: - name: ubuntu image: ubuntu:latest command: ["/bin/sleep", "3650d"] imagePullPolicy: IfNotPresent restartPolicy: Always
You can run it under the kubectl command to deploy the Pod in the current namespace:
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: ubuntu labels: app: ubuntu spec: containers: - name: ubuntu image: ubuntu:latest command: ["/bin/sleep", "3650d"] imagePullPolicy: IfNotPresent restartPolicy: Always EOF
Check the status of the Pod:
$ kubectl get pod ubuntu NAME READY STATUS RESTARTS AGE ubuntu 1/1 Running 0 34s
Access the pod shell
Once you confirm that the Pod is running, you can use kubectl or oc commands to access its shell session.
Use kubectl command:
kubectl exec --stdin --tty ubuntu -- /bin/bash
To exit the shell, use the exit command:
[email protected]:/# exit exit
Use the oc command:
$ oc rsh --shell=/bin/bash ubuntu [email protected]:/# exit exit
Install packages in Ubuntu Pod
You can use the standard ubuntu apt package management tool to install and remove packages.
The following example installs telnet into an Ubuntu container.
$ kubectl exec --stdin --tty ubuntu -- /bin/bash [email protected]:/# apt update [email protected]:/# apt install telnet Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: netbase The following NEW packages will be installed: netbase telnet 0 upgraded, 2 newly installed, 0 to remove and 4 not upgraded. Need to get 77.2 kB of archives. After this operation, 207 kB of additional disk space will be used. Do you want to continue? [Y/n] y
Confirm that we can use the installed telnet.
[email protected]:/# telnet 10.10.6.5 8080 Trying 10.10.6.5... Connected to 10.10.6.5. Escape character is '^]'. E��^] telnet> quit Connection closed.
We have confirmed that our Pod is available. Check out our other good guides on containers:
Install Grafana on Kubernetes for cluster monitoring
Use K3s to install a Kubernetes cluster on Ubuntu
Using ECS to run Docker containers on AWS-part 1
Run Docker container on AWS ECS-upload Docker image to ECR