Operating System CentOS

Getting Started with Kubernetes on AlmaLinux 9 and Rocky Linux 9

Learn the core Kubernetes objects and the kubectl commands to deploy, expose, scale, update and roll back an application on AlmaLinux 9 or Rocky Linux 9.

This is a first-steps guide to using Kubernetes on a server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9. You will learn the handful of objects you actually work with, connect kubectl to a cluster, deploy an application, expose it, scale it, update it and roll it back. The same commands work on any conformant cluster, whatever distribution the nodes run.

Older tutorials on this topic installed Kubernetes on CentOS 7 from distribution packages with a manually configured etcd, Docker and Flannel. CentOS 7 is end-of-life, those packages no longer exist, and replication controllers have been superseded by Deployments. Today a cluster is bootstrapped with kubeadm and a container runtime such as containerd; this guide assumes that part is done and concentrates on using the cluster.

What you need

  • A running Kubernetes cluster. For self-managed servers the standard route is kubeadm with containerd and a pod network add-on, as described in the official kubeadm cluster guide. Each node needs at least 2 GB of RAM, and the control-plane node at least 2 CPUs.
  • kubectl on the machine you work from, and a kubeconfig file for the cluster.

Single-node option

You do not need several servers to learn. A one-node kubeadm cluster works once you allow ordinary pods on the control-plane node by removing its taint:

kubectl taint nodes --all node-role.kubernetes.io/control-plane-

For a workstation, the Kubernetes project also lists minikube and kind as tools that run a local cluster; kind needs Docker or Podman.

The core objects

ObjectWhat it does
PodOne or more containers that share a network address and storage. The smallest unit Kubernetes schedules.
DeploymentKeeps a set number of identical pods running and replaces them gradually when you change the image or configuration.
ServiceA stable address and port in front of a changing set of pods, selected by label.
LabelA key/value tag on any object. Services and Deployments find their pods through labels.
NamespaceA named partition of the cluster for grouping and isolating objects.

You rarely create bare pods. You describe the desired state in a Deployment, and Kubernetes creates, restarts and reschedules pods to match it.

Connect kubectl to the cluster

On a kubeadm control-plane node, copy the admin kubeconfig into your user's home directory:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Enable shell completion (it needs the bash-completion package):

sudo dnf install -y bash-completion
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc

Check that the cluster answers:

kubectl get nodes -o wide
kubectl get pods --all-namespaces

Every node should report Ready. If you manage more than one cluster, kubectl config get-contexts lists them and kubectl config use-context NAME switches between them.

Run your first Deployment

Create a Deployment that runs the nginx web server:

kubectl create deployment web --image=nginx

Watch what was created:

kubectl get deployments
kubectl get pods -o wide
kubectl describe deployment web

The describe output ends with an event list. When something does not start, that list and kubectl describe pod POD_NAME are the first places to look.

Scale it

Ask for three replicas instead of one:

kubectl scale --replicas=3 deployment/web
kubectl get pods -o wide

On a multi-node cluster the pods are spread over the workers. Delete one pod with kubectl delete pod POD_NAME and list the pods again: the Deployment has already started a replacement.

Expose it with a Service

Pods get new IP addresses whenever they are recreated, so clients connect through a Service instead. A ClusterIP Service (the default type) is reachable only inside the cluster. A NodePort Service additionally opens the same port on every node, taken from the range 30000–32767 by default:

kubectl expose deployment web --type=NodePort --port=80
kubectl get services

The PORT(S) column shows a mapping such as 80:31234/TCP. Request that port on any node's address:

curl http://203.0.113.11:31234

If the request times out, open the NodePort range in the node's firewall (sudo firewall-cmd --permanent --add-port=30000-32767/tcp && sudo firewall-cmd --reload). To test without exposing anything, forward a local port to the Service instead:

kubectl port-forward svc/web 8080:80

Update and roll back

Change the image of the container named nginx in the Deployment. Kubernetes replaces the pods a few at a time, so the Service keeps answering:

kubectl set image deployment/web nginx=nginx:stable
kubectl rollout status deployment/web

If the new version misbehaves, look at the history and return to the previous revision:

kubectl rollout history deployment/web
kubectl rollout undo deployment/web

Describe the application in a file

Imperative commands are good for learning. For anything you want to keep, write the desired state to a YAML manifest, store it in version control and apply it. Save this as web.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:stable
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Apply it. Running the same command again after editing the file updates the live objects to match:

kubectl apply -f web.yaml

Inspect and troubleshoot

kubectl logs deploy/web
kubectl logs -f POD_NAME
kubectl exec --stdin --tty POD_NAME -- /bin/sh
kubectl get events --sort-by=.metadata.creationTimestamp
  • Pod stuck in Pending: no node can take it. kubectl describe pod names the reason, typically insufficient CPU or memory, or the control-plane taint on a single-node cluster.
  • ImagePullBackOff: the image name or tag is wrong, or the node cannot reach the registry.
  • CrashLoopBackOff: the container starts and exits. Read kubectl logs POD_NAME --previous.

Clean up

kubectl delete service web
kubectl delete deployment web

If you created the objects from the manifest, kubectl delete -f web.yaml removes both. The kubectl quick reference lists many more everyday commands.

Frequently asked questions

What happened to replication controllers?

They still exist in the API, but Deployments are what you use now. A Deployment manages ReplicaSets for you and adds rolling updates and rollback, which a replication controller needed manual steps for.

Do I need Docker on the nodes?

No. The kubelet talks to a CRI runtime such as containerd or CRI-O. Docker Engine works only through the separate cri-dockerd adapter. Images built with Docker run on any of these runtimes.

How do I give my application a normal port 80 or 443 address?

NodePort is meant for simple access and testing. On your own servers the usual next step is an ingress controller or Gateway API implementation that listens on 80 and 443 and routes by hostname to ClusterIP Services. A LoadBalancer Service needs a load-balancer integration, which a bare kubeadm cluster does not include.

Is a single-node cluster good enough for production?

It runs workloads the same way, but if that one server fails, both the control plane and every application go down. Use it for learning, development and small internal tools where that is acceptable.

Share story:

Continue reading other guides on How VPS.