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.
- 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.
-
Download the
control-plane.sh
script 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
At the end of the script, you will receive a join command for worker nodes. This is stored at:
/tmp/kubeadm-init-output.txt
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.
-
Download the
worker-node.sh
script 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>
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
sysctl
settings. - Installs and configures
containerd
. - Configures Kubernetes repositories.
- Installs Kubernetes components (
kubelet
,kubeadm
, andkubectl
). - Initializes the Kubernetes control plane.
- Sets
crictl
endpoints. - Deploys a default network add-on.
- Displays the
kubeadm init
command output. - Adds
kubectl
aliases and autocompletion for ease of use.
The worker node script:
- Updates and upgrades the system.
- Disables swap.
- Configures required kernel modules and
sysctl
settings. - Installs and configures
containerd
. - Configures Kubernetes repositories.
- Installs Kubernetes components (
kubelet
andkubeadm
).
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.
Below are the port details required for the cluster to function properly:
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. |
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. |
- Ensure ports are open and accessible between nodes in the cluster.
- Ensure you use the correct
kubeadm join
command 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.