네임스페이스 간 Gateway API 리소스 구성
네임스페이스 간 Gateway API 리소스 구성 (Configure Gateway API resources across namespaces)
이 문서는 Kubernetes Gateway API 리소스를 여러 네임스페이스에 걸쳐 구성하는 방법을 다뤄요. Gateway의 allowedRoutes를 All로 설정하고 ReferenceGrant를 만들어서, 다른 네임스페이스에 있는 Service로 HTTP 트래픽을 라우팅하는 방법을 단계별로 설명해요. Konnect 배포와 온프레미스 Kong Ingress Controller 배포에 필요한 사전 준비부터 네임스페이스 생성, HTTPRoute 배포, 리소스 정리까지 전체 과정을 함께 살펴봐요
출처: 문서
본문
TL;DR
다른 네임스페이스의 Service로 HTTP 트래픽을 라우팅하려면 어떻게 해야 하나요?
Gateway 리소스에 allowedRoutes: All을 설정하고, 특정 네임스페이스의 HTTPRoute 인스턴스가 현재 네임스페이스의 Service에 접근할 수 있도록 허용하는 ReferenceGrant를 생성해요.
사전 준비
Konnect 배포: Kong Konnect
Konnect 계정이 없다면 온보딩 마법사를 통해 빠르게 시작할 수 있어요.
-
이 튜토리얼을 완료하려면 다음의 Konnect 항목들이 필요해요:
- 개인 액세스 토큰(PAT): Konnect PAT 페이지를 열고 Generate Token을 선택해서 새 개인 액세스 토큰을 생성해요.
-
개인 액세스 토큰을 환경 변수로 설정해요:
export KONNECT_TOKEN='YOUR KONNECT TOKEN'
Gateway API 활성화
-
Kong Ingress Controller를 설치하기 전에 Gateway API CRDs를 설치해요.
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.6.1/standard-install.yaml{: data-test-prereq="block" }
-
사용할
Gateway와GatewayClass인스턴스를 생성해요.echo " apiVersion: v1 kind: Namespace metadata: name: kong --- apiVersion: gateway.networking.k8s.io/v1 kind: GatewayClass metadata: name: kong annotations: konghq.com/gatewayclass-unmanaged: 'true' spec: controllerName: konghq.com/kic-gateway-controller --- apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: kong spec: gatewayClassName: kong listeners: - name: proxy port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same " | kubectl apply -n kong -f -{: data-test-prereq="block" }
Konnect 배포: KIC Control Plane 생성
Konnect API를 사용해서 새로운 CLUSTER_TYPE_K8S_INGRESS_CONTROLLER Control Plane을 생성해요:
CONTROL_PLANE_DETAILS=$(curl -X POST "https://us.api.konghq.com/v2/control-planes" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN" \
--json '{
"name": "My KIC CP",
"cluster_type": "CLUSTER_TYPE_K8S_INGRESS_CONTROLLER"
}'
)
나중에 values.yaml 파일에 사용할 id와 telemetry_endpoint가 필요해요. 환경 변수로 저장해 두세요:
CONTROL_PLANE_ID=$(echo $CONTROL_PLANE_DETAILS | jq -r .id)
CONTROL_PLANE_TELEMETRY=$(echo $CONTROL_PLANE_DETAILS | jq -r '.config.telemetry_endpoint | sub("https://";"")')
mTLS 인증서 생성
Kong Ingress Controller는 TLS 인증서로 보안이 유지되는 연결을 통해 Konnect와 통신해요.
openssl을 사용해서 새 인증서를 생성해요:
openssl req -new -x509 -nodes -newkey rsa:2048 -subj "/CN=kongdp/C=US" -keyout ./tls.key -out ./tls.crt
인증서를 curl로 Konnect API에 전송하려면 인증서가 한 줄짜리 문자열이어야 해요. awk를 사용해서 인증서를 포맷해요:
export CERT=$(awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' tls.crt);
다음으로, 인증서를 Konnect에 업로드해요:
curl -X POST "https://us.api.konghq.com/v2/control-planes/$CONTROL_PLANE_ID/dp-client-certificates" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN" \
--json '{
"cert": "'$CERT'"
}'
마지막으로, Kong Ingress Controller가 읽을 수 있도록 인증서를 Kubernetes 시크릿에 저장해요:
kubectl create namespace kong -o yaml --dry-run=client | kubectl apply -f -
kubectl create secret tls konnect-client-tls -n kong --cert=./tls.crt --key=./tls.key
Konnect 배포: Kong Ingress Controller 실행 (Konnect에 연결)
-
Kong Helm 차트를 추가해요:
helm repo add kong https://charts.konghq.com helm repo update -
values.yaml파일을 생성해요:cat <<EOF > values.yaml controller: ingressController: image: tag: "3.5" env: feature_gates: "FillIDs=true" konnect: license: enabled: true enabled: true controlPlaneID: "$CONTROL_PLANE_ID" tlsClientCertSecretName: konnect-client-tls apiHostname: "us.kic.api.konghq.com" gateway: image: repository: kong tag: "3.9.3" env: konnect_mode: 'on' vitals: "off" cluster_mtls: pki cluster_telemetry_endpoint: "$CONTROL_PLANE_TELEMETRY:443" cluster_telemetry_server_name: "$CONTROL_PLANE_TELEMETRY" cluster_cert: /etc/secrets/konnect-client-tls/tls.crt cluster_cert_key: /etc/secrets/konnect-client-tls/tls.key lua_ssl_trusted_certificate: system proxy_access_log: "off" dns_stale_ttl: "3600" secretVolumes: - konnect-client-tls EOF{: data-test-prereq="block" }
-
Helm을 사용해서 Kong Ingress Controller를 설치해요:
helm install kong kong/ingress -n kong --create-namespace --values ./values.yaml --wait{: data-test-prereq="block" }
-
이후 명령어에서 사용할 수 있도록
$PROXY_IP를 환경 변수로 설정해요:export PROXY_IP=$(kubectl get svc --namespace kong kong-gateway-proxy -o jsonpath='{range .status.loadBalancer.ingress[0]}{@.ip}{@.hostname}{end}') echo $PROXY_IP
온프레미스 배포: Kong Ingress Controller 실행
-
Kong Helm 차트를 추가해요:
helm repo add kong https://charts.konghq.com helm repo update -
Helm을 사용해서 Kong Ingress Controller를 설치해요:
helm install kong kong/ingress -n kong --create-namespace --wait{: data-test-prereq="block" }
-
이후 명령어에서 사용할 수 있도록
$PROXY_IP를 환경 변수로 설정해요:export PROXY_IP=$(kubectl get svc --namespace kong kong-gateway-proxy -o jsonpath='{range .status.loadBalancer.ingress[0]}{@.ip}{@.hostname}{end}') echo $PROXY_IP
네임스페이스 생성 및 참조 허용
-
HTTPRoute와 대상 Service를 담을 별도의 네임스페이스를 생성해요:kubectl create namespace test-source kubectl create namespace test-destination -
대상 네임스페이스에
ReferenceGrant리소스를 생성해요:echo 'kind: ReferenceGrant apiVersion: gateway.networking.k8s.io/v1beta1 metadata: name: test-grant namespace: test-destination spec: from: - group: gateway.networking.k8s.io kind: HTTPRoute namespace: test-source to: - group: "" kind: Service ' | kubectl apply -f -
ReferenceGrant는 네임스페이스가 다른 리소스에서 오는 참조를 직접 허용할 수 있게 해줘요. ReferenceGrant는 대상 리소스의 네임스페이스에 위치하며, ReferenceGrant의 네임스페이스에 있는 특정 리소스와 통신할 수 있는 리소스와 네임스페이스를 나열해요.
이 경우, 예제 설정은 test-source 네임스페이스의 HTTPRoute가 test-destination 네임스페이스의 Service를 참조할 수 있도록 허용해요.
다른 네임스페이스의 Gateway 리소스 사용
Gateway 리소스는 다른 네임스페이스에 있는 리소스(HTTPRoute, TCPRoute 등)로부터의 참조도 허용할 수 있어요. 하지만 이러한 참조는 ReferenceGrant를 사용하지 않아요. 이런 참조는 Gateway 전체가 아니라 Gateway 리소스의 각 리스너별로 정의되기 때문이에요.
리스너의 allowedRoutes 필드를 사용하면 어떤 라우팅 리소스가 해당 리스너에 바인딩될 수 있는지 정의할 수 있어요.
이 가이드의 기본 Gateway는 자신과 같은 네임스페이스(kong)에 있는 Route만 허용해요. test-source 네임스페이스의 Route를 허용하려면 허용 범위를 넓혀야 해요:
kubectl patch -n kong --type=json gateways.gateway.networking.k8s.io kong -p='[{"op":"replace","path": "/spec/listeners/0/allowedRoutes/namespaces/from","value":"All"}]'
그러면 다음과 같은 설정을 가진 Gateway 리소스가 만들어져요:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: kong
namespace: kong
spec:
gatewayClassName: kong
listeners:
- name: proxy
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: All
{:.no-copy-code}
리스너는 자신의 네임스페이스에 있는 Route(from: Same), 모든 네임스페이스의 Route(from: All), 또는 레이블로 지정된 네임스페이스 집합의 Route(from: Selector)를 허용할 수 있어요.
Service 및 HTTPRoute 배포
-
test-destination리소스에 echo Service를 배포해요.kubectl apply -f https://developer.konghq.com/manifests/kic/echo-service.yaml -n test-destination -
Service로 트래픽을 보내는 HTTPRoute를 배포해요:
echo 'apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: echo namespace: test-source annotations: konghq.com/strip-path: "true" spec: parentRefs: - name: kong namespace: kong rules: - matches: - path: type: PathPrefix value: /echo backendRefs: - name: echo kind: Service port: 1027 namespace: test-destination ' | kubectl apply -f -parent 참조와 backend 참조 양쪽에 있는
namespace필드에 주목하세요. 기본적으로 네임스페이스를 지정하지 않으면 여기의 항목들은 HTTPRoute와 같은 네임스페이스를 사용하려고 시도해요. -
Route를 통해 요청을 보내서 설정을 검증해요:
curl -s "$PROXY_IP/echo"결과는 다음과 같아야 해요:
Welcome, you are connected to node kind-control-plane. Running on Pod echo-965f7cf84-z9jv2. In namespace test-destination. With IP address 10.244.0.6.{:.no-copy-code}
정리
클러스터에서 KIC 제거
helm uninstall kong -n kong
더 알아보기 (Learn more)
- Konnect 계정이 아직 없다면 온보딩 마법사를 통해 빠르게 시작할 수 있어요 — onboarding wizard
- Konnect API 호출에 필요한 개인 액세스 토큰(PAT)은 이 페이지에서 생성할 수 있어요 — Konnect PAT page