Kubernetes: Master and Node Configuration

Creating Instances:

To kick things off, let's create two instances – one for the master and one for the node. Use the t2.medium instance type and Ubuntu as the base image.

SSH Connection:

Now, let's establish an SSH connection. Whether you prefer using Putty or launching the instance directly, connect to both the master and node using your terminal.

Kubernetes Setup:

Moving on to the Kubernetes setup, execute the following commands on both the master and node, one after the other:

#Here we are installing the docker engine
sudo apt update
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo apt install docker.io -y

Adding the GPG keys and enabling Docker

sudo systemctl enable --now docker # enable and start in single command.

# Adding GPG keys.
curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg

Master

Just to check whether the command ran properly execute the below

sudo service docker status

Now adding and source list and installing kubeadm

# Add the repository to the sourcelist.
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt update 
sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

Master Node Initialization:

Initialize the Kubernetes master node:

sudo kubeadm init

After successful execution, your Kubernetes control plane will be initialized. Set up local kubeconfig for both the root user and a normal user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Apply Weave network for master to communicate with nodes:

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Worker Node Configuration:

Run the following commands on the worker node.

Run the following command on node to perform pre-flight checks and reset Kubernetes configurations

sudo kubeadm reset pre-flight checks

To generate the join command token, execute the following on your Kubernetes master node, This will provide you with the command needed for joining other nodes to the cluster. Ensure to run this on the master node and follow the displayed instructions on your worker nodes.

sudo kubeadm token create --print-join-command

Paste the join command you got from the master node and append --v=5 at the end. Make sure either you are working as sudo user or use sudo before the command

sudo kubeadm join 172.31.12.239:6443 --token iwuxnk.wbhzjrhqd226a2l4     --discovery-token-ca-cert-hash sha256:16b427ac6a78c173405351e5f7a25e91735265eabb386a7c7e1bdc0f44bef7ad --v=5

As you may see it is trying to connect but since the port 6443 is not exposed we need to expose the port

Expose Port on AWS:

Now the below operation we are going to perform on AWS on the Master instance

Now execute the below on the Master

kubectl get nodes

Test a demo Pod

If you want to test a demo pod, you can use the following command: Execute on Master

kubectl run hello-world-pod --image=busybox --restart=Never --command -- sh -c "echo 'Hello, World' && sleep 3600"

Now goto Node and excute docker ps it should resemble the below

This should provide you with the expected results.

Conclusion:

You've successfully completed the Kubernetes setup journey! With instances for the master and node configured using t2.medium and Ubuntu, and SSH connections established, you've set the stage for a powerful Kubernetes cluster.

Consider this just the start of your Kubernetes adventure—may your orchestrations be smooth, your containers happy, and your tech endeavors full of joy!