Ingress

Kubernetes Ingress objects allow more sophisticated routing patterns to be established for traffic originating outside your cluster. By the end of this exercise, you should be able to:

  • Set up a coutour Kubernetes IngressController
  • Configure L7 routing, path-based routing and sticky sessions with a Kubernetes Ingress object

Setting up an IngressController

Before we can create any Ingress objects, we need an IngressController to manage them and provide the actual proxy to do the routing; we’ll set up a contour-based IngressController.

kubectl apply -f \
  https://raw.githubusercontent.com/projectcontour/contour/main/examples/render/contour.yaml
<result omitted>

File: /home/student/ingressclass.yaml

apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  name: contour
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
spec:
  controller: projectcontour.io/ingress-controller
kubectl apply -f ingressclass.yaml

Create a namespace for a test ingress:

kubectl create ns test-ingress

Create a new deployment of a captainkube version.

vi /home/student/captainkube.yaml

File: /home/student/captainkube.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: captainkube
    version: "1.0"
  name: captainkube
  namespace: test-ingress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: captainkube
      version: "1.0"
  strategy: {}
  template:
    metadata:
      labels:
        app: captainkube
        version: "1.0"
    spec:
      containers:
      - image: r.deso.tech/whoami/whoami
        name: captainkube
        ports:
        - containerPort: 80
        resources: {}
        env:
        - name: NAME_APPLICATION
          value: "captainkube"
kubectl apply -f captainkube.yaml
deployment.apps/captainkube created
vi /home/student/phippy.yaml

File: /home/student/phippy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: phippy
    version: "1.0"
  name: phippy
  namespace: test-ingress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: phippy
      version: "1.0"
  strategy: {}
  template:
    metadata:
      labels:
        app: phippy
        version: "1.0"
    spec:
      containers:
      - image: r.deso.tech/whoami/whoami
        name: phippy
        ports:
        - containerPort: 80
        resources: {}
        env:
        - name: NAME_APPLICATION
          value: "phippy"
kubectl apply -f phippy.yaml
deployment.apps/phippy created
kubectl expose deploy phippy -n test-ingress --type=ClusterIP --port=80
service/phippy exposed
kubectl expose deploy captainkube -n test-ingress --type=ClusterIP --port=80
service/captainkube exposed
kubectl get svc -n test-ingress
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
captainkube   ClusterIP   10.105.72.33   <none>        80/TCP    58s
phippy        ClusterIP   10.102.67.89   <none>        80/TCP    68s

Define an Ingress with two different rules:

  • phippy –> ClusterIP phyppy
  • captainkube –> ClusterIp captainkube
vi /home/student/ingressrules.yaml

File: /home/student/ingressrules.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: animals
  namespace: test-ingress
spec:
  ingressClassName: contour
  rules:
  - host: phippy.local
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: phippy
            port:
              number: 80
  - host: captainkube.local
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: captainkube
            port:
              number: 80

This ingress definition is using the Ingress class name that uses contour. Apply this ingress definition:

kubectl apply -f ingressrules.yaml
ingress.networking.k8s.io/animals created
kubectl get ingress
NAME      CLASS     HOSTS                            ADDRESS        PORTS   AGE
animals   contour   phippy.local,captainkube.local   10.10.95.201   80      6s

Now try to test with curl the IP of Ingress controller:

curl --header 'Host: phippy.local' http://10.10.95.201/

curl --header 'Host: captainkube.local' http://10.10.95.201/

In base of your host header you finish on the relative cluster IP.

Clean up

kubectl  delete ns test-ingress
namespace "test-ingress" deleted