How to Install Kubernetes on Rocky Linux 9 | AlmaLinux 9
Kubernetes is the leading platform for container orchestration, enabling automated deployment, scaling, and management of containerized applications. Rocky Linux 9 and AlmaLinux 9, as successors to CentOS, provide a stable and secure environment ideal for running Kubernetes clusters. This guide will walk you through the process of installing Kubernetes on Rocky Linux 9 or AlmaLinux 9, ensuring you have a reliable setup for your containerized workloads.
Table of Contents
- Introduction
- Prerequisites
- Setting Up the Environment
- Installing Kubernetes
- Configuring the Cluster
- Deploying a Sample Application
- Cluster Management Tips
- Troubleshooting Common Issues
- Conclusion
1. Introduction
Rocky Linux and AlmaLinux are community-driven distributions that continue the legacy of CentOS, offering enterprise-grade reliability and compatibility with Red Hat Enterprise Linux (RHEL). Kubernetes, with its robust container orchestration capabilities, perfectly complements these distributions, providing a scalable and efficient platform for modern applications.
2. Prerequisites
Before you start, ensure the following:
- A fresh installation of Rocky Linux 9 or AlmaLinux 9 on at least one server.
- A minimum of 2GB RAM and 2 CPU cores per node.
- Root or sudo access to the server.
- Basic knowledge of Linux command-line operations.
3. Setting Up the Environment
3.1 Update Your System
Begin by updating your system to ensure all packages are up-to-date:
sudo dnf update -y sudo dnf upgrade -y
3.2 Disable Swap
Kubernetes requires swap to be disabled. Edit the /etc/fstab
file to disable swap permanently:
sudo sed -i '/ swap / s/^/#/' /etc/fstab sudo swapoff -a
3.3 Install Required Packages
Install necessary packages for Kubernetes:
sudo dnf install -y yum-utils device-mapper-persistent-data lvm2
4. Installing Kubernetes
4.1 Add Kubernetes Repository
Add the Kubernetes repository to your system:
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF
4.2 Install Kubernetes Components
Install kubeadm
, kubelet
, and kubectl
:
sudo dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
Enable and start the kubelet
service:
sudo systemctl enable --now kubelet
5. Configuring the Cluster
5.1 Initialize the Kubernetes Master Node
On the master node, initialize the cluster:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
This command will output several lines of information, including the kubeadm join
command. Save this output for later use.
5.2 Configure kubectl for the Master Node
To start using the cluster, set up the kubectl
configuration:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
5.3 Install a Pod Network Add-on
Install the Flannel pod network add-on:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
6. Deploying a Sample Application
6.1 Create a Deployment
Deploy a simple Nginx application to verify your Kubernetes cluster is working correctly:
kubectl create deployment nginx --image=nginx
6.2 Expose the Deployment
Expose the Nginx deployment to make it accessible:
kubectl expose deployment nginx --port=80 --type=NodePort
6.3 Verify the Deployment
Check the status of your pods and services:
kubectl get pods kubectl get services
You should see your Nginx pod running and a service with a NodePort.
7. Cluster Management Tips
7.1 Scaling Applications
Scale your applications as needed:
kubectl scale deployment nginx --replicas=3
7.2 Upgrading Kubernetes
To upgrade Kubernetes components, use:
sudo dnf upgrade -y kubeadm kubelet kubectl --disableexcludes=kubernetes sudo kubeadm upgrade plan sudo kubeadm upgrade apply <version>
7.3 Backup and Restore
Regularly back up your Kubernetes data directory:
sudo tar -czvf /backup/kubernetes_backup.tar.gz /var/lib/etcd
Restore from backup by replacing the /var/lib/etcd
directory with your backup.
8. Troubleshooting Common Issues
8.1 Check Logs
If you encounter issues, check the logs for more details:
sudo journalctl -u kubelet -f kubectl logs <pod-name>
8.2 Network Issues
Ensure firewall settings allow necessary ports and network configurations are correct.
8.3 Node Issues
If nodes are not showing as Ready
, ensure the kubelet
service is running and check node logs:
sudo systemctl status kubelet
9. Conclusion
By following this guide, you have successfully installed Kubernetes on Rocky Linux 9 or AlmaLinux 9. You've set up a robust environment for managing containerized applications, deployed a sample application, and learned essential management techniques. Kubernetes, combined with the stability of Rocky Linux and AlmaLinux, provides a powerful platform for modern application development and deployment.
For further reading and advanced configurations, refer to the official Kubernetes documentation and the Rocky Linux documentation or AlmaLinux documentation.
References:
By following these steps, you've set up a Kubernetes cluster on Rocky Linux 9 or AlmaLinux 9, enabling you to leverage the power of container orchestration in a stable and secure environment. Whether you're deploying applications for development or production, this guide provides a solid foundation for your Kubernetes journey.