CKA Lightning Lab 2 question 4 - Ingress I'm confused what this is doing ``` an . . .

Justin Dowe:
CKA Lightning Lab 2 question 4 - Ingress
I’m confused what this is doing

annotations:
    <http://nginx.ingress.kubernetes.io/rewrite-target|nginx.ingress.kubernetes.io/rewrite-target>: /

The documentation has that listed as the top example, but does not use it again
https://kubernetes.io/docs/concepts/services-networking/ingress/

Justin Dowe:

apiVersion: <http://networking.k8s.io/v1|networking.k8s.io/v1>
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    <http://nginx.ingress.kubernetes.io/rewrite-target|nginx.ingress.kubernetes.io/rewrite-target>: /
spec:
  ingressClassName: nginx-example
  rules:
  - http:
      paths:
      - path: /testpath
        pathType: Prefix
        backend:
          service:
            name: test
            port:
              number: 80
apiVersion: <http://networking.k8s.io/v1|networking.k8s.io/v1>
kind: Ingress
metadata:
  name: name-virtual-host-ingress
spec:
  rules:
  - host: <http://foo.bar.com|foo.bar.com>
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: service1
            port:
              number: 80
  - host: <http://bar.foo.com|bar.foo.com>
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: service2
            port:
              number: 80

Justin Dowe:
Solution
kind: Ingress
apiVersion: http://networking.k8s.io/v1|networking.k8s.io/v1
metadata:
name: ingress-vh-routing
annotations:
http://nginx.ingress.kubernetes.io/rewrite-target|nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:

Alistair Mackay:
HI @Justin Dowe

The rewrite target creates a “<https://www.nginx.com/blog/creating-nginx-rewrite-rules/|rewrite rule>” within the nginx machinery.

So we have our two paths in the ingress /watch and /wear . These are pointing to separate web services that expect their homepage to be at / so the rewrite-target annotation says that when the request is forwarded to the back end services, replace (rewrite) the /watch and /wear parts of the path with /.

To use <http://foo.bar.com|foo.bar.com> example, simplistically,

<http://foo.bar.com/watch> -> <http://video-service/>
and
<http://foo.bar.com/wear> -> <http://apparels-service/>

Without rewrite-target
<http://foo.bar.com/watch> -> <http://video-service/watch>
etc

Alistair Mackay:
There are many different annotations that can be used to control how nginx handles your ingress :slightly_smiling_face:
You should only need to know about rewrite-target and ssl-redirect
Note the following is allowed documentation in the exam
https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/nginx-configuration/annotations.md

Justin Dowe:
thank you for the explanation, and the rewrite rules makes sense as you explained it. However in the second example, if you have a moment, what is it doing differently, no rewrite and just a ‘/’?

Alistair Mackay:
You mean the one with the two hosts foo.bar and bar.foo?

No rewrite here, so the path should be passed unchanged, Host name makes the routing decision

<http://foo.bar.com/> -> <http://service1/>
<http://bar.foo.com/> -> <http://service2/>

Alistair Mackay:
This is “Name based virtual hosting”
https://en.wikipedia.org/wiki/Virtual_hosting

Alistair Mackay:
Just to clarify my initial response on the watch/wear. There is a combination of named-based virtual host and rewrite in that ingress. The actual routing is

<http://watch.ecom-store.com/watch> -> <http://video-service/>
<http://apparels.ecom-store.com/wear> -> <http://apparels-service/>