Kubernetes is challenging. There is no dispute about that. Deploying containers to a Kubernetes cluster has many moving parts, and many things can go wrong along the way. Further complicating matters is that deploying a Kubernetes cluster can be a brain-wrenching task.
That’s why tools like Canonical’s MicroK8 were developed. With such software, the process of deploying a Kubernetes cluster is much less demanding, allowing you to focus more on accelerating the deployment of applications and services to your cluster.
One of the many things you need to do to deploy applications and services to your Kubernetes cluster is to access it. For example, unlike Docker, when you deploy an application or service to a Kubernetes cluster, it is not automatically available on the network. If you are on a machine that is part of a cluster, you can definitely access that app or service because that machine has access to the subnet used by the cluster. Without a little extra trickery, those applications and services aren’t available outside the cluster. That is, you must manually enable it.
again… Lots of moving parts.
We have already seen the steps to install Microk8 on Rocky Linux and then connect the node to the controller. Add nodes to create clusters on which you can deploy applications and services.
What we’re going to do now is show you how to deploy your first application to a cluster and then make that application accessible from outside the cluster. One thing to note is that deploying applications/services to a MicroK8s cluster is similar to any other Kubernetes deployment. The biggest difference for users is that the deployment requires MicroK8s commands, whereas other Kubernetes distributions do not have that requirement.
Things that you need
The only thing needed for this is one or more controllers and a running MicroK8s cluster of 2 nodes. It can be deployed in data centers, test networks, or third-party cloud hosts. All you need is that the node is connected to the controller.
If you are not sure if the nodes are connected, log in to the controller and run the following command:
Get microk8s kubectl node
microk8s kubectl Import node
All nodes connected to the controller should be listed. If not, you will need to take steps to reconnect the nodes.
How to deploy your first application with MicroK8s
Log in to the MicroK8s controller. This demo deploys the NGINX web server application. Name this deployment nginx-webserver and use the official NGINX container image for the deployment. The command for this is:
create microk8s kubectl deployment nginx-webserver –image=nginx
microk8s kubectl create deployment nginx–web server —video=nginx
The output of that command should be:
deployment.app/nginx–web server made
Use the following command to verify the deployment was successful.
Get the microk8s kubectl pod
microk8s kubectl Import pod
You should see something like the following in the output.
nginx-webserver-67f557b648-4mfc6 1/1 run 0 9m59s
nginx–web server–67f557b648–4mfc6 One/One running 9 minutes 59 seconds
Congratulations. The first pod has been deployed into the cluster. What is a Pod? A Kubernetes pod is a collection of one or more containers and is the smallest unit of an application. A pod usually consists of multiple integrated containers, but can also consist of a single container. In the instance above, we deployed a pod with a single container (NGINX).
At this point, the NGINX application is running but not accessible. You must also deploy the service to make it accessible. What we are going to do here is expose our nginx-webserver deployment on port 80 using the “NodePort” type. What are NodePorts? In simple terms, a NodePort is an open port on every node connected to the cluster. Kubernetes routes incoming traffic from the NodePort to your deployed service or application.
The command to deploy the service is:
microk8s kubectl expose deploy nginx-webserver –type=”NodePort” –port 80
microk8s kubectl expose deployment nginx–web server —category=“Nodeport” —port 80
The output of the command should be:
Service/web server exposure
service/web server exposed
When I try to access a container running on port 80, such as http://192.168.1.45, I cannot access it. What do you offer? Kubernetes maps internal port 80 to a random internal port. You need to find the mapped port before accessing the running web server. For that, run the following command:
microk8s kubectl get svc nginx-webserver
microk8s kubectl Import SVC nginx–web server
The output should be:
nginx-webserver NodePort 10.152.183.105
nginx–web server NodePort 10.152.183.105 <doesn’t exist> 80:31508/TCP 3 minutes 11 seconds
As you can see, Kubernetes has mapped internal port 80 (the port being used by NGINX) to external port 31508 (the port being used by the Kubernetes cluster). So, point your web browser to 192.168.1.45:31508 and your browser should display the NGINX welcome screen.
Of course it uses the IP address of one of the nodes. However, the NGINX site does not appear when using the controller’s IP address. why? This is because the controller deploys the container to a node rather than itself.
All you need to do is deploy your first application to a Kubernetes cluster using MicroK8s.