To setup a Kubernetes cluster, here are some essential notes to consider:
Prerequisites:
Ensure you have a compatible operating system (Linux is preferred).
Install necessary tools such as kubectl, kubeadm, and kubelet.
Verify that your system meets the minimum hardware requirements.
Cluster Architecture:
Understand the components of a Kubernetes cluster, including the Master Node and Worker Nodes.
Familiarize yourself with the roles of etcd, API server, controller manager, and scheduler.
Networking:
Choose a suitable networking solution (e.g., Calico, Flannel) to manage pod communication.
Configure the network settings to allow for proper communication between nodes.
Installation Steps:
Initialize the control plane on the Master Node using kubeadm init.
Set up a pod network add-on to enable networking capabilities.
Join Worker Nodes to the cluster using the command provided by kubeadm.
Verification:
Use kubectl get nodes to confirm that all nodes are successfully joined and in a Ready state.
Check the status of the pods with kubectl get pods –all-namespaces.
Best Practices:
Regularly update your Kubernetes components to the latest stable versions.
Implement RBAC (Role-Based Access Control) for security.
Monitor cluster performance and resource usage.
Below are the installation steps for Kubernetes cluster master and worker nodes.
Master Node Configuration:
To set the hostname for your master node, execute the following command:
sudo hostnamectl set-hostname "kubemaster01.abcxyz.com"
exec bash
Worker Nodes Configuration:
To set the hostname for the worker nodes, execute the following commands:
sudo hostnamectl set-hostname "kubenode01.abcxyz.com" # 1st worker node
sudo hostnamectl set-hostname "kubenode03.abcxyz.com" # 2nd worker node
exec bash
Make sure to replace the placeholders with the appropriate node names as needed.
===
To configure each node, please add the following lines to the /etc/hosts file:
192.168.0.26 [kubemaster01.abcxyz.com](http://kubemaster01.abcxyz.com) kubemaster01
192.168.0.25 [kubenode01.abcxyz.com](http://kubenode01.abcxyz.com) kubenode01
192.168.0.28 [kubenode03.abcxyz
.com](http://kubenode03.abcxyz.com) kubenode03
This setup ensures that each node can correctly resolve the specified hostnames.
===
Run the following commands on all nodes:
Disable Swap and Add Kernel Parameters
Execute the commands:sudo swapoff -a sudo sed -i '/ swap / s/^\\(.*\\)$/#\\1/g' /etc/fstab
Load the Required Kernel Modules
Ensure that you load the specified kernel modules on all nodes.
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
$ sudo tee /etc/sysctl.d/kubernetes.conf <<EOT
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOT
Reload the changes mentioned above by executing the following command:
sudo sysctl --system
======
Install Containerd Runtime
We will be utilizing the containerd runtime for our Kubernetes cluster. To begin the installation of containerd, first, install its dependencies:
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
Enable Docker Repository
Next, enable the Docker repository by running the following commands:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] <custom data-type="smartlink" data-id="id-0">https://download.docker.com/linux/ubuntu</custom> $(lsb_release -cs) stable"
Install Containerd
Now, execute the following commands to install containerd:
sudo apt update
sudo apt install -y <custom data-type="smartlink" data-id="id-1">http://containerd.io</custom>
Configure Containerd
Configure containerd to start using systemd as the cgroup:
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \\= false/SystemdCgroup \\= true/g' /etc/containerd/config.toml
Finally, complete the installation and start the containerd service:
sudo apt-get update
sudo apt-get install -y containerd
sudo systemctl start containerd
sudo systemctl enable containerd
=====================
Add Apt Repository for Kubernetes
To add the apt repository for Kubernetes, execute:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
=========
Install Kubectl, Kubeadm, and Kubelet on All Nodes
Run the following commands to install kubectl, kubeadm, and kubelet on all nodes:
$ sudo apt update
$ sudo apt install -y kubelet kubeadm kubectl
$ sudo apt-mark hold kubelet kubeadm kubectl
kubectl will be used for CLI commands.
kubeadm will be used for Kubernetes.
kubelet acts as the agent for worker nodes to communicate with the master.
=========
Initialize the Kubernetes Cluster
Now, we are ready to initialize the Kubernetes cluster. Run the following kubeadm command on the master node only:
sudo kubeadm init --control-plane-endpoint=[kubemaster01.abcxyz
.com](http://kubemaster01.abcxyz.com)
===========
Configure Access to the Cluster
To start interacting with the cluster, execute the following commands on the master node:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You can now run the following kubectl commands to view the cluster and node status:
kubectl cluster-info
kubectl get nodes
===============
Install Calico Network Plugin
A network plugin is essential for enabling communication between pods in the cluster. To install the Calico network plugin from the master node, run:
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.0/manifests/calico.yaml
Finally, verify the status of the pods in the kube-system namespace:
kubectl get pods -n kube-system
====================
# Test Your Kubernetes Cluster Installation
To verify your Kubernetes cluster installation, follow these steps:
1. **Deploy an NGINX-based application** and attempt to access it:
```bash
kubectl create deployment nginx-app --image=nginx --replicas=2
Check the status of the NGINX application deployment:
kubectl get deployment nginx-app
Expose the deployment as a NodePort:
kubectl expose deployment nginx-app --type=NodePort --port=80
Run the following commands to view the service status:
kubectl get svc nginx-app kubectl describe svc nginx-app
Connecting to the Worker Nodes
To start using your cluster, execute the following commands as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
Next, you should deploy a pod network to the cluster. Execute the following command with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
To join any number of control-plane nodes, copy the certificate authorities and service account keys on each node, then run the following command as root:
kubeadm join [kubemaster01.abcxyz.com:6443](http://kubemaster01.abcxyz.com:6443) --token 7c7581.yu5oehlvyrsh8vlz \\
--discovery-token-ca-cert-hash sha256:ab86309ee7e81976c23ca7be305d7788d180e2b6f8553052b2104733b277ed8c \\
--control-plane
To join any number of worker nodes, run the following command on each as root:
kubeadm join [kubemaster01.abcxyz.com:6443](http://kubemaster01.abcxyz.com:6443) --token 7c7581.yu5oehlvyrsh8vlz \\
--discovery-token-ca-cert-hash sha256:ab86309ee7e81976c23ca7be305d7788d180e2b6f8553052b2104733b277ed8c
Output after Joining
After executing the join command, you should see output similar to the following:
root@kubenode01:/home/hcloud# kubeadm join [kubemaster01.abcxyz.com:6443](http://kubemaster01.abcxyz.com:6443) --token 7c7581.yu5oehlvyrsh8vlz \\
--discovery-token-ca-cert-hash sha256:ab86309ee7e81976c23ca7be305d7788d180e2b6f8553052b2104733b277ed8c
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has successfully joined the cluster:
A certificate signing request was sent to the API server, and a response was received.
The Kubelet was informed of the new secure connection details.
Run kubectl get nodes on the control-plane to see this node join the cluster.