TheKoguryo's Tech Blog

Version 2023.06.19

Warning

This content has been generated by machine translation. The translations are automated and have not undergone human review or validation.

4.1.2 PATH based routing in NGINX Ingress Controller

PATH-based basic routing test

This is the most basic routing, and the routing service is different according to the URL PATH.

  1. Deploy the sample app for testing.

    Deploy two web pages with different background colors.

    kubectl create deployment nginx-blue --image=thekoguryo/nginx-hello:blue
    kubectl expose deployment nginx-blue --name nginx-blue-svc --port 80
    kubectl create deployment nginx-green --image=thekoguryo/nginx-hello:green
    kubectl expose deployment nginx-green --name nginx-green-svc --port 80
    
  2. Write the ingress setup YAML (path-basic.yaml).

    • /blue requests are routed to nginx-blue-svc
    • /green requests are routed to nginx-green-svc
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-path-basic
      annotations:
        kubernetes.io/ingress.class: nginx
    spec:
      rules:
      - http:
          paths:
          - path: /blue
            pathType: Prefix
            backend:
              service:
                name: nginx-blue-svc
                port:
                  number: 80
      - http:
          paths:
          - path: /green
            pathType: Prefix
            backend:
              service:
                name: nginx-green-svc
                port:
                  number: 80
    
  3. Deploy the created path-basic.yaml.

    $ kubectl apply -f path-basic.yaml 
    ingress.networking.k8s.io/ingress-path-basic created
    $ kubectl get ingress
    NAME                 CLASS    HOSTS   ADDRESS           PORTS   AGE
    ingress-path-basic   <none>   *       132.226.225.240   80      49s
    
  4. Connect to the EXTERNAL IP of the ingress controller checked earlier and check the result.

    • request /blue

      image-20211206164120654

    • request /green

      image-20211206164138996

    • Check POD information

      According to the defined PATH, you can see that the blue and green apps are routed to the deployed POD, respectively, with the web page background color and POD IP.

      $ kubectl get pod -o wide
      NAME                           READY   STATUS    RESTARTS   AGE   IP             NODE          NOMINATED NODE   READINESS GATES
      nginx-blue-6fccd8fb49-q6qph    1/1     Running   0          13m   10.244.0.138   10.0.10.139   <none>           <none>
      nginx-green-7f646c5c7f-snpxf   1/1     Running   0          13m   10.244.0.5     10.0.10.84    <none>           <none>
      

Rewrite Target

If you look at the URL PATH routing result, you can see that the paths of /blue and /green are finally routed and delivered to the running app. It is used only for routing in the ingress controller, and is used when modifications are necessary for the actual operation of the app.

  1. Write the ingress setup YAML (path-rewrite-target.yaml).

    • path: /blue -> /blue(/|$)(.*)
    • Add annotation: nginx.ingress.kubernetes.io/rewrite-target: /$2
    • example
      • ~~/blue -> passed to the app with ~~/
      • ~~/blue/abc -> passed to the app as ~~/abc
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-path-rewrite-target
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/rewrite-target: /$2
    spec:
      rules:
      - http:
          paths:
          - path: /blue(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: nginx-blue-svc
                port:
                  number: 80
      - http:
          paths:
          - path: /green(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: nginx-green-svc
                port:
                  number: 80
    
  2. Delete the preceding path-basic.yaml and deploy path-rewrite-target.yaml.

    $ kubectl delete -f path-basic.yaml 
    ingress.networking.k8s.io "ingress-path-basic" deleted
    $ kubectl apply -f path-rewrite-target.yaml 
    ingress.networking.k8s.io/ingress-path-rewrite-target created
    $ kubectl get ingress
    NAME                          CLASS    HOSTS   ADDRESS           PORTS   AGE
    ingress-path-rewrite-target   <none>   *       132.226.225.240   80      43s
    
  3. Connect to the EXTERNAL IP of the ingress controller checked earlier and check the result.

    • request ~~/blue

      • In routed apps, /blue is missing and only received by /

      image-20211207115428971

    • Request ~~/blue/abc

      • In routed apps, /blue is missing and only received by /abc

      image-20211207115512042



As an individual, this article was written with my personal time. There may be errors in the content of the article, and the opinions in the article are personal opinions.

Last updated on 5 Dec 2021