Install and Configure Kubernetes on Ubuntu/Debian and CentOS/RHEL

Posted on

Install and Configure Kubernetes on Ubuntu/Debian and CentOS/RHEL

Install and Configure Kubernetes on Ubuntu/Debian and CentOS/RHEL

Kubernetes has emerged as the leading open-source platform for container orchestration, streamlining the deployment, scaling, and overall management of containerized applications. This comprehensive guide provides a step-by-step walkthrough of installing Kubernetes on both Ubuntu/Debian and CentOS/RHEL operating systems. We’ll leverage essential tools like kubeadm, kubectl, and kubelet to establish a fully functional Kubernetes cluster.

setup and config Kubernetes on Ubuntu/Debian CentOS/RHEL install

Prerequisites

Before embarking on the installation journey, ensure the following prerequisites are met:

  • A minimum of two machines: one designated as the master node and one or more as worker nodes.
  • Each machine should have at least 2 GB of RAM and 2 CPUs.
  • A stable network connection between all machines.
  • Docker or another compatible container runtime installed on all machines.
  • Root privileges or sudo access on all machines.

Installing Kubernetes on Ubuntu/Debian

Step 1: Update Your System

Begin by updating your system packages to the latest versions:

$ sudo apt update && sudo apt upgrade -y

Step 2: Install Docker

Kubernetes relies on a container runtime to manage containers. We’ll use Docker in this guide. You can follow this article to learn how to install and use Docker on Ubuntu LTS. Once Docker is installed, return to this guide to continue with the Kubernetes installation process.

Step 3: Disable Swap

For optimal performance, Kubernetes requires swap to be disabled:

$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^/#/' /etc/fstab

Step 4: Install Kubernetes Components

Install the required Kubernetes components: kubelet, kubeadm, and kubectl:

$ sudo apt install -y kubelet kubeadm kubectl
$ sudo systemctl enable kubelet

Step 5: Initialize Kubernetes with kubeadm

Initialize Kubernetes on the master node using kubeadm. Replace <your-pod-network-cidr> with your desired Pod network CIDR (e.g., 192.168.0.0/16):

$ sudo kubeadm init --pod-network-cidr=<your-pod-network-cidr>

Step 6: Set Up Kubectl

Configure kubectl to interact with your Kubernetes cluster:

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

Step 7: Configure Pod Network

Install a Pod network add-on. We’ll use Calico in this example:

$ kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Step 8: Join Worker Nodes (Optional)

Join worker nodes to the cluster using the kubeadm join command displayed during master initialization. This command will look similar to this:

kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Installing Kubernetes on CentOS/RHEL

Step 1: Update Your System

Ensure your system is up-to-date:

$ sudo yum update -y

Step 2: Install Docker

Install Docker, the container runtime for Kubernetes:

$ sudo yum install docker -y
$ sudo systemctl enable docker
$ sudo systemctl start docker

Step 3: Disable Swap

Disable swap to improve Kubernetes performance:

$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^/#/' /etc/fstab

Step 4: Install Kubernetes Components

Install kubeadm, kubectl, and kubelet:

$ sudo tee /etc/yum.repos.d/kubernetes.repo <<EOF
[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
$ sudo yum install -y kubelet kubeadm kubectl
$ sudo systemctl enable kubelet

Step 5: Initialize Kubernetes with kubeadm

Initialize Kubernetes on the master node, specifying your Pod network CIDR:

$ sudo kubeadm init --pod-network-cidr=<your-pod-network-cidr>

Step 6: Set Up Kubectl

Configure kubectl for your user:

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

Step 7: Configure Pod Network

Deploy a Pod network add-on, such as Calico:

$ kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Step 8: Join Worker Nodes (Optional)

Add worker nodes to the cluster using the kubeadm join command obtained during master initialization. The command will be similar to what was shown for Ubuntu/Debian.

Alternative Solutions for Kubernetes Installation

While kubeadm offers a straightforward approach to Kubernetes installation, alternative solutions provide different levels of abstraction and flexibility. Here are two alternatives:

1. Using Minikube:

Minikube is a lightweight Kubernetes distribution designed for local development and testing. It creates a single-node Kubernetes cluster within a virtual machine on your local machine. This approach simplifies the installation process, especially for developers who want to experiment with Kubernetes without setting up a full-fledged cluster.

  • Explanation: Minikube abstracts away much of the complexity of manual Kubernetes installation. It handles the installation of the Kubernetes components, container runtime, and networking configuration. It’s ideal for quickly setting up a development environment.

  • Installation Example (Ubuntu/Debian):

    First install a hypervisor like KVM2 or VirtualBox. Then install minikube.

    curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    chmod +x minikube
    sudo mv minikube /usr/local/bin/
    
    minikube start --driver=kvm2

    This will download and install Minikube, start a Kubernetes cluster using KVM2 as the driver. You can then interact with the cluster using kubectl.

  • Advantages: Very easy to install and use, ideal for local development, requires minimal resources.

  • Disadvantages: Limited to a single node, not suitable for production environments.

2. Using Rancher Kubernetes Engine (RKE):

RKE is a CNCF-certified Kubernetes distribution that runs entirely within Docker containers. It allows you to provision Kubernetes clusters on bare metal, virtual machines, or cloud providers. RKE automates the installation and configuration of Kubernetes components, providing a more flexible and customizable solution compared to kubeadm.

  • Explanation: RKE uses a declarative configuration file to define the desired state of the Kubernetes cluster. It then uses Docker containers to deploy and manage the Kubernetes components. This approach allows for greater control over the cluster configuration and simplifies cluster upgrades.

  • Installation Example:

    1. Install Docker: Ensure Docker is installed on all nodes in the cluster.

    2. Download RKE CLI: Download the RKE CLI tool on your local machine.

      curl -O https://github.com/rancher/rke/releases/download/v1.4.6/rke_linux-amd64
      chmod +x rke_linux-amd64
      sudo mv rke_linux-amd64 /usr/local/bin/rke
    3. Create a Cluster Configuration File (cluster.yml):

      nodes:
        - address: <node1-ip>
          user: ubuntu
          role: [controlplane,worker,etcd]
        - address: <node2-ip>
          user: ubuntu
          role: [worker]
        - address: <node3-ip>
          user: ubuntu
          role: [worker]
      
      services:
        etcd:
          snapshot: true
          creation: 12h
          retention: 72h
      
      system_images:
        etcd: rancher/mirrored-coreos-etcd:v3.5.5

      Replace <node1-ip>, <node2-ip>, and <node3-ip> with the IP addresses of your nodes. Adjust the user and role settings as needed.

    4. Provision the Cluster:

      rke up --config cluster.yml

      RKE will then connect to the nodes and provision the Kubernetes cluster based on the configuration file.

  • Advantages: Flexible and customizable, supports various infrastructure providers, simplifies cluster upgrades, built to be secure.

  • Disadvantages: More complex than kubeadm or Minikube, requires a deeper understanding of Kubernetes concepts.

Conclusion

This guide has covered the installation of Kubernetes on Ubuntu/Debian and CentOS/RHEL using kubeadm. We have also explored two alternative solutions: Minikube and RKE. Choosing the right approach depends on your specific needs and experience level. kubeadm provides a solid foundation for learning and deploying Kubernetes, while Minikube is ideal for local development. RKE offers greater flexibility and control for production deployments. Remember to secure your cluster and explore Kubernetes features to maximize its potential.

Leave a Reply

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