Table of Contents
ToggleFor configuring Kubernetes Cluster, we are having 1 Master
and 3 Worker nodes.
Master Node: kb-master.kb.com
Worker Node1: worker1.kb.com
Worker Node2: worker2.kb.com
Worker Node3: worker3.kb.com
Step1: Set hostname of all servers and make entry in /etc/hosts
so that all servers can communicate each other via names.
root@kb-master:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 kb-master
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.64.230 kb-master kb-master.kb.com
192.168.64.231 worker1 worker1.kb.com
192.168.64.232 worker2 worker2.kb.com
192.168.64.233 worker3 worker3.kb.com
root@kb-master:~#
Step2: Disable firewall on all servers as it is basic
requirement of it.
root@kb-master:~# systemctl status ufw
● ufw.service - Uncomplicated firewall
Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: enabled)
Active: active (exited) since Sun 2023-12-31 07:48:51 UTC; 18min ago
Docs: man:ufw(8)
Main PID: 659 (code=exited, status=0/SUCCESS)
CPU: 5ms
Dec 31 07:48:51 kb-master systemd[1]: Starting Uncomplicated firewall...
Dec 31 07:48:51 kb-master systemd[1]: Finished Uncomplicated firewall.
root@kb-master:~# systemctl disable ufw
Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ufw
Removed /etc/systemd/system/multi-user.target.wants/ufw.service.
root@kb-master:~# systemctl stop ufw
root@kb-master:~#
Step3: Disable the swap on all servers and make it permanent
using /etc/fstab file.
root@kb-master:~# swapon -s
Filename Type Size Used Priority
/swap.img file 2875388 0 -2
root@kb-master:~# swapoff -a
root@kb-master:~# swapon -s
root@kb-master:~# vi /etc/fstab
root@kb-master:~# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-sh8bAuDGbOqZoj0uxGSm8hkNW05AufsSgaYK2fkssez0lhssM7LGT7s1Nd5RYSWa / ext4 defaults 0 1
# /boot was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/2b0ab1cf-259c-414c-ada0-56c303438032 /boot ext4 defaults 0 1
#/swap.img none swap sw 0 0
root@kb-master:~#
Step4: We need to load some kernel parameters on all nodes.
root@kb-master:~# tee /etc/modules-load.d/containerd.conf <<EOF
> overlay
br_netfilter
EOF
overlay
br_netfilter
root@kb-master:~# modprobe overlay
root@kb-master:~# modprobe br_netfilter
root@kb-master:~# 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
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
root@kb-master:~# sysctl --system
* Applying /etc/sysctl.d/10-console-messages.conf ...
kernel.printk = 4 4 1 7
* Applying /etc/sysctl.d/10-ipv6-privacy.conf ...
net.ipv6.conf.all.use_tempaddr = 2
net.ipv6.conf.default.use_tempaddr = 2
* Applying /etc/sysctl.d/10-kernel-hardening.conf ...
kernel.kptr_restrict = 1
* Applying /etc/sysctl.d/10-magic-sysrq.conf ...
kernel.sysrq = 176
* Applying /etc/sysctl.d/10-network-security.conf ...
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.all.rp_filter = 2
* Applying /etc/sysctl.d/10-ptrace.conf ...
kernel.yama.ptrace_scope = 1
* Applying /etc/sysctl.d/10-zeropage.conf ...
vm.mmap_min_addr = 65536
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.default.accept_source_route = 0
sysctl: setting key "net.ipv4.conf.all.accept_source_route": Invalid argument
net.ipv4.conf.default.promote_secondaries = 1
sysctl: setting key "net.ipv4.conf.all.promote_secondaries": Invalid argument
net.ipv4.ping_group_range = 0 2147483647
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_regular = 1
fs.protected_fifos = 1
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
kernel.pid_max = 4194304
* Applying /usr/lib/sysctl.d/99-protect-links.conf ...
fs.protected_fifos = 1
fs.protected_hardlinks = 1
fs.protected_regular = 2
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/kubernetes.conf ...
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
* Applying /etc/sysctl.conf ...
root@kb-master:~#
Step5: Install containerd on all hosts so that we'll have
container services on all hosts. Below are the list of
commands which will install required packages after
configuring repository.
# apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# apt update
# apt install -y containerd.io
# containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
# sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
Now start and enable its services.
# systemctl restart containerd
# systemctl enable containerd
Step6: Install kubernetes on all hosts.
# apt-get update
# apt-get install -y apt-transport-https ca-certificates curl
# curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.27/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.27/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# apt-get update
# apt-get install -y kubelet kubeadm kubectl
# apt-mark hold kubelet kubeadm kubectl
Step7: Initialize kubernetes cluster. This command will run
only on master node.
root@kb-master:~# kubeadm init --control-plane-endpoint=kb-master
I1231 11:14:34.111571 6092 version.go:256] remote version is much newer: v1.29.0; falling back to: stable-1.27
[init] Using Kubernetes version: v1.27.9
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
W1231 11:15:12.873770 6092 checks.go:835] detected that the sandbox image "registry.k8s.io/pause:3.6" of the container runtime is inconsistent with that used by kubeadm. It is recommended that using "registry.k8s.io/pause:3.9" as the CRI sandbox image.
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kb-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.64.230]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [kb-master localhost] and IPs [192.168.64.230 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [kb-master localhost] and IPs [192.168.64.230 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 11.504732 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node kb-master as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node kb-master as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: bagalk.tws1rc8r2d7q3lnq
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join kb-master:6443 --token bagalk.tws1rc8r2d7q3lnq \
--discovery-token-ca-cert-hash sha256:a0700d7e259a21c2c7627c81d1963edcc56700f77d918b5569fe3cd337a26bcb \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join kb-master:6443 --token bagalk.tws1rc8r2d7q3lnq \
--discovery-token-ca-cert-hash sha256:a0700d7e259a21c2c7627c81d1963edcc56700f77d918b5569fe3cd337a26bcb
root@kb-master:~#
Step8: You have successfully initialized the cluster and you
need to run commands which have been displayed above.
root@kb-master:~# mkdir -p $HOME/.kube
root@kb-master:~# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
root@kb-master:~# chown $(id -u):$(id -g) $HOME/.kube/config
root@kb-master:~# kubectl cluster-info
Kubernetes control plane is running at https://kb-master:6443
CoreDNS is running at https://kb-master:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
root@kb-master:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
kb-master NotReady control-plane 74s v1.27.9
root@kb-master:~#
Step9: Now your master node is ready and you can go ahead and
register worker nodes using the command output in Step7.
root@worker1:~# kubeadm join kb-master:6443 --token bagalk.tws1rc8r2d7q3lnq \
--discovery-token-ca-cert-hash sha256:a0700d7e259a21c2c7627c81d1963edcc56700f77d918b5569fe3cd337a26bcb
[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.
root@worker1:~#
Step10: You can see all worker nodes now.
root@kb-master:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
kb-master NotReady control-plane 2m51s v1.27.9
worker1 NotReady <none> 36s v1.27.9
worker2 NotReady <none> 7s v1.27.9
worker3 NotReady <none> 3s v1.27.9
root@kb-master:~#
Step11: Now you need a network plugin which will be used for
communication between pods and cluster. Below is the Calico
network plugin. You can download and it will create required
system.
root@kb-master:~# wget https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml
--2024-01-02 06:28:27-- https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.109.133, 185.199.108.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 252363 (246K) [text/plain]
Saving to: ‘calico.yaml’
calico.yaml 100%[============================================================================>] 246.45K 1.05MB/s in 0.2s
2024-01-02 06:28:30 (1.05 MB/s) - ‘calico.yaml’ saved [252363/252363]
root@kb-master:~#
root@kb-master:~# kubectl create -f calico.yaml
poddisruptionbudget.policy/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
serviceaccount/calico-node created
serviceaccount/calico-cni-plugin created
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgpfilters.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/caliconodestatuses.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipreservations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrole.rbac.authorization.k8s.io/calico-cni-plugin created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-cni-plugin created
daemonset.apps/calico-node created
deployment.apps/calico-kube-controllers created
root@kb-master:~#
root@kb-master:~# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-867bf4f5b5-xtpk7 1/1 Running 0 117s
calico-node-5zqpt 1/1 Running 0 117s
calico-node-6m274 1/1 Running 0 117s
calico-node-nxdl9 1/1 Running 0 117s
calico-node-pf2ql 1/1 Running 0 117s
coredns-5d78c9869d-ftmz8 1/1 Running 0 43h
coredns-5d78c9869d-l8pbb 1/1 Running 0 43h
etcd-kb-master 1/1 Running 0 43h
kube-apiserver-kb-master 1/1 Running 0 43h
kube-controller-manager-kb-master 1/1 Running 0 43h
kube-proxy-2w9vf 1/1 Running 0 43h
kube-proxy-7cwxq 1/1 Running 0 43h
kube-proxy-mdn5k 1/1 Running 0 43h
kube-proxy-n2vmb 1/1 Running 0 43h
kube-scheduler-kb-master 1/1 Running 1 (55m ago) 43h
root@kb-master:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
kb-master Ready control-plane 43h v1.27.9
worker1 Ready <none> 43h v1.27.9
worker2 Ready <none> 43h v1.27.9
worker3 Ready <none> 43h v1.27.9
root@kb-master:~#
Step12: Now go ahead and test your cluster installation if it
is working fine or not.
root@kb-master:~# kubectl create deployment nginx-test --image=nginx --replicas=2
deployment.test/nginx-test created
root@kb-master:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test-5c64488cdf-jk669 1/1 Running 0 11s
nginx-test-5c64488cdf-jxwv4 1/1 Running 0 11s
root@kb-master:~#