ingress

Home

Kubernetes Ingress Lab

This lab guides you through setting up Kubernetes Ingress for routing external traffic to internal services. We will deploy sample applications, install an Nginx Ingress controller, and configure host-based routing and TLS.

1. Prerequisites: Kubernetes Cluster

Ensure you have a running Kubernetes cluster. You can use Minikube, Docker Desktop's Kubernetes, or a cloud-managed cluster. For on-premises setups, projects like Tigera Calico for On-Premises Kubernetes (link from original document) can be helpful, but any conformant cluster will do.

2. Initial Cluster Inspection

  1. List Nodes: Verify your nodes are ready.

    kubectl get no
  2. List Namespaces: See the existing namespaces in your cluster.

    kubectl get namespaces

3. Deploy Sample Applications (app1 & app2)

We'll create two simple Nginx applications, each in its own namespace, and expose them via ClusterIP services.

  1. Create and Deploy app1:

    kubectl create ns app1
    # The following YAMLs deploy Nginx and expose it via a ClusterIP service named 'my-nginx-clusterip'
    kubectl apply -n app1 -f https://raw.githubusercontent.com/xxradar/kuberneteslearning/master/nginx-deployment.yaml
    kubectl apply -n app1 -f https://raw.githubusercontent.com/xxradar/kuberneteslearning/master/nginx-expose-clusterip.yaml
  2. Create and Deploy app2:

    kubectl create ns app2
    kubectl apply -n app2 -f https://raw.githubusercontent.com/xxradar/kuberneteslearning/master/nginx-deployment.yaml
    kubectl apply -n app2 -f https://raw.githubusercontent.com/xxradar/kuberneteslearning/master/nginx-expose-clusterip.yaml
  3. Test app1 and app2 Internally: Verify that the applications are running and their services are up.

    kubectl get all -n app1 -o wide
    kubectl get all -n app2 -o wide

    Test connectivity to the services from within the cluster (e.g., using a temporary pod with siege or curl):

    # Test app1's service (my-nginx-clusterip.app1.svc.cluster.local or just my-nginx-clusterip from within app1 ns)
    kubectl run siege-app1 -it -n app1 --rm --image dockersec/siege -- siege -c1 -r1 http://my-nginx-clusterip
    # Test app2's service
    kubectl run siege-app2 -it -n app2 --rm --image dockersec/siege -- siege -c1 -r1 http://my-nginx-clusterip

    (Added -c1 -r1 to siege for a quick test.)

4. Install Nginx Ingress Controller

We will use the Nginx Ingress controller.Note: The manifest URL below points to version 0.32.0. For production or newer clusters, always refer to the official Nginx Ingress Controller deployment documentation for the latest recommended version and instructions.

Verify the Ingress controller installation:

To simplify access to the Ingress controller via its NodePorts:

(Made variable names more descriptive and robust by checking port names first.)

Test direct access to the Ingress controller's NodePorts (replace YOUR_NODE_IP with the IP of one of your Kubernetes nodes; if using Minikube/Kind, localhost or 127.0.0.1 might work).

(Clarified localhost usage.)

5. Configure Host-Based Routing (app1)

  1. Create app1_ingress.yaml:Note on apiVersion: extensions/v1beta1 for Ingress is deprecated and removed in Kubernetes v1.22+. Use networking.k8s.io/v1. The structure of the backend field also changes.

  2. Apply the Ingress resource for app1:

  3. Test access to app1: (Replace YOUR_NODE_IP with your node's IP or localhost if appropriate.)

6. Configure Host-Based Routing (app2)

  1. Create app2_ingress.yaml:

  2. Apply the Ingress resource for app2:

  3. Test access to app1 and app2: (Replace YOUR_NODE_IP with your node's IP or localhost.)

7. Inspect Ingress Controller Configuration (Optional)

You can check the Nginx configuration generated inside the Ingress controller pod to see how it's routing traffic.

(Made step 10 into a proper section and added command to get pod name.)

8. Configure TLS for an Ingress Resource

  1. Create a Self-Signed Certificate and Key: For testing, we'll create a self-signed certificate. For production, use certificates from a trusted CA.

  2. Create a Kubernetes Secret for TLS: Store the certificate and key in a TLS secret in the app1 namespace.

  3. Create tlsapp1_ingress.yaml:Note on apiVersion: networking.k8s.io/v1beta1 is also deprecated. Use networking.k8s.io/v1.

  4. Apply the TLS Ingress resource:

  5. Update Your /etc/hosts File: To test tlsapp1.dockersec.me locally, you need to map this hostname to an IP address that routes to your Ingress controller (e.g., a Node IP, or 127.0.0.1 if using Minikube/Kind or port-forwarding to a node).

    Important: Add the following line to your /etc/hosts file on the machine where you are running curl:

    (Example: 127.0.0.1 tlsapp1.dockersec.me or 192.168.1.100 tlsapp1.dockersec.me)

  6. Test TLS Access: (Replace YOUR_NODE_IP_OR_LOCALHOST with the IP used in /etc/hosts.)

    (Clarified curl command for TLS testing.)

Home

Last updated