In this post I will guide you through the process of deployment of a static website on Kubernetes on Minikube. The Source code for all the exercises are on github. This exercise is in the folder called kubernetes_static_website.
You should have installed minikube by now. Start minikube if you have not already done so.
Static website on Kubernetes – create docker image
The first step is creating the docker image. We will use the docker Runtime from Minikube so that the image is available to Kubernetes. In production you would have Kubernetes download the image from a repository. To use the docker Runtime type in
minikube.exe docker-env . It will give you the steps to setup the environment variables so that your docker points to the Runtime from Minikube. The screenshot shows how we have done it. Make sure that when you type in docker.exe images you see the images from Kubernetes.
The static website for us is a plain HTML page that has a single header that says Hello Kubernetes.
To create the image we first create the Dockerfile which uses httpd as the base. httpd or apache server sets /usr/local/apache2/htdocs as the base directory for the website. We copy the index.html file to this directory. When the docker container starts it will start the Apache Webserver and serve pages from this directory.
To build the image type in docker build -t , which is the tag name, kubernetes-static-website followed by dot. Docker will look for the dockerfile in current directory and use it to build the image. If you type in docker images, you should now be able to see the image. We will now deploy the image.
Static website in Kubernetes – create YAML files
To deploy we use the static_website_deployment.yml file. Lets look at what the file contains.
The content might not make sense now, but we will go through its details later on. For now, you need to understand that it is written in YAML format. The keyword kind tells Kuberentes that this file contains Deployment information and Service Information. The deployment specifies the image that will be deployed. In this case its the kubernetes_static_website image that we just created. The YAML also specifies that we need two instances or replicas of the website. We tell Kuberentes to open up port 80 from the container. imagePullPolicy is set to Never since we do not want to download the image from the repository.
We also specify a service in addition to the deployment. Service creates network rules to allow access to port 80 on the container that we opened. In this case we created a service of type NodePort since we want the website to be available outside the clusters. We will cover services in details in a later section. The service says that the website will be available at port 30500. In production we will create a service, an ingress controller and a load balancer to make the website available to end user. This website will be highly available since it has multiple replicas. We can also easily scale the application by increasing the replicas and Kubernetes will take care of adding the new replicas to the load balancer or the ingress controller. The steps to deploy the application are same as what we saw earlier, but let me repeat them here for sake of completeness.
123456 > kubectl get deployments> kubectl get pods> kubectl apply -f static_website_deployment.yml> kubectl get deployments> kubectl get pods> kubectl get services
Hope you had fun creating your first deployment in Kubernetes. Don’t worry if it doesn’t make much sense right now. We will cover each of the concepts that you saw here in great details later on.
In the next Section we will look at the architecture of Kubernetes and understand some of the key concepts.