Kubernetes 101 part 2: Deploying apps to Kubernetes Cluster
In our previous article we looked at the kubernetes cluster and its components. We went a step further and created a local cluster using minikube.
With modern web services, users expect applications to be available 24/7, and developers expect to deploy new versions…medium.com
The objective of this section is to learn about application deployments, how it’s done with examples. We will deploy our app via the Kubectl CLI and configuration files. The code used in this section will be hosted on our GitHub repository.
Now that we have a running Kubernetes cluster, we can deploy a containerized application on it. This can be achieved through use of Deployment configurations. The deployments instruct Kubernetes how to create and update instances of your application. Once a deployment has been created, the kubernetes master schedules mentioned application instances into individual Nodes in the cluster.
Once the application has been created, Kubernetes deployment Controller will continuously monitor instances to ensure they satisfy the desired state. If a node hosting the application goes down or is deleted, the deployment controller replaces it, thus providing a self-healing mechanism to address machine failure or maintenance.
Deploying An App to Kubernetes
Kubernetes deployments can be managed via kubernetes command line interface kubectl. Kubectl uses the Kubernetes API to interact with the cluster. In this section we will look into some of the commands you will need to create deployments that run an application on the kubernetes cluster.
When creating a deployment you will need to specify the container image for your application and the number of replicas that you need in your cluster. This information can be changed later by updating the Deployment.
Create Nodejs Application
Now that we have established how we can manage deployments, let us create the application we will be deploying to our cluster. Create a folder in your workspace and add
Update the file with the following code. This will setup a simple server that we can use to test our deployments.
Let's run our app to make sure it is working fine.
When you visit http://localhost:3000, you should be able to see “Hello, World” on your browser. Now that our app is working, let's package it into a container.
Create a Docker container image
Before we can make any deployments to our cluster we need to create an image we will be using. This image will contain the app we just built. In your current working directory, create a file named
Dockerfile. A Dockerfile describes the image that you want to build. Images can be built by extending an existing image, for instance, our images will extend from Nodejs Image.
This image extends the official NodeJs image, exposes port 3000, copies
index.jsfile to the image and starts the NodeJs server.
Because we are using minikube, instead of pushing our images to a registry, we can simply build the image using the same docker host as the minikube VM so that the images will be available automatically. To achieve this we will have to ensure that we are using Minikube Docker Daemon:
If in any case, you don’t wish to use minikube host, you can undo this change by running.
Having setup docker let's build our image.
The command above will build our image tagged
kubernetes-101:v1 using our Dockerfile. After the command is done executing, the image will be available within our minikube context.
Create a Kubernetes Deployment
Kubernetes deployments are responsible for creating and managing pods — a kubernetes pod is a group of one or more containers, tied together for the purpose of administration and networking. Deployments check on the health of the pods and restart the pods' container when it terminates. For this reason, deployments are the recommended way to manage the creation and scaling of pods.
Kubernetes Deployments can be created in two ways, through kubectl run command or through YAML configuration- which is the recommended approach.
Note: We will be using the command line approach, however, we will be including the yaml configuration in code repository accompanying this article.
Run the command below to create a deployment.
This command will create a deployment that manages a pod. This pod will run a container based on the
kubernetes-101:v1image we created earlier on port 3000.
We can check if our deployment has been created by running kubectl get deployments command.
As you can see we now have a deployment with name
kubernetes-101. If we need more details on this deployment we can use the describe command.
This command provides more detailed information about our deployment this includes the image, replicas, ports, status etc. We can use this information to audit our deployments.
So far we have created the deployments via command-line. We can still achieve the same results using yaml configuration.
Note: If you didn’t clone the code repository accompanying add the following yaml configuration, add the following code snippet under the directory named k8s within your current working directory.
Before we proceed, let's delete the previous deployment. Run the following command against the cluster.
After is done executing, run
kubectl create -f k8s/deployment.yaml
When you run the commands we did before, you will notice that we get the same details and information. Configuration files a more recommended approach to creating kubernetes objects. This approach embraces the concept of infrastructure as code with makes managing the cluster much easier.
Now that we have explored both approaches of creating deployments, let's test if our deployment is actually working. We can achieve this by via kubernetes port forwarding.
First, we get the pod name, then use the kubectl port-forward command to forward traffic to the local port. We can now access our deployed application on
There we go!
In this section, we have looked into kubernetes deployments. We have also looked at how to package our applications into images via docker which we later deployed to the cluster via the command-line and yaml configuration. At this point, you should be able to explain what a deployment controller is, what it does and both approaches to creating one.
In the next article, we shall explore the deployment we just made to better understand how it works and other commands we can use to audit progress.