Kubernetes
Kubernetes is an open-source orchestration framework for software containers. Containers(Docker) are a way to package and run code that’s more efficient than virtual machines. Kubernetes provides the tools you need to run containerized applications in production and at scale.
Installing Kubernetes CLI tools:
- Installing the kubernetes command line tools
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
-
Disabling Swap:
sudo swapoff -a
-
Initiating a master node on one of the servers by ignoring swap.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Note: For creating cluster with public network:
#While doing kubeadm init add the PUBLIC IP and PORT as part of --control-plane-endpoint parameter.
sudo kubeadm init --control-plane-endpoint "PUBLIC_IP:PORT"
- Make sure no other K8s services are online(they might use the ports you are using which would end up causing problems at pod network initiation or joining pods.
- In our case, MicroK8s are active, so we stop it using
sudo microk8s.stop
- Error: kubeadm init shows kubelet isn’t running or healthy
FIX: https://stackoverflow.com/questions/52119985/kubeadm-init-shows-kubelet-isnt-running-or-healthy
create a file :
/etc/docker/daemon.json`
Content:
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
{
"dns": ["8.8.8.8", "8.8.4.4"]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl restart kubelet
- On successful initiation of master node, join the other worker nodes to master using the token generated at master node:
In my case, the generated token is:
kubeadm join 192.168.0.112:6443 --token 4bh1s5.1r9y4yd01hpaetlp --discovery-token-ca-cert-hash sha256:e89e942e1d9dc5463045c7231dd3d01d026abde4bf953534ee182632ee62f39a
Note: You can also generate token and print the join command:
kubeadm token create --print-join-command
- Check whether the worker node joined the master node,
for development purpose only
, we consider the master node to run pods too.
run the following command to taint the master and run pods in it.
kubectl taint nodes <node_name> node-role.kubernetes.io/master-
- On Master:
kubectl get nodes
It should list the nodes connected to master like this:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
server1 NotReady <none> 26s v1.22.4
server2 NotReady control-plane,master 5m52s v1.23.0
- Installing the network plugin so the master can create network bridges to communicate with pods.
Here, We can use Calico or Flannel network plugin from
curl https://docs.projectcalico.org/manifests/calico.yaml -O
kubectl apply -f calico.yaml
OR
curl https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -O
kubectl apply -f kube-flannel.yml
- Note: If the network plugin and CoreDNS are stuck at creating State, add the folllowing lines to /etc/resolv.conf
sudo nano /etc/resolve.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
Reference: https://github.com/coredns/coredns/issues/3681#issuecomment-592997864
- check the nodes status now to make sure they are in ready state
kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION
server1 Ready <none> 21m v1.22.4
server2 Ready control-plane,master 26m v1.23.0
- check the health of kube-cluster with
kubectl cluster-info
- Installing a Metrics server
kubectl apply -f https://raw.githubusercontent.com/scriptcamp/kubeadm-scripts/main/manifests/metrics-server.yaml