How To Run Rocky / AlmaLinux Container in Kubernetes

In this blog post we will be showing you how to run Rocky Linux or AlmaLinux container Pod in Kubernetes and OpenShift environment. Rocky and AlmaLinux are community-driven operating systems 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.

To run Rocky or AlmaLinux 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 the OS in a container is installation of admin tools e.g telnet or tcpdump tools for network connectivity and packet capturing tasks.

Let’s generate a Rocky or AlmaLinux Pod deployment YAML manifest file with kubectl.

# Rocky / AlmaLinux 9 OS creation
kubectl run rockylinux9 --image=rockylinux:9 --restart=Never --dry-run=client -o yaml > rocky9-pod.yaml

kubectl run almalinux9 --image=almalinux:9 --restart=Never --dry-run=client -o yaml > almalinux9-pod.yaml

# Rocky / AlmaLinux 8 OS creation
kubectl run almalinux8 --image=rockylinux:8 --restart=Never --dry-run=client -o yaml > rocky8-pod.yaml

kubectl run almalinux8 --image=almalinux:8 --restart=Never --dry-run=client -o yaml > almalinux8-pod.yam8

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

$ vim rocky9-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: rockylinux9
  name: rockylinux9
spec:
  containers:
  - image: rockylinux:9
    name: rockylinux9
    resources: {}
    command: ["sleep"]
    args: ["infinity"]
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

The command ["sleep"] with args ["infinity"]will ensure that the container runs indefinitely.

To create the Pod run the command:

$ kubectl apply -f rocky9-pod.yaml
pod/rockylinux9 created

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

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

Check if the Pod is running:

$ kubectl get pods rockylinux9
NAME          READY   STATUS    RESTARTS   AGE
rockylinux9   1/1     Running   0          31s

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

kubectl exec -ti rockylinux9 -- bash

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

[root@rockylinux9 /]# yum update
....
Transaction Summary
===================================================================================================================================================================================================================================================================================
Install   1 Package
Upgrade  72 Packages

Total download size: 41 M
Is this ok [y/N]: y

[root@rockylinux9 /]# yum -y install telnet vim tcpdump

When done

[root@rockylinux9 /]# exit
exit

Deleting the Pod.

kubectl delete pod <name>

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 container Pod in Kubernetes you need to generate deployment manifest file.

# Rocky Linux 9 image
kubectl create deploy rockylinux9 --image=rockylinux:9 --replicas=1 --dry-run=client -o yaml > rocky9-deployment.yaml

# Rocky Linux 8 image
kubectl create deploy rockylinux8 --image=rockylinux:8 --replicas=1 --dry-run=client -o yaml  > rocky8-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: rockylinux9
  name: rockylinux9
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rockylinux9
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: rockylinux9
    spec:
      containers:
      - image: rockylinux:9
        name: rockylinux
        resources: {}
        command: ["/bin/sleep", "infinity"]
status: {}

Create Pod by applying the deployment manifest file.

$ kubectl apply -f rocky9-deployment.yaml
deployment.apps/rockylinux9 created

Check deployment creation status:

$ kubectl get deploy rockylinux9
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
rockylinux9   1/1     1            1           16s

Confirm the Pod is running

$ kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
rockylinux9-794c5bb645-jrg2f   1/1     Running   0          46s

Start shell to the Pod.

$ kubectl exec -ti rockylinux9-794c5bb645-jrg2f -- bash
[root@rockylinux9-794c5bb645-jrg2f /]# dnf -y update
[root@rockylinux9-794c5bb645-nnkx8 /]# exit
exit

To delete the deployment run the following commands:

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

Building custom container 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.

mkdir -p ~/rocky9 && cd ~/rocky9
vim Dockerfile

Here is my Dockerfile settings.

FROM rockylinux:9
LABEL maintainer="Josphat Mutai <[email protected]>"

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

# Install my Custom packages
RUN dnf -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. For Podman install container-tools on RHEL based systems.

sudo dnf -y install @container-tools

Create a container image.

# Using Docker
docker build -t rocky9 .

# Using Podman
podman build -t rocky9 .

Confirm successful build:

$ docker  images
REPOSITORY                 TAG            IMAGE ID       CREATED              SIZE
rocky9                     latest         45cf515156ae   About a minute ago   383MB
ghcr.io/k3d-io/k3d-proxy   5.7.3          0475c00b0478   7 days ago           61.2MB
ghcr.io/k3d-io/k3d-tools   5.7.3          91ba69c868e8   7 days ago           20.8MB
rancher/k3s                v1.30.3-k3s1   86193a59fa57   8 days ago           209MB

Tag image and push to your image registry.

$ docker tag rocky9:latest docker.io/<yourusername>/rocky:9
$ docker images
REPOSITORY                 TAG            IMAGE ID       CREATED         SIZE
jmutai/rocky               9              45cf515156ae   3 minutes ago   383MB
rocky9                     latest         45cf515156ae   3 minutes ago   383MB
ghcr.io/k3d-io/k3d-proxy   5.7.3          0475c00b0478   7 days ago      61.2MB
ghcr.io/k3d-io/k3d-tools   5.7.3          91ba69c868e8   7 days ago      20.8MB
rancher/k3s                v1.30.3-k3s1   86193a59fa57   8 days ago      209MB

$ docker login docker.io
Username: <username>
Password:
Login Succeeded!

$ docker push docker.io/<username>/rocky:9
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: rockylinux9
  name: rockylinux9
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rockylinux9
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: rockylinux9
    spec:
      containers:
      - image: docker.io/<username>/rocky:9
        name: rockylinux
        resources: {}
        command: ["/bin/sleep", "infinity"]
status: {}

Update deployment.

$ kubectl apply -f rocky9-deployment.yaml
deployment.apps/rockylinux9 created

Check if the Pod is running and you can access.

$ kubectl get deploy rockylinux9
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
rockylinux9   1/1     1            1           6s

$ kubectl get pod -l app=rockylinux9
NAME                           READY   STATUS    RESTARTS   AGE
rockylinux9-794c5bb645-nnkx8   1/1     Running   0          25s

$ kubectl exec -ti rockylinux9-794c5bb645-nnkx8 -- bash
[root@rockylinux9-794c5bb645-nnkx8 /]#

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

[root@rockylinux9-794c5bb645-jrg2f /]# tcpdump --version
tcpdump version 4.99.0
libpcap version 1.10.0 (with TPACKET_V3)
OpenSSL 3.0.7 1 Nov 2022

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

$ kubectl delete -f rocky9-deployment.yaml
deployment.apps "rockylinux9" deleted

I hope this guide helped you to run Linux container based on Rocky or AlmaLinux Pod in Kubernetes / OpenShift Pod. We offer Kubernetes and OpenShift Consultancy services. Check more details in our website pages.

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

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

Related Post

The web browser is the key arsenal that we use to browse the internet. If you want to access information […]

In this guide,we will look at how you can install Java 11 on Oracle Linux 8. Java is a widely […]

The adoption of Containers and microservice architectures has been amazing and speedy in the past few years. Docker is widely […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.