Skip to content

Definicja HTTPRoute w manifeście

Szablon definicji HTTPRoute

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
    name: <httproute-name>
spec:
    parentRefs:
    - name: <gateway-name>
    hostnames:
    - "<URL_hostname>"
    rules:
    - matches:
        - path:
              type: PathPrefix
              value: /<path>
          backendRefs:
          - name: <service_name>
            port: <port>

Przykładowa definicja HTTPRoute

  • Nazwa HTTPRoute: example-httproute
  • Hostname URL: www.example.com
  • Nazwa Gateway: example-gateway
  • Nazwa GatewayClass: example-gatewayclass
  • Rodzaj protokołu wystawionego w Gateway: http
  • Port nasłuchujący, ustalony w Gateway: 80
  • Nazwa Service: test-svc
  • Port Service: 80
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
    name: example-httproute
spec:
    parentRefs:
    - name: example-gateway
    hostnames:
    - "www.example.com"
    rules:
    - matches:
        - path:
              type: PathPrefix
              value: /test
          backendRefs:
          - name: test-svc
            port: 80

Podział ruchu (Canary)

Ruch zostanie podzielony w skali 70% do 30% między serwisami app1 oraz app2.

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
    name: example-split-httproute
spec:
    parentRefs:
    - name: example-gateway
    rules:
    - backendRefs:
      - name: app1
        port: 80
        weight: 70
     - name: app2
       port: 80
       weight: 30

Przekierowanie z HTTP na HTTPS

    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: https-redirect
      namespace: default
    spec:
      parentRefs:
      - name: nginx-gateway
      rules:
      - filters:
        - type: RequestRedirect
          requestRedirect:
            scheme: https

gdzie:

  • filters - dodatkowe przetwarzanie każdego z żądań
  • filters:type: RequestRedirect - nastąpi przekierowanie ruchu
  • RequestRedirect:scheme: https - nastąpi przekierowanie do HTTPS

Źródło: https://gateway-api.sigs.k8s.io/guides/http-redirect-rewrite/