Setup Kubernetes on Ubuntu 18.04 and deploy NGINX

  • by

This weekend’s project covers setting up a Kubernetes cluster on Ubuntu 18.04 and deploying the base NGINX image. First, let’s go over our setup.

VM Setup

K8 Master:
K8-M01- IP: 10.0.0.2 / Specs: 4 CPU, 4gb MEM / OS: Ubuntu 18.04

K8 Nodes:
K8-N01 – IP: 10.0.0.3 / Specs: 4 CPU, 4gb MEM / OS: Ubuntu 18.04
K8-N02 – IP: 10.0.0.4 / Specs: 4 CPU, 4gb MEM / OS: Ubuntu 18.04
K8-N03 – IP: 10.0.0.5 / Specs: 4 CPU, 4gb MEM / OS: Ubuntu 18.04

Perform on all master and node(s)

1. Update your package list:

# sudo apt-get update

2. Install docker.io and enable automatic startup:
# sudo apt-get install docker.io -y 
# sudo systemctl enable docker

3. Add K8 signing key:
# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

4. Add K8 repo:
# sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

5. Install the Kubernetes tools:
# sudo apt-get install kubeadm kubelet kubectl -y

6. Disable swap
# sudo swapoff -a

Perform on master:

7. Initiate Master (important: keep the pod-network-cidr the same as below for calico):

# sudo kubeadm init --pod-network-cidr=192.168.0.0/16

8. Once you receive a message indicating you are now ready to join workers – run the following to configure kubectl
# mkdir -p $HOME/.kube 
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 
# sudo chown $(id -u):$(id -g) $HOME/.kube/config

9. Before we join nodes, lets setup our network plugin (calico):
# sudo kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

10. Let’s check the status of the namespace and wait till the status is ready for all nodes:
# watch kubectl get pods --all-namespaces

11. We can confirm status again using:

# kubectl get nodes

12. After running kubeadm init, you should have also seen the command to join nodes to the cluster

You can now join any number of machines by running the following on each node as root:

kubeadm join 10.0.0.2:6443 --token rS3ZwbCh2ygy8DQW2hPm7 \ 
--discovery-token-ca-cert-hash sha256:48437914f4f871712f12df6022ea44b5b9e529d2b592a925767f6925d300b8ef

Perform on the node(s):

13. Use the join command provided in step 12 on each node.

# sudo kubeadm join 10.0.0.2:6443 --token rS3ZwbCh2ygy8DQW2hPm7 \
--discovery-token-ca-cert-hash sha256:48437914f4f871712f12df6022ea44b5b9e529d2b592a925767f6925d300b8ef

Check the status of your cluster on the master:

14. After joining the nodes to the cluster, you can then check your node status from your master:

# kubectl get nodes

Deploy NGINX image to your PODs

15. Deploy the basic NGINX image using deployments:

# kubectl create deployment nginx --image=nginx

Create Deployment

16.  Confirm the status of the deployment:

# kubectl get deployments

Deployment Results

17. Create a service to expose your NGINX cluster to the outside world:

# kubectl create service nodeport nginx --tcp=80:80

Service Results

18. You can now browse to your nodes using the port assigned in services and see the test page:

Test Page

At this point, you could then use a load balancer to forward to your nodes OR if you are doing this in the cloud you can leverage the LoadBalancer service type instead of NodePort