Kubernetes for Beginners: Deploying a Static Website Made Easy
Deploy your static website on Kubernetes and learn the basics of container orchestration with this easy guide.
Great choice! Deploying a static website on Kubernetes is a simple yet powerful way to learn the basics of Kubernetes.
Together we can explore with simple steps how to deploy a static website on Kubernetes. Yes its easy, and this is the purpose; taking some easy steps to understand and learn the basics of Kubernetes.
Lets dig into it!
The docker image
Prep the files
I use Hugo, you can either have a range of HTML, JS, CSS or a simple index.html file. In my case I have a Hugo website, so I need to build it first.
I will use hugo cli to build that localy and export the statif files to the public
folder. Then I will copy the content of the public
folder to the nginx container.
|
|
Create the Dockerfile
Create a Docker image of your website: To deploy your static website on Kubernetes, you first need to create a Docker image of your website. You can use a simple Dockerfile to create the image. For example, if you have a website in a directory called mywebsite, you can create a Dockerfile with the following contents:
|
|
This Dockerfile uses the nginx base image and copies the contents of the public directory to the html directory of the image.
Build the Docker image
Now that you have a Dockerfile, you can build the Docker image. You can use the docker build command to build the image. For example, if you have a Dockerfile in the current directory, you can build the image with the following command:
|
|
This command builds the Docker image and tags it as
mywebsite:v1
.
Push the Docker image to a registry
No this is optional. If you want to push the image to a registry, you can use the docker push command. For example, if you want to push the image to the Docker Hub registry, you can use the following command:
|
|
If you upload the image to Docker Hub, you need to name the image with your Docker Hub username. For example, if your Docker Hub username is
myusername
, you need to name the image asmyusername/mywebsite:v1
.
Deploy the website on Kubernetes
To deploy the website on Kubernetes, you need to create a deployment and a service. The deployment manages the pods that run the website, and the service exposes the website to the outside world.
Deployment is a Kubernetes object that manages a set of pods. A deployment is responsible for creating and updating pods. A deployment is also responsible for managing the replica set that is associated with the pods. A replica set is responsible for managing the pods that are created by the deployment.
Service is a Kubernetes object that exposes a set of pods to the outside world. A service is responsible for load balancing the traffic that is sent to the pods. A service is also responsible for exposing the pods to the outside world.
Create the deployment
Create a file called mywebsite-deployment.yaml
with the following contents:
|
|
This YAML file creates a deployment with three replicas of the mywebsite container. The container runs the mywebsite:v1 image and listens on port 80.
Create the service
Create a file called mywebsite-service.yaml
with the following contents:
|
|
This YAML file creates a service that exposes the website to the outside world. The service selects the pods with the app: mywebsite label and exposes port 80.
Deploy the website
To deploy the website, run the following commands:
|
|
Check if the deployment and the service were deployed correctly
To check if the deployment and the service were deployed correctly, run the following commands:
|
|
To check if the pods were created correctly, run the following command:
|
|
To check id the service was created correctly, run the following command:
|
|
The EXTERNAL-IP field shows the IP address of the service. You can use this IP address to access the website on the port 80.
Access the website
To access the website, open a browser and navigate to the EXTERNAL-IP address of the service. For example, if the EXTERNAL-IP address is 123.123.123.123
, you can access the website at http://123.123.123.123.:80
.
Congratulations! You have now deployed a static website on Kubernetes. This is just the beginning of your journey with Kubernetes, and there is a lot more to learn.