Networking with Ingress on Kubernetes

KubernetesKubernetesBeginner
Practice Now

Introduction

In this lab, you will learn how to use Ingress to route external traffic to services running in a Kubernetes cluster.

Kubernetes Ingress is a powerful tool for managing external access to services in a Kubernetes cluster. Ingress acts as a layer 7 load balancer, allowing you to route traffic to different services based on the incoming URL path or hostname.

In this lab, we will create a sample application and expose it to the outside world using Ingress. We will use nginx-ingress as the Ingress controller, which is a popular and widely-used solution for Kubernetes Ingress.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/create -.-> lab-9681{{"`Networking with Ingress on Kubernetes`"}} kubernetes/get -.-> lab-9681{{"`Networking with Ingress on Kubernetes`"}} kubernetes/apply -.-> lab-9681{{"`Networking with Ingress on Kubernetes`"}} end

Install the Nginx Ingress Controller

First, we need to install the nginx-ingress controller in our cluster. We can do this by creating a Deployment and a Service that will be responsible for running the Ingress controller.

Create a namespace for the Ingress controller:

kubectl create namespace ingress-nginx

Install the ingress-nginx chart using kubectl:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.0/deploy/static/provider/cloud/deploy.yaml

Verify that the ingress-nginx controller pods are running:

kubectl get pods -n ingress-nginx

Create a Sample Application

Next, we will create a sample application that we will expose using Ingress.

Create a Deployment for a sample application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
      - name: sample-app
        image: nginx
        ports:
        - containerPort: 80

The YAML file called sample-app.yaml. Apply the deployment to your cluster with the following command:

kubectl apply -f sample-app.yaml

Create a Service for the sample application:

apiVersion: v1
kind: Service
metadata:
  name: sample-app
spec:
  selector:
    app: sample-app
  ports:
  - name: http
    port: 80
    targetPort: 80

The YAML file called service-sample-app.yaml. Apply the deployment to your cluster with the following command:

kubectl apply -f service-sample-app.yaml

Create an Ingress Resource

Now that we have the Ingress controller set up and a backend service running, we can create the rules for the Ingress resource.

In this example, we will create a simple rule to route traffic for the example.com domain to our backend service:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: sample-app
            port:
              name: http

The YAML file called ingress.yaml. Apply the Ingress resource to the cluster:

kubectl apply -f ingress.yaml

Test the Ingress Resource

Finally, we can test the Ingress resource to make sure everything is working correctly.

First, determine the IP address of the node:

kubectl get node -o wide

This command will get the kubernetes node address, The IP address labeled as INTERNAL-IP.

Next, add an entry to your /etc/hosts file to map the example.com domain to the IP address of the node:

echo "<IP_ADDRESS> example.com" | sudo tee -a /etc/hosts

Replace <IP_ADDRESS> with the internal IP address of the node address.

Then, get service nodeport for ingress-nginx.

kubectl get services -n ingress-nginx

This command will dcurisplay a list of services in the ingress-nginx namespace. Look for the nginx-ingress-controller service and note its NodePort.

Finally, use curl to make an HTTP request to the Ingress endpoint:

curl http://example.com: < NodePort > /

Replace <NodePort> with the NodePort of the nginx-ingress-controller service.

If everything is set up correctly, you should see the Nginx welcome page.

You can also test the Ingress by using a web browser to visit http://example.com:<NodePort>/nginx.

Congratulations, you have successfully set up an Ingress resource in Kubernetes and tested it to ensure that it is working correctly.

Summary

In this lab, we walked through the process of setting up and configuring an Nginx ingress controller in Kubernetes. We also created a sample application and used Ingress to route external traffic to our Nginx service. This is just the beginning of what you can do with Kubernetes networking, and we encourage you to explore further.

Other Kubernetes Tutorials you may like