rspatel031/k8s-cluster-setup
Step-by-step guide to set up a Kubernetes (K8s) cluster on Ubuntu, including configuring containerd, installing Kubernetes components, and deploying Weave Net as the network plugin. Perfect for developers and DevOps engineers looking to deploy containerized applications efficiently.
Kubernetes Cluster Setup
This guide provides a step-by-step process for setting up a Kubernetes cluster with:
- A Control Plane node.
- One or more Worker Nodes.
Two scripts are included:
control-plane.sh: For setting up the control plane.worker-node.sh: For setting up the worker nodes.
Both setups include detailed instructions and port configurations for various network add-ons like Calico, Weave, and Flannel.
Prerequisites
- You have at least two machines (VMs or physical) with Ubuntu installed (22.04 or later is recommended).
- You have root or sudo access to these machines.
- Networking is properly configured between the nodes.
- Swap is disabled on all nodes.
Installation Overview
Script 1: Setting Up the Control Plane
-
Download the
control-plane.shscript to your control plane node:wget https://raw.githubusercontent.com/rspatel031/k8s-cluster-setup/refs/heads/main/control-plane.sh
-
Make the script executable:
chmod +x control-plane.sh
-
Run the script:
sudo ./control-plane.sh
Output:
At the end of the script, you will receive a join command for worker nodes. This is stored at:
/tmp/kubeadm-init-output.txtScript 2: Setting Up Worker Nodes
Important: Ensure you set the hostname before executing the worker-node.sh script.
The worker-node.sh script prepares worker nodes and joins them to the cluster.
Steps:
-
Download the
worker-node.shscript to your worker node(s).wget https://raw.githubusercontent.com/rspatel031/k8s-cluster-setup/refs/heads/main/worker-node.sh
-
Make the script executable:
chmod +x worker-node.sh
-
Run the script:
sudo ./worker-node.sh
-
Once the script finishes, use the join command from the control plane node to connect the worker node to the cluster. For example:
sudo kubeadm join <control-plane-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Script Breakdown
Control Plane Script (control-plane.sh)
The control plane script:
- Updates and upgrades the system.
- Sets the hostname to
control-plane. - Disables swap for Kubernetes compatibility.
- Configures required kernel modules and
sysctlsettings. - Installs and configures
containerd. - Configures Kubernetes repositories.
- Installs Kubernetes components (
kubelet,kubeadm, andkubectl). - Initializes the Kubernetes control plane.
- Sets
crictlendpoints. - Deploys a default network add-on.
- Displays the
kubeadm initcommand output. - Adds
kubectlaliases and autocompletion for ease of use.
Worker Node Script (worker-node.sh)
The worker node script:
- Updates and upgrades the system.
- Disables swap.
- Configures required kernel modules and
sysctlsettings. - Installs and configures
containerd. - Configures Kubernetes repositories.
- Installs Kubernetes components (
kubeletandkubeadm).
Network Add-Ons
Kubernetes requires a network add-on to manage communication between pods. Below are the supported network add-ons with their corresponding configuration files:
-
Calico:
wget https://raw.githubusercontent.com/rspatel031/k8s-network-addon/refs/heads/main/calico/calico.yaml
-
Flannel:
wget https://raw.githubusercontent.com/rspatel031/k8s-network-addon/refs/heads/main/flannel/flannel.yaml
-
Weave:
wget https://raw.githubusercontent.com/rspatel031/k8s-network-addon/refs/heads/main/weave/weave.yaml
CIDR Configuration: All network add-ons mentioned above are preconfigured to use the 10.244.0.0/16 CIDR range.
Port Requirements
Below are the port details required for the cluster to function properly:
Kubernetes Components
| Component | Protocol | Ports | Description |
|---|---|---|---|
| Kube-API Server | TCP | 6443 | Kubernetes API server port. |
| etcd | TCP | 2379-2380 | Communication between etcd members. |
| Kubelet | TCP | 10250 | Worker node to API server communication. |
| Kube Scheduler | TCP | 10251 | Scheduler communication. |
| Kube Controller | TCP | 10252 | Controller-manager communication. |
Network Add-Ons
| Add-On | Protocol | Ports | Description |
|---|---|---|---|
| Calico | TCP/UDP | 179 | BGP communication between nodes. |
| Flannel | UDP | 8285 | Overlay network communication. |
| Flannel | UDP | 8472 | VXLAN communication. |
| Weave | TCP/UDP | 6783-6784 | Control plane and data plane traffic. |
Additional Notes
- Ensure ports are open and accessible between nodes in the cluster.
- Ensure you use the correct
kubeadm joincommand on worker nodes. - The scripts are written for Ubuntu and may require modifications for other distributions.
- The control-plane setup also deploys a Calico network add-on. You can customize the network plugin if needed.
- Restart nodes if necessary after installation.
- Use the provided configuration files to deploy any network add-on suitable for your environment.