컨테이너용 Azure Application Gateway에 대한 헤더 다시 작성 - 수신 API

컨테이너용 Application Gateway를 사용하면 백 엔드 대상에서 클라이언트 요청 및 응답의 HTTP 헤더를 다시 쓸 수 있습니다.

사용량 세부 정보

헤더 다시 쓰기는 Containers용 Application Gateway의 IngressExtension 사용자 지정 리소스를 활용합니다.

배경

헤더 다시 쓰기를 사용하면 백 엔드 대상에 대한 요청 및 응답 헤더를 수정할 수 있습니다.

다음 그림에서는 컨테이너용 Application Gateway에서 백 엔드 대상에 대해 요청을 시작할 때 특정 사용자 에이전트를 rewritten-user-agent라는 간소화된 값으로 다시 쓰는 요청의 예를 보여 줍니다.

요청 헤더를 백 엔드에 다시 쓰는 컨테이너용 Application Gateway를 보여 주는 다이어그램.

필수 조건

  1. BYO 배포 전략을 따르는 경우 컨테이너용 Application Gateway 리소스 및 ALB 컨트롤러를 설정했는지 확인합니다.

  2. ALB 관리형 배포 전략을 따르는 경우 ALB 컨트롤러를 프로비전하고 ApplicationLoadBalancer 사용자 지정 리소스를 통해 컨테이너용 Application Gateway 리소스를 프로비전했는지 확인합니다.

  3. 샘플 HTTP 애플리케이션 배포 클러스터에 다음 deployment.yaml 파일을 적용하여 헤더 다시 쓰기를 보여 주는 샘플 웹 애플리케이션을 만듭니다.

    kubectl apply -f https://trafficcontrollerdocs.blob.core.windows.net/examples/traffic-split-scenario/deployment.yaml
    

    이 명령은 클러스터에 다음을 만듭니다.

    • test-infra라는 네임스페이스
    • test-infra 네임스페이스에 있는 backend-v1backend-v2라는 두 서비스
    • test-infra 네임스페이스에 있는 backend-v1backend-v2라는 두 배포

필수 수신 API 리소스 배포

contoso.com에 대한 요청을 수신 대기하는 수신 리소스를 만듭니다.

kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-01
  namespace: test-infra
  annotations:
    alb.networking.azure.io/alb-name: alb-test
    alb.networking.azure.io/alb-namespace: alb-test-infra
    alb.networking.azure.io/alb-ingress-extension: header-rewrite
spec:
  ingressClassName: azure-alb-external
  rules:
    - host: contoso.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: backend-v1
                port:
                  number: 8080
EOF

참고 항목

ALB 컨트롤러는 ARM에서 컨테이너용 Application Gateway 리소스를 만들 때 프런트 엔드 리소스에 fe-<임의로 생성된 문자 8개>와 같은 명명 규칙을 사용합니다.

Azure에서 만들어진 프런트 엔드의 이름을 변경하려면 자체 배포 전략 가져오기를 따르는 것이 좋습니다.

수신 리소스가 만들어지면 상태에 부하 분산 장치의 호스트 이름이 표시되고 두 포트가 모두 요청을 수신 대기하는지 확인합니다.

kubectl get ingress ingress-01 -n test-infra -o yaml

수신이 성공적으로 만들어진 출력의 예제입니다.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    alb.networking.azure.io/alb-frontend: FRONTEND_NAME
    alb.networking.azure.io/alb-id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/yyyyyyyy/providers/Microsoft.ServiceNetworking/trafficControllers/zzzzzz
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.k8s.io/v1","kind":"Ingress","metadata":{"annotations":{"alb.networking.azure.io/alb-frontend":"FRONTEND_NAME","alb.networking.azure.io/alb-id":"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/yyyyyyyy/providers/Microsoft.ServiceNetworking/trafficControllers/zzzzzz", "alb.networking.azure.io/alb-ingress-extension":"header-rewrite"},"name"
:"ingress-01","namespace":"test-infra"},"spec":{"ingressClassName":"azure-alb-external","rules":[{"host":"contoso.com","http":{"paths":[{"backend":{"service":{"name":"backend-v1","port":{"number":8080}}},"path":"/","pathType":"Prefix"}]}}]}}
  creationTimestamp: "2023-07-22T18:02:13Z"
  generation: 2
  name: ingress-01
  namespace: test-infra
  resourceVersion: "278238"
  uid: 17c34774-1d92-413e-85ec-c5a8da45989d
spec:
  ingressClassName: azure-alb-external
  rules:
    - host: contoso.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: backend-v1
                port:
                  number: 8080
status:
  loadBalancer:
    ingress:
    - hostname: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.fzyy.alb.azure.com
      ports:
      - port: 80
        protocol: TCP

수신이 만들어지면 헤더 다시 쓰기 규칙을 사용하여 IngressExtension을 정의해야 합니다.

이 예제에서는 rewritten-user-agent 값으로 정적 사용자 에이전트를 설정합니다.

이 예제에서는 값 AGC-value를 사용하여 AGC-Header-Add라는 새 헤더를 추가하고 client-custom-header라는 요청 헤더를 제거하는 방법을 보여 줍니다.

이 예제에서는 문자열 일치에 "Exact"의 HTTPHeaderMatch를 사용할 수 있지만 추가 기능을 표시하기 위해 정규식에서는 데모가 사용됩니다.

kubectl apply -f - <<EOF
apiVersion: alb.networking.azure.io/v1
kind: IngressExtension
metadata:
  name: header-rewrite
  namespace: test-infra
spec:
  rules:
    - host: contoso.com
      rewrites:
        - type: RequestHeaderModifier
          requestHeaderModifier:
            set:
              - name: "user-agent"
                value: "rewritten-user-agent"
            add:
              - name: "AGC-Header-Add"
                value: "AGC-value"
            remove:
              - "client-custom-header"
EOF

IngressExtension 리소스가 만들어지면 리소스가 유효성 검사 오류 없음을 반환하고 유효한지 유효성을 검사합니다.

kubectl get IngressExtension header-rewrite -n test-infra -o yaml

컨테이너용 Application Gateway 리소스의 상태가 성공적으로 업데이트되었는지 확인합니다.

애플리케이션에 대한 액세스 테스트

이제 프런트 엔드에 할당된 FQDN을 통해 샘플 애플리케이션에 일부 트래픽을 전송할 준비가 되었습니다. 다음 명령을 사용하여 FQDN을 가져옵니다.

fqdn=$(kubectl get ingress ingress-01 -n test-infra -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')

프런트 엔드 FQDN에 대해 contoso.com curl 명령을 사용하여 서버 이름 표시기를 지정하면 backend-v1 서비스의 응답이 반환됩니다.

fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com

응답을 통해 다음을 확인해야 합니다.

{
 "path": "/",
 "host": "contoso.com",
 "method": "GET",
 "proto": "HTTP/1.1",
 "headers": {
  "Accept": [
   "*/*"
  ],
  "User-Agent": [
   "curl/7.81.0"
  ],
  "X-Forwarded-For": [
   "xxx.xxx.xxx.xxx"
  ],
  "X-Forwarded-Proto": [
   "http"
  ],
  "X-Request-Id": [
   "dcd4bcad-ea43-4fb6-948e-a906380dcd6d"
  ]
 },
 "namespace": "test-infra",
 "ingress": "",
 "service": "",
 "pod": "backend-v1-5b8fd96959-f59mm"
}

my-user-agent 값으로 user-agent 헤더를 지정하면 rewritten-user-agent의 백 엔드 서비스로부터 응답이 반환되어야 합니다.

fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com -H "user-agent: my-user-agent"

응답을 통해 다음을 확인해야 합니다.

{
 "path": "/",
 "host": "fabrikam.com",
 "method": "GET",
 "proto": "HTTP/1.1",
 "headers": {
  "Accept": [
   "*/*"
  ],
  "User-Agent": [
   "curl/7.81.0"
  ],
  "X-Forwarded-For": [
   "xxx.xxx.xxx.xxx"
  ],
  "X-Forwarded-Proto": [
   "http"
  ],
  "X-Request-Id": [
   "adae8cc1-8030-4d95-9e05-237dd4e3941b"
  ]
 },
 "namespace": "test-infra",
 "ingress": "",
 "service": "",
 "pod": "backend-v1-5b8fd96959-f59mm"
}

moo 값으로 client-custom-header 헤더를 지정하면 컨테이너용 Application Gateway가 백 엔드 서비스에 대한 연결을 시작할 때 요청에서 제거되어야 합니다.

fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com -H "client-custom-header: moo"

응답을 통해 다음을 확인해야 합니다.

{
 "path": "/",
 "host": "fabrikam.com",
 "method": "GET",
 "proto": "HTTP/1.1",
 "headers": {
  "Accept": [
   "*/*"
  ],
  "User-Agent": [
   "curl/7.81.0"
  ],
  "X-Forwarded-For": [
   "xxx.xxx.xxx.xxx"
  ],
  "X-Forwarded-Proto": [
   "http"
  ],
  "X-Request-Id": [
   "kd83nc84-4325-5d22-3d23-237dd4e3941b"
  ]
 },
 "namespace": "test-infra",
 "ingress": "",
 "service": "",
 "pod": "backend-v1-5b8fd96959-f59mm"
}

축하합니다. 컨테이너용 Application Gateway의 수집 API를 통해 ALB 컨트롤러를 설치하고, 백 엔드 애플리케이션을 배포하고, 헤더 값을 수정했습니다.