Kubernetes is one of the most powerful methods of deploying clusters for the management and deployment of containers. NGINX is one of the most popular web servers on the planet, and also one of the best ways to illustrate how to deploy a container. Combine the two and you can enjoy a highly scalable web server, ready to help your business grow.

But how do you deploy that NGINX container on a Kubernetes cluster? I’m going to show you. A word of warning, I’m using an Antsle cloud server, which makes deploying the Kubernetes platform incredibly simple. The operating system hosting Kubernetes is Ubuntu Server 16.04. I will assume you already have Kubernetes up and running. For those that don’t have an Antsle, and need to first install Kubernetes, check out how to here. For this demonstration, I’ll be deploying on three virtual machines:

  • kubernetes at 192.168.1.190
  • kubernetes2 at 192.168.1.191
  • kubernetes3 at 192.168.1.192

The machine with hostname kubernetes will serve as my master, while kubernetes2/3 will serve as nodes.

With that out of the way, let’s deploy.

Setting up hostnames

The first thing we have to do is map out hostnames on each machine. So for each machine, we’ll issue the command sudo nano /etc/hosts and map the other machine’s IP address to hostname. So on kubernetes, my hosts additions will be:

192.168.1.191 kubernetes2
192.168.1.192 kubernetes3

On kubernetes2, the additions will be:

192.168.1.190 kubernetes
192.168.1.192 kubernetes3

On kubernetes3, the additions will be:

192.168.1.190 kubernetes
192.168.1.191 kubernetes2

Once you’ve made the additions, save and close the file. Make sure you can ping each server, via hostname.

Initialize the master node

With everything in place, it’s time to initialize the master node. Log into kubernetes (my master node) and issue the command:

sudo kubeadm init --pod-network-cidr=192.168.1.0/16 --apiserver-advertise-address=192.168.1.190

This command can take a minute or two to complete, as the necessary images might have to be pulled. Once it completes, you should see similar output shown in Figure A.

Figure A

Included with the output will be your token and discovery token. Make sure you copy those down (or just copy the entire join command), as you’ll need that information to join the nodes to the cluster.

The next step is clearly outlined in the output of the initialization command. Effectively, you must issue the following commands:

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

Once you’ve issued the above commands, check on the status of the nodes (there’ll only be one at this point), with the command:

kubectl get nodes

You should see the master node listed (Figure B).

Figure B

The reason our Master Node is listed as not ready is because it has yet to have a Container Networking Interface (CNI). Let’s deploy a Calico CNI for the master with the command:

sudo kubectl apply -f https://docs.projectcalico.org/v2.6/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml

Let’s make sure Calico was deployed correctly with the command kubectl get pods –all-namespaces.

The output of the above command (Figure C) should show Calico running.

Figure C

Run kubectl get nodes again, and you should see the Master Node is now listed as Ready.

Adding nodes to the cluster

Next we head over to our nodes to add them to the cluster. Remember the join command in the output from the Master Node initialization command? Head over to kubernetes2 and issue that command, which will look something like this:

sudo kubeadm join --token 6779e1.164c5515cf412fdf 192.168.1.190:6443 --discovery-token-ca-cert-hash sha256:c3e413050e40675280bbf8e37a99c53a1481f82d714469b51b77ed17b38015de

Once that command completes, do the same on kubernetes3. After you’ve issued the join command on your nodes, go back to the Master Node and issue the command kubectl get nodes and you should see all nodes ready (Figure D).

Figure D

Deploy the NGINX container to the cluster

It’s now time to deploy the NGINX container. From the master node, issue the command:

sudo kubectl create deployment nginx --image=nginx

Next we make the NGINX container available to the network with the command:

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

Issue the command kubectl get svc to see your NGINX listing (as well as the assigned port, given by Kubernetes – Figure E)

Figure E

Let’s test this with the command:

curl kubernetes3:30655

NOTE: The 30655 port was assigned during the create service command. It will be unique to your deployment.

The output of the curl command should display the HTML of the NGINX index.html page. If you see that, congratulations, your NGINX container has been deployed on your Kubernetes cluster. If you point a web browser to http://IP_OF_NODE:ASSIGNED_PORT (Where IP_OF_NODE is an IP address of one of your nodes and ASSIGNED_PORT is the port assigned during the create service command), you should see the NGINX Welcome page!

Basic deployment

What we’ve done is a very basic Kubernetes deployment of NGINX on a cluster. There is so much more to learn about using Kubernetes. This, however, should give you a good start as well as help you easily deploy NGINX on your Kubernetes cluster.

Subscribe to the Data Insider Newsletter

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more. Delivered Mondays and Thursdays

Subscribe to the Data Insider Newsletter

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more. Delivered Mondays and Thursdays