Modal Title
Cloud Native Ecosystem / Kubernetes

Kubernetes App Deployment from the Command Line

Here is how to deploy a NGINX-based application directly from the Kubernetes command line.
Jan 14th, 2023 6:00am by
Featued image for: Kubernetes App Deployment from the Command Line

If you’ve been following my Kubernetes 101 series, you may have discovered that there are ways to make Kubernetes considerably easier. Thanks to MicroK8s and Portainer, the journey doesn’t have to be challenging all the time. But even though you can begin your dive into Kubernetes as a point-and-click affair with Portainer, at some point you might want to be able to work from the command line. That’s not a given but you never know if you’ll be thrown into a situation where you’re asked to deploy an app or service to a Kubernetes cluster and there is no Portainer GUI to be found.

Should such a situation arise, you’ll be glad you know how to take control of the command line interface and get the job done.

That’s exactly what we’re going to do here. We’ll run through a simple NGINX deployment and do it all from the command line.

Requirements

To follow along, you’ll need a running Kubernetes cluster, which is very easy to deploy with the help of MicroK8s. You can find out how to install MicroK8s here and then how to add nodes to the cluster here. Now, because we’re demonstrating with a MicroK8s version of Kubernetes, the commands will be a bit different because they’ll use microk8s along with kubectl.

That’s all you need. Let’s get to the deployment.

How to Create Your First Deployment

The first thing we’ll do is log in to the Kubernetes controller node. Once logged in, verify your nodes are all still connected with the command:

microk8s kubectl get nodes

The output of the command should contain something like this:


Now that you’re certain your nodes are all in the Ready state, create a new directory with the command:


Change into that newly-created directory with the command:


Create a new deployment YAML file with:


In that file, paste the following contents:


apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
– name: nginx
image: nginx:1.7.9
ports:
– containerPort: 80

Most of the above file should be self-explanatory, but there are certain sections to pay close attention to, such as:

  • apiVersion – details which version of the API will be used.
  • kind – details the type of object you are creating.
  • metadata – data that helps identify the object.
  • spec – the state you desire for the object

Save and close the file with the [Ctrl]+[X] keyboard shortcut.

Apply the newly created YAML file with the command:


The output should show something like this:


Verify the deployment with:


You should see something like this in the output:


Awesome. Let’s keep going.

You’ll now need to find out what IP address for which the NGINX pod can be reached. One thing to keep in mind is that this first deployment will only be available to the local network. We’ll take that a step further next time and make it available outside the cluster.

To locate the IP address of the NGINX pod, issue the command:


In the output, you should see something like this:


Our NGINX pod is running on IP address 10.1.219.7. We can test that by running the following command from the controller node:


The output should look something like this:


If your controller node has a GUI, you can open a web browser and point it to http://10.1.219.7 and see the NGINX Welcome Page printed.

Now, if you’ve followed my previous articles and installed Portainer to the Microk8s cluster, you can log into Portainer, go to Applications, and see our nginx-deployment is available (Figure 1).

Figure 1: Our nginx-deployment is running.

Click on the listing and you’ll see that the pod has been replicated to two nodes (Figure 2).

Figure 2:  Our NGINX deployment is not just running on a single node, but two.

This, of course, isn’t very useful, as you cannot reach the app from outside of the cluster. That’s OK, because next time around we’re going to go back to Portainer and find out how easy it is to deploy the same NGINX application, only make it visible from outside of the cluster. And even though deploying such an application is far easier with a tool like Portainer, it’s always good to know how to deploy from the command line as well.

Of course, it’s always best to work smarter, not harder… which is exactly what Portainer will help us achieve.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.