Prerequisites:
– First, build your Docker image from your website’s code.
– Ensure MicroK8s is installed on your Ubuntu server. If you need to install MicroK8s, you can follow the steps in this article. Part 1: How to setup Microk8s Kubernetes in Ubuntu Server
– Install Lens Desktop to simplify Kubernetes management.
– Push your Docker image to your image registry, such as GitLab, DockerHub, Gitea, etc.
– Ensure you have access to log in and pull from your image registry.
- Register a registry credential to pull the Docker Image.
- Log in via SSH to your MicroK8s server.
- Create a file named “registry-secret.yaml” and fill it with the following script:
microk8s kubectl create secret docker-registry alvian-docker-registry \ --docker-server=registry.alvianaufan.my.id \ --docker-username=alvian \ --docker-password=glasds212331133455XXXXXXXXX \ --docker-email=alvian@alvianaufan.my.id
- Save the file with the name “registry-secret.yaml” and exit.
- Run this command to register a credential for logging into your image registry and pulling your Docker image.
microk8s kubectl apply -f registry-secret.yaml
-
If you want to deploy a website with environment variables, I recommend creating an environment secret in your cluster.
Trending– Create a file named “env-website.yaml” and fill it with the following script:
apiVersion: v1 kind: Secret metadata: name: alvian-website-env type: Opaque data: NODE_ENV: ZGV2ZWxvcG1lbnQ= APP_PORT: NTAwMQ==
– You can add more environment variables by copying the example and saving it first. In the next step, you can edit the environment variables from Lens Desktop.
– Save and exit.
– Apply the environment variables by running this command:microk8s kubectl apply -f env-website.yaml
– Now you can see your env and edit your env simply by Lens Desktop > your cluster > Config > Secret
– Adjust the ENV to your needs
- Deploy your Docker image and run it.
– Create a file named “deploy-website.yaml” and fill it with the following script:apiVersion: apps/v1 kind: Deployment metadata: name: alvian-website spec: replicas: 1 selector: matchLabels: app: alvian-website template: metadata: labels: app: alvian-website spec: containers: - name: alvian-website image: registry.alvianaufan.my.id/alvianaufan/website:latest ports: - containerPort: 5001 envFrom: - secretRef: name: alvian-website-env imagePullSecrets: - name: alvian-docker-registry
– save and exit
– run this command to apply the deploymentmicrok8s kubectl apply -f deploy-website.yaml
– Your website deployment is in progress. You can check the progress in Lens Desktop by navigating to Your Cluster > Workloads > Pods or Deployments.
– Ensure your pods are in the “Running” state.
– If your pod is stuck in “Pending” or “Evicted” status, check the pod logs for troubleshooting any errors. - Create a service to forward your pod’s container port.
- Create a file named “website-service.yaml” and fill it with the following script:
apiVersion: v1 kind: Service metadata: name: alvian-website-service spec: selector: app: alvian-website ports: - protocol: TCP port: 5001 targetPort: 5001 type: ClusterIP
- Save and exit.
- Apply the service by running this command:
microk8s kubectl apply -f website-service.yaml
-
The example above illustrates that your cluster IP and ports are assigned to your pod container.
- Create a file named “website-service.yaml” and fill it with the following script:
- Next, you can point your domain to your pod using a URL that can be accessed by a browser.
– For example, I will use my domain: website.alvianaufan.my.id.
– Install and configure an Ingress Controller.microk8s enable ingress
– Create a file named “ingress.yaml” and fill it with the following script:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: alvian-website-ingress namespace: default annotations: nginx.ingress.kubernetes.io/rewrite-target: / nginx.ingress.kubernetes.io/affinity: cookie nginx.ingress.kubernetes.io/session-cookie-name: "route" nginx.ingress.kubernetes.io/session-cookie-hash: "sha1" spec: rules: - host: website.alvianaufan.my.id http: paths: - path: / pathType: Prefix backend: service: name: alvian-website-service port: number: 5001
– Save and exit.
– Apply the Ingress by running this command:microk8s kubectl apply -f ingress.yaml
– Now you can see on your Lens Desktop > Network > Ingress
That’s How to deploy your website to a Mikrok8s Cluster. Please write any questions or problems you have regarding this article in the comments section. 🙂