Skip to content

Latest commit

 

History

History
208 lines (183 loc) · 3.57 KB

File metadata and controls

208 lines (183 loc) · 3.57 KB

video solution

https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io%2fv1.HTTPHeaderMatch

Create a Gateway

# vim nginx-gateway.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: nginx-gateway
  namespace: nginx-gateway
spec:
  gatewayClassName: nginx
  listeners:
    - name: http
      port: 80
      protocol: HTTP
      allowedRoutes: 
       namespaces: 
        from: All
k apply -f nginx-gateway.yaml

Create default restrict

# vim restricted.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: restricted
  namespace: app
spec:
  parentRefs:
    - name: nginx-gateway
      namespace: nginx-gateway
      sectionName: http
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: app-restricted
          port: 80


k apply -f restricted.yaml

create route logic cka.local

#vim cka-route.yaml

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cka-route
  namespace: app
spec:
  hostnames:
    - cka.local
  parentRefs:
    - name: nginx-gateway
      namespace: nginx-gateway
      sectionName: http
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
          headers:
            - name: environment
              value: dev
      backendRefs:
        - name: app-dev
          port: 80
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: app-prod
          port: 80


k apply -f cka-route.yaml

create route logic dev-cka.local

# vim dev-cka-route.yaml

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: dev-cka-route
  namespace: app
spec:
  hostnames:
    - dev-cka.local
  parentRefs:
    - name: nginx-gateway
      namespace: nginx-gateway
      sectionName: http
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: app-dev
          port: 80

k apply -f dev-cka-route.yaml

create route logic weight-cka.local

# vim weight-cka-route.yaml

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: weight-cka-route
  namespace: app
spec:
  hostnames:
    - weight-cka.local
  parentRefs:
    - name: nginx-gateway
      namespace: nginx-gateway
      sectionName: http
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: app-weight-v1
          port: 80
          weight: 70
        - name: app-weight-v2
          port: 80
          weight: 30          

k apply -f weight-cka-route.yaml

create route logic header-cka.local

# vim header-cka-route.yaml

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: header-cka-route
  namespace: app
spec:
  hostnames:
    - header-cka.local
  parentRefs:
    - name: nginx-gateway
      namespace: nginx-gateway
      sectionName: http
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      filters:
       - type: RequestHeaderModifier
         requestHeaderModifier:
           add:
           - name: User-Type
             value: test-user
           remove:
           - X-CH          
           set:
           - name: User-City
             value: NYC              
           
      backendRefs:
        - name: app-dev
          port: 80

k apply -f header-cka-route.yaml