Kubernetes practice environment setup on AWS EC2 with kubeadm

This is a simple step by step instructions to set up a practice environment to play with Kubernetes. We will use two EC2 instances during this setup.

1. Create two EC2 instances
Create two m5.large instances with Ubuntu 18.04 image. One will be used as the master node and the other as a worker node. Why m5.large? because it matched with the minimum requirement to setup a Kubernetes cluster with kubeadm.

2. Master node configuration
Choose one of the previously created EC2 instance as the master node. It is good to tag it as the master node (Ex: k8s-master) so that it will be easy to identify later. Log in to the selected EC2 instance using a SSH client and execute the following commands.

1. sudo sawpoff -a : Swap must be disabled for kubelet to work properly.
2. sudo apt-get update && sudo apt-get upgrade -y : Do the update.
3. sudo apt-get install -y docker.io: Install Docker as CRI(Container Runtime Engine) Note: Kubernetes is deprecating Docker as a container runtime after v1.20.More details here.
4. Install kubeadm, kubelet, kubeclt using following commands

1. sudo sh -c "echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list"2. sudo sh -c "curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -"3. sudo apt-get update4. sudo apt-get install -y kubeadm=1.19.0-00 kubelet=1.19.0-00 kubectl=1.19.0-005. sudo apt-mark hold kubelet kubeadm kubectl6. sudo kubeadm init --kubernetes-version 1.19.0 --pod-network-cidr 192.168.0.0/167. mkdir -p $HOME/.kube8. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config9. sudo chown $(id -u):$(id -g) $HOME/.kube/config

Note: After the kubeadm init command you will see the kubeadm join command. Keep that command saved, as it is needed to execute in worker node.

5. Configure network plugin
Kubernetes is not responsible for networking between Pods, we have to use a CNI plugin for that. Calico plugin will be used for it. Here is a nice post about it. Run below command to setup Calico.

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Master node should be ready after these steps.

2. Worker node configuration
1. sudo swapoff -a : Swap should be disabled
2. sudo apt-get update && sudo apt-get upgrade -y: Do the update
3. sudo apt-get install -y docker.io: Use Docker as CRI
4. Install kubeadm, kubelet, kubectl on worker node as well

1. sudo sh -c "echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list"2. sudo sh -c "curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -"
3. sudo apt-get update
4. sudo apt-get install -y kubeadm=1.19.0-00 kubelet=1.19.0-00 kubectl=1.19.0-005. sudo apt-mark hold kubelet kubeadm kubectl

After this run the kubeadm join command which you saved during the master node setup.(Step 4 in master node setup, after kubeadm init command. 6th command). With this worker should be join with master node. Still we have to do bit more configuration on master node.

3. Additional configuration on master node
By default master node does not allow to host containers, since there is only one worker node we can enable master node to have containers as well. Loging to master node gain using a ssh client and issue following command.

kubectl taint nodes --all node-role.kubernetes.io/master-

Now you should have two node cluster to play with k8s.
kubectl get pods should list the nodes in cluster.