K8-Bootstrap the cluster using kubeadm

Thanks for watching this blog, before proceeding the below steps hope you are successfully able to install VM on virtual machine using vagrant and able to ssh as well.

if you request you please revisit the first article called K8-cluster setup

So, Once you are able to login to each nodes using vagrant ssh <hostname>

Initialize the nodes.

The following steps must be performed on each of the three nodes, so ssh to kubemaster and run the steps, then to kubenode01, then to kubenode02

1. Configure kernel parameters

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# Apply sysctl params without reboot
sudo sysctl --system

2. Verify that the br_netfilteroverlay modules are loaded by running the following commands

lsmod | grep br_netfilter
lsmod | grep overlay

3. Verify that the net.bridge.bridge-nf-call-iptablesnet.bridge.bridge-nf-call-ip6tables, and net.ipv4.ip_forward system variables are set to 1 in your sysctl config by running the following command:

sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward



4. Install Contrainerd container driver and other tools to bring the k8 components.


# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

5. Verify the containerd status using below command.

systemctl status containerd


6. Now to support the cgroup with containerd, changes the below value in containerd
config file.

vagrant@kubenode02:~$ sudo vim /etc/containerd/config.toml
vagrant@kubenode02:~$
vagrant@kubenode02:~$ sudo systemctl restart containerd
vagrant@kubenode02:~$
vagrant@kubenode01:~$ sudo cat /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
    SystemdCgroup = true


7. Install kubeadm, kubelet and kubectl

sudo apt-get update
# apt-transport-https may be a dummy package; if so, you can skip that package
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

# This overwrites any existing configuration in /etc/apt/sources.list.d/kubernetes.list
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl


8. Initialize Kubernetes cluster using kubeadm, where you have run the below command only on master node.

sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.56.2

9. The above command will initialize the master cluster and give command to run on
worker nodes to add them into the clusters.

[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following 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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.56.2:6443 --token tr9m34.qm5zwwsl0ewxi7ko \
        --discovery-token-ca-cert-hash sha256:ecb5e35f96daad37ee6aadec70e84ba3a806f008678d398369bf9b9c36ff96f4
vagrant@kubemaster:~$
vagrant@kubemaster:~$

vagrant@kubenode01:~$
vagrant@kubenode01:~$ sudo kubeadm join 192.168.56.2:6443 --token tr9m34.qm5zwwsl0ewxi7ko --discovery-token-ca-cert-hash sha256:ecb5e35f96daad37ee6a
adec70e84ba3a806f008678d398369bf9b9c36ff96f4
[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 joined the cluster: * Certificate signing request was sent to apiserver 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.


vagrant@kubenode02:~$ vagrant@kubenode02:~$ sudo kubeadm join 192.168.56.2:6443 --token tr9m34.qm5zwwsl0ewxi7ko --discovery-token-ca-cert-hash sha256:ecb5e35f96daad37ee6aadec70e84ba3a806f008678d398369bf9b9c36ff96f4 [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 joined the cluster: * Certificate signing request was sent to apiserver 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.


Reference:
  • https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
  • https://docs.docker.com/engine/install/ubuntu/
  • https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd
  • https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
  • https://www.weave.works/docs/net/latest/kubernetes/kube-addon/