In this blog post we will be showing you how to run CentOS container Pod in Kubernetes and OpenShift environment. CentOS is a community-driven operating system focused on delivering a robust open source ecosystem around a Linux platform. It is one of the most adopted enterprise operating systems which is associated with Red Hat Enterprise Linux OS.

To run CentOS container Pod in Kubernetes and OpenShift, you’ll use the same process of running normal microservice applications in Kubernetes. You start by creating a deployment manifest. One of the main reasons for running a CentOS Container in Kubernetes|OpenShift is installation of admin tools e.g telnet or tcpdump tools for network connectivity and packet capturing tasks.

Run CentOS Container Pod in Kubernetes | OpenShift

Let’s generate a CentOS Pod deployment YAML manifest file with kubectl.

# CentOS 8 Pod creation
kubectl run centos --image=centos:8 --restart=Never --dry-run=client -o yaml > centos-pod.yaml

# CentOS 7 Pod creation
kubectl run centos --image=centos:7 --restart=Never --dry-run=client -o yaml > centos-pod.yaml

We’ll edit the YAML file and add command to be executed in the container after the image definition under spec.containers section.

$ vim centos-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: centos
  name: centos
spec:
  containers:
  - image: centos:8
    name: centos
    command: ["/bin/sleep", "3650d"]
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

As you can see I’ve added the command to make the container sleep forever.

To create the Pod run the command:

$ kubectl apply -f centos-pod.yaml
pod/centos created

You can as well create the Pod in a specified namespace.

$ kubectl apply -f centos-pod.yaml -n <namespace-name>

Check if the Pod is running:

$ kubectl get pods centos7
NAME      READY   STATUS    RESTARTS   AGE
centos7   1/1     Running   0          57s

Connect to the Pod console and start bash shell using the command below.

$ kubectl exec -ti centos -- bash
[[email protected] /]# 

You should be able to do package installation and any tool you want.

[[email protected] /]# yum update
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                                                                                         25 MB/s | 6.2 MB     00:00
CentOS Linux 8 - BaseOS                                                                                                           6.1 MB/s | 2.3 MB     00:00
CentOS Linux 8 - Extras                                                                                                            27 kB/s | 8.1 kB     00:00
Dependencies resolved.
Nothing to do.
Complete!

[[email protected] /]# yum -y install telnet vim tcpdump

When done

[[email protected] /]# exit
exit

Deleting CentOS Pod.

$ kubectl delete pod centos
pod "centos" deleted

Run using Kubernetes Deployment resource type

If you want to use a deployment which manages creating Pods by means of ReplicaSets generate deployment template with the command below. Before you can run CentOS container Pod in Kubernetes you need to generate deployment manifest file.

# CentOS 8 image
kubectl create deploy centos --image=centos:8  --replicas=1 --dry-run=client -o yaml > centos-deployment.yaml

# CentOS 7 image
kubectl create deploy centos --image=centos:7  --replicas=1 --dry-run=client -o yaml  > centos-deployment.yaml

I’ll edit the YAML file to add run command similar to how we modified pod manifest file.

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: centos
  name: centos
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: centos
    spec:
      containers:
      - image: centos:8
        command: ["/bin/sleep", "3650d"]
        name: centos
        resources: {}
status: {}

Create Pod by applying the deployment manifest file.

$ kubectl apply -f centos-deployment.yaml
deployment.apps/centos created

Check deployment creation status:

$ kubectl get deploy centos
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
centos   1/1     1            1           31s

Confirm the Pod is running

$ kubectl get pod centos
NAME                      READY   STATUS    RESTARTS   AGE
centos-85898cf6d8-dbz4r   1/1     Running   0          81s

Start shell to the Pod.

$ kubectl exec -ti centos-85898cf6d8-dbz4r -- bash
kubectl exec -ti centos-85898cf6d8-dbz4r -- bash
[[email protected] /]# yum -y update
[[email protected] /]# exit
exit

Clean up.

$ kubectl delete deployment/centos
deployment.apps "centos" deleted

Building custom CentOS image

Sometimes it makes more sense to build a custom image with all set of troubleshooting tools you’ll use frequently.

Let’s create a Dockerfile.

$ cd ~
$ vim Dockerfile-centos

Here is my Dockerfile settings.

FROM centos:8
MAINTAINER Josphat Mutai <[email protected]>

# Update base image packages
RUN yum -y update && yum clean all

# Install my Custom packages
RUN yum -y install \
           telnet \
           wget \
           vim \
           bash-completion \
           tcpdump \
           traceroute \
           nmap-ncat

# Set default command
CMD ["/usr/bin/bash"]

Build your image with Docker or Podman CLI. I’ll install and use Podman on my CentOS 8 machine.

$ sudo yum -y install @container-tools

Create a container image.

$ podman build -t mycentos -f Dockerfile-centos

Confirm successful build:

$ podman  images
REPOSITORY                TAG     IMAGE ID      CREATED             SIZE
localhost/mycentos        latest  de2ac46ae71e  About a minute ago  704 MB
docker.io/library/centos  8       300e315adb2f  4 days ago          217 MB

Tag image and push to your image registry.

$ podman tag localhost/mycentos:latest docker.io/jmutai/mycentos:8
$ podman  images
REPOSITORY                 TAG     IMAGE ID      CREATED        SIZE
localhost/mycentos         latest  de2ac46ae71e  4 minutes ago  704 MB
docker.io/jmutai/mycentos  8       de2ac46ae71e  4 minutes ago  704 MB
docker.io/library/centos   8       300e315adb2f  4 days ago     217 MB

$ podman login docker.io
Username: jmutai
Password:
Login Succeeded!

$ podman push docker.io/jmutai/mycentos:8
Getting image source signatures
Copying blob 2653d992f4ef done
Copying blob d5ad0338072e done
Copying blob fecd9c7cda47 done
Copying config de2ac46ae7 done
Writing manifest to image destination
Storing signatures

We can then update our deployment to use our custom image.

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: centos
  name: centos
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: centos
    spec:
      containers:
      - image: docker.io/jmutai/mycentos:8
        command: ["/bin/sleep", "3650d"]
        name: centos
        resources: {}
status: {}

Update deployment.

$ kubectl apply -f centos-deployment.yaml
deployment.apps/centos created

Check if the Pod is running and you can access.

$ kubectl get deploy centos
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
centos   1/1     1            1           22s

$ kubectl get pod -l app=centos
NAME                      READY   STATUS    RESTARTS   AGE
centos-66fc46dbbb-fwwkt   1/1     Running   0          58s

$ kubectl exec -ti centos-66fc46dbbb-fwwkt -- bash
[[email protected] /]#

The tools we install during image build should be available for our use.

[[email protected] /]# tcpdump --version
tcpdump version 4.9.3
libpcap version 1.9.1 (with TPACKET_V3)
OpenSSL 1.1.1g FIPS  21 Apr 2020

You can now use the container to troubleshoot your microservices issues and clean the deployment once done.

$ kubectl delete -f centos-deployment.yaml
deployment.apps "centos" deleted

I hope this guide helped you to run CentOS container Pod in Kubernetes | OpenShift container orchestration environment.

More guides:

How To Check Running Kubernetes Components Version

Run Kubernetes on Linux Mint with Minikube

How To Run Rancher k3s Kubernetes in Docker Containers

LEAVE A REPLY

Please enter your comment!
Please enter your name here