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:
1 2 3 |
k8s3 Ready <none> 23d v1.24.8-2+1dda18a15eea38 k8s2 Ready <none> 23d v1.24.8-2+1dda18a15eea38 k8s1 Ready <none> 23d v1.24.8-2+1dda18a15eea38 |
Now that you’re certain your nodes are all in the Ready state, create a new directory with the command:
1 |
mkdir ~/deployment |
Change into that newly-created directory with the command:
1 |
cd ~/deployment |
Create a new deployment YAML file with:
1 |
nano nginx-deployment.yml |
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:
1 |
microk8s kubectl apply -f nginx-deployment.yml |
The output should show something like this:
1 |
deployment.apps/nginx-deployment created |
Verify the deployment with:
1 |
microk8s kubectl get deployments |
You should see something like this in the output:
1 |
nginx-deployment 2/2 2 2 5m32s |
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:
1 |
microk8s kubectl describe pods nginx |
In the output, you should see something like this:
1 2 |
IPs: IP: 10.1.219.7 |
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:
1 |
curl 10.1.219.7 |
The output should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at" <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> |
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.