How To Deploy Ubuntu Pod in Kubernetes|OpenShift

0
4

How can I create a single Ubuntu Pod in a Kubernetes or OpenShift cluster?. In Kubernetes a Pod is a group of one or more containers, with shared storage/network resources, and a specification for how to run the containers.

When a Pod running a single container you can think of a it as a wrapper around a single container. Kubernetes manages Pods rather than managing the containers directly.

In this tutorial we will look at how you can deploy an Ubuntu Pod in Kubernetes or OpenShift cluster. This can be for Debug purposes or just testing network connectivity to other Pods and Services in the namespace.

Since Pods are designed as relatively ephemeral and disposable entities you should never run Production container workloads by creating Pods directly.  Instead, create them using workload resources such as Deployment.

We will create a sleep container from Ubuntu docker image using latest tag. Below is the Pod creation YAML contents.

$ vim ubuntu-pod.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

Then apply the file created using kubectl command:

kubectl apply -f ubuntu-pod.yaml

You can run below kubectl commands 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

Expected output:

pod/ubuntu created

Check the Pod’s status:

$ kubectl get pod ubuntu
NAME     READY   STATUS    RESTARTS   AGE
ubuntu   1/1     Running   0          34s

Running as deployment:

$ vim ubuntu-pod-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: ubuntu
  name: ubuntu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ubuntu
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: ubuntu
    spec:
      containers:
      - image: ubuntu
        name: ubuntu
        command: ["/bin/sleep", "3650d"]
        resources: {}

Apply file manifest

kubectl apply -f ubuntu-pod-deployment.yaml

Access Pod’s shell

Once you confirm the Pod is running you can access its shell session with kubectl or oc command.

Using kubectl command:

kubectl exec --stdin --tty ubuntu -- /bin/bash

To quit shell use exit command:

root@ubuntu:/# exit
exit

Using oc command:

$ oc rsh  --shell=/bin/bash ubuntu
root@ubuntu:/# exit
exit

Installing Package in an Ubuntu Pod

You can use standard ubuntu apt package management tool for installation and removal of software packages.

Example below install telnet into the Ubuntu container.

$ kubectl exec --stdin --tty ubuntu -- /bin/bash
root@ubuntu:/# apt update
root@ubuntu:/# 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 telnet installed.

root@ubuntu:/# 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 usable. Check other good guides we have on containers: