Friday, December 27, 2024
Google search engine
HomeGuest BlogsHow to Set up Kubernetes Ingress with MicroK8s

How to Set up Kubernetes Ingress with MicroK8s

Introduction

Kubernetes provides a secure, efficient, and reliable way to exchange data between microservices and resources outside the cluster. By utilizing the concept of Ingress, Kubernetes enables more straightforward load balancing, service discovery, and external connectivity management.

This article explains how to set up Ingress in a MicroK8s Kubernetes cluster. Testing in MicroK8s lets you understand how Ingress works and how to apply it to your production environments.

How to set up Kubernetes ingress with microk8s.How to set up Kubernetes ingress with microk8s.

Prerequisites

What Is Kubernetes Ingress?

Ingress is a Kubernetes API object that defines DNS routing rules for external traffic coming into a Kubernetes cluster. Using Ingress, cluster administrators set up granular load balancing, SSL/TLS termination, and name-based virtual hosting for their cluster services.

Ingress allows a single cluster to host multiple domains or subdomains, which enhances deployment flexibility and utilization of available resources. Since it represents the single point of contact for all external traffic coming into the cluster, it is also a single, centralized configuration point.

Note: Read our Kubernetes Ingress Guide for a more detailed overview of this Kubernetes concept.

How to Set Up Kubernetes Ingress?

Ingress is available in MicroK8s as a preinstalled add-on that must be enabled and configured to work. The following steps show how to enable, set up, and test Ingress on a MicroK8s cluster.

Step 1: Enable Ingress in MicroK8s

When you activate Ingress in MicroK8s, the platform creates and runs an Nginx Ingress controller pod. An Ingress controller implements the Ingress routing rules and ensures those rules are consistently applied.

Enable Ingress by executing the instructions below:

1. Enter the following command to activate the Ingress add-on:

microk8s enable ingress

The output shows MicroK8s creating several Kubernetes objects necessary for Ingress to work. Lastly, a message confirms that Ingress is enabled.

Enabling the Ingress add-on in MicroK8s.Enabling the Ingress add-on in MicroK8s.

2. Check if the Ingress controller is running by listing all the pods in the cluster and using the grep command to display only the Ingress pod:

microk8s kubectl get pods -A | grep ingress

The output shows the controller pod and confirms that it is Running.

The get pods command confirming that the Ingress controller pod is running.The get pods command confirming that the Ingress controller pod is running.

3. Make note of the pod name.

Step 2: Test Connection with Nginx

For the Ingress rules in this tutorial to work, the Nginx web server installation must be able to communicate with localhost. Test the connection with the curl command:

curl -v 127.0.0.1

The output shows an Nginx response, confirming that the web server communicates with localhost. The content of the response may be a 404 page, like in the example below:

The output of the curl command showing the Nginx web server is connected.The output of the curl command showing the Nginx web server is connected.

However, this response still means that Nginx is working correctly, and you may proceed to the next step.

Step 3: Edit ConfigMap

When MicroK8s enables Ingress, it generates three ConfigMaps in the ingress namespace. To enable exposing services over HTTP, you must edit the load balancer ConfigMap. Follow the procedure below to make the necessary adjustments.

1. List the available ConfigMaps by typing:

microk8s kubectl -n ingress get configmap

The output shows the existing ConfigMaps.

The three ConfigMaps generated by MicroK8s upon enabling the Ingress add-on.The three ConfigMaps generated by MicroK8s upon enabling the Ingress add-on.

The nginx-load-balancer-microk8s-conf is the ConfigMap that needs to be edited.

2. Enter ConfigMap edit mode by typing:

microk8s kubectl -n ingress edit configmaps nginx-load-balancer-microk8s-conf

3. Navigate around the file and add the following section under the kind: ConfigMap line:

data:
  use-forwarded-headers: "true"

The example screenshot shows the contents of the ConfigMap file after editing:

Contents of the ConfigMap file after editing.Contents of the ConfigMap file after editing.

4. Save and exit the file with the following Vim command:

:wq

The output confirms that the changes were automatically applied.

The output of the ingress edit command after editing.The output of the ingress edit command after editing.

Step 4: Confirm Ingress Setup

Verify the Ingress setup by checking the logs of the Ingress controller. To check the log, type the command below using the pod name you obtained in Step 1:

microk8s kubectl -n ingress logs [pod-name] | grep reload

The output confirms that the backend was reloaded successfully.

The log output showing that the configuration changes have been reloaded successfully.The log output showing that the configuration changes have been reloaded successfully.

Step 5: Test Ingress Setup

With Ingress enabled and set up, create a rule to test the configuration. Follow the steps below to create an Ingress rule.

1. Create a YAML file using a text editor, such as Nano:

nano test-ingress.yaml

2. Paste the following content into the file, replacing the service-name and the service-port placeholder values with the data from your cluster.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: [service-name]
            port:
              number: [service-port]

Note: If you do not have a service running in your cluster to follow along, you may want to create a test WordPress Kubernetes deployment.

Below is an example of the YAML file that creates an Ingress rule for the hello-world service on port 80. The / value in the path field means that the rule will be enforced when the user visits the domain home page.

The contents of the Ingress rule YAML.The contents of the Ingress rule YAML.

Save and exit the file.

3. Apply the configuration to the cluster by entering the following command:

microk8s kubectl apply -f test-ingress.yaml

The output confirms the creation of the Ingress rule.

Applying the Ingress rule.Applying the Ingress rule.

4. Navigate to localhost (or 127.0.0.1) page in your browser. It should show the contents served by the Ingress.

The localhost page showing the content served by the Ingress.The localhost page showing the content served by the Ingress.

Conclusion

After reading this tutorial, you should know how to enable, set up, and test Ingress on your MicroK8s Kubernetes cluster.

Due to the ease of deployment, Microk8s is a great testing ground for experimenting with Ingress and understanding how it works.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments