CKA SimA 13 Gateway API 및 Ingress

1 개요[ | ]

Ingress 리소스를 Gateway API 기반 HTTPRoute로 변환 및 User-Agent 기반 조건부 라우팅 구현
  • 기존 Ingress 리소스를 Gateway API의 HTTPRoute로 전환
  • /auto 경로에서 User-Agentmobile인 경우 web-mobile로, 아닌 경우 web-desktop으로 라우팅 처리
  • Gateway API를 활용한 조건부 경로 매칭 구조 학습
  • 관련문서: "gateway" → https://kubernetes.io/docs/concepts/services-networking/gateway/

2 환경[ | ]

  • 네임스페이스: project-gatewayapi
  • Gateway: main
  • GatewayClass: nginx
  • Gateway 접근 주소: http://gateway.project:30080
  • 백엔드 서비스: web-desktop, web-mobile
  • 참고 Ingress 파일: /opt/course/13/ingress.yaml
  • 작성 대상 파일: /tmp/gatewayapi-httproute.yaml

3 Ingress 구조 확인[ | ]

Ingress 리소스의 정의를 확인하여 HTTPRoute로 변환할 정보 파악

$ cat /opt/course/13/ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: traffic-director
spec:
  ingressClassName: nginx
  rules:
    - host: r500.gateway
      http:
        paths:
          - path: /desktop
            pathType: Prefix
            backend:
              service:
                name: web-desktop
                port:
                  number: 80
          - path: /mobile
            pathType: Prefix
            backend:
              service:
                name: web-mobile
                port:
                  number: 80

4 HTTPRoute 생성[ | ]

Ingress 내용을 기반으로 Gateway API의 HTTPRoute로 변환

# /tmp/gatewayapi-httproute.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: traffic-director
  namespace: project-gatewayapi
spec:
  parentRefs:
    - name: main
  hostnames:
    - "gateway.project"
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /desktop
      backendRefs:
        - name: web-desktop
          port: 80
    - matches:
        - path:
            type: PathPrefix
            value: /mobile
      backendRefs:
        - name: web-mobile
          port: 80
    - matches:
        - path:
            type: PathPrefix
            value: /auto
          headers:
            - name: user-agent
              type: Exact
              value: mobile
      backendRefs:
        - name: web-mobile
          port: 80
    - matches:
        - path:
            type: PathPrefix
            value: /auto
      backendRefs:
        - name: web-desktop
          port: 80

5 주요 구현 포인트[ | ]

  • /desktop 경로 → web-desktop 서비스로 매핑
  • /mobile 경로 → web-mobile 서비스로 매핑
  • /auto 경로
    • User-Agentmobile일 경우 → web-mobile
    • 그 외에는 모두 → web-desktop
  • 조건부 매칭 시 headerspath는 AND 조건
  • 조건이 구체적인 항목을 먼저 선언해야 우선순위가 제대로 작동함

6 적용[ | ]

작성한 HTTPRoute 리소스 적용

# k apply -f /tmp/gatewayapi-httproute.yaml
httproute.gateway.networking.k8s.io/traffic-director created

7 테스트[ | ]

기본 경로 확인

$ curl http://gateway.project:30080/desktop
Web Desktop App
$ curl http://gateway.project:30080/mobile
Web Mobile App

조건부 라우팅 테스트

$ curl -H "User-Agent: mobile" http://gateway.project:30080/auto
Web Mobile App
$ curl -H "User-Agent: other" http://gateway.project:30080/auto
Web Desktop App
$ curl http://gateway.project:30080/auto
Web Desktop App

8 같이 보기[ | ]

9 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}