Deploying a website on Kubernetes.
I currently have a very simple static website hosted on Digital Ocean. I use Publii to generate the static content: the website is just a bunch of .html files generated by Publii. I want to learn how to deploy a website on Kubernetes. How would I deploy this simple website on Kubernetes?
To deploy a simple static website on Kubernetes, you'll need to perform a few steps. Here's a general outline of the process:
Containerize your website: Kubernetes works with containers, so you'll need to package your website into a container image. Since your website consists of static HTML files, you can use a lightweight web server like Nginx to serve them. Create a Dockerfile that pulls the Nginx base image, copies your static files into the appropriate location, and exposes the necessary port (typically port 80).
Build the container image: Use Docker to build the container image based on the Dockerfile you created. Run the following command in the directory containing your Dockerfile:
docker build -t your-image-name:tag .
This will build the image and tag it with the specified name and tag.
The period afteryour-image-name:tag
in the Docker build command is intentional and not an error. The period (.
) specifies the build context, which is the current directory. It tells Docker to look for the Dockerfile in the current directory and include its contents when building the image.Push the container image: If you're using a private container registry, push the image to the registry. For example, if you're using Docker Hub, you can push the image using the following command:
docker push your-image-name:tag
Set up Kubernetes cluster: If you don't have a Kubernetes cluster set up already, you'll need to create one. You can use a cloud provider like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or deploy Kubernetes locally using a tool like Minikube or kind.
Define a Kubernetes Deployment: Create a deployment YAML file to describe your website's deployment. Here's an example YAML file (
deployment.yaml
) for deploying your static website:apiVersion: apps/v1
kind: Deployment
metadata:
name: your-website
spec:
replicas: 1
selector:
matchLabels:
app: your-website
template:
metadata:
labels:
app: your-website
spec:
containers:
- name: your-website
image: your-image-name:tag
ports:
- containerPort: 80
Make sure to replace
your-website
,your-image-name
, andtag
with appropriate values.Apply the deployment: Apply the deployment YAML file to create the deployment on your Kubernetes cluster using the following command:
kubectl apply -f deployment.yaml
Expose the deployment: To access your website from outside the cluster, you'll need to create a service to expose it. Create a service YAML file (
service.yaml
) with the following content:apiVersion: v1
kind: Service
metadata:
name: your-website
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
app: your-website
Apply the service YAML file to create the service:
kubectl apply -f service.yaml
Access your website: Once the service is created, you can obtain the external IP by running the following command:
kubectl get service your-website
You should see an external IP address assigned to the service. Visit that IP address in your web browser, and you should be able to access your static website.
That's it! Your static website is now deployed on Kubernetes. Remember to update your website's content or Docker image and redeploy when needed.