Ingress 경로 유형 예제
Ingress 경로 유형 예제 (Ingress Path Types Example)
이 예제는 다양한 경로 유형이 어떻게 상호작용하는지 알려드리고, Cilium이 기대대로 동작하는지 테스트할 수 있게 해줘요. Cilium Ingress가 활성화되어 있어야 하며, kubectl과 jq가 설치되어 있어야 해요.
본문
이 예제는 다양한 경로 유형이 어떻게 상호작용하는지 살펴보고, Cilium이 기대대로 동작하는지 테스트할 수 있게 해줘요.
이 예제는 Cilium Ingress가 활성화되어 있어야 하며, kubectl과 jq가 설치되어 있어야 해요.
예제 앱 배포하기 (Deploy the example app)
이것은 어떤 경로가 어떤 백엔드로 전달되는지 볼 수 있게 해주는 ingress-conformance-echo 도구의 다섯 개 복사본을 배포해요.
$ # Apply the base definitions
$ kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/main/examples/kubernetes/servicemesh/ingress-path-types.yaml
$ # Apply the Ingress
$ kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/main/examples/kubernetes/servicemesh/ingress-path-types-ingress.yaml
Ingress 검토하기 (Review the Ingress)
여기 사용된 Ingress는 다음과 같아요:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: multiple-path-types
namespace: default
spec:
ingressClassName: cilium
rules:
- host: "pathtypes.example.com"
http:
paths:
- backend:
service:
name: exactpath
port:
number: 80
path: /exact
pathType: Exact
- backend:
service:
name: prefixpath
port:
number: 80
path: /
pathType: Prefix
- backend:
service:
name: prefixpath2
port:
number: 80
path: /prefix
pathType: Prefix
- backend:
service:
name: implpath
port:
number: 80
path: /impl
pathType: ImplementationSpecific
- backend:
service:
name: implpath2
port:
number: 80
path: /impl.+
pathType: ImplementationSpecific
여기서 디플로이먼트마다 하나씩 다섯 개의 일치(match)가 있는 걸 볼 수 있어요.
Ingress는 의도적으로 규칙을 Envoy에서 구성될 순서와 다르게 배열해요.
- Exact 일치의 경우
/exact만 일치시켜exactpathService로 보내요. - Prefix 일치의 경우
/를 일치시켜prefixpathService로 보내고,/prefix를 일치시켜prefixpath2Service로 보내요. - ImplementationSpecific 일치의 경우
/impl.+(완전한 정규식)를 일치시켜implpath2Service로 보내요. 또한/impl(정규식 문자가 없는 것)을 일치시켜implpathService로 보내요.
여기서 의도는 ingress-conformance-echo 컨테이너가 반향한 응답을 참고해서 어떤 규칙을 일치시켰는지 알 수 있게 하는 거예요.
Ingress가 올바르게 프로비저닝됐는지 확인 (Check that the Ingress has provisioned correctly)
먼저 Ingress가 올바르게 프로비저닝됐는지 확인해야 해요.
$ export PATHTYPE_IP=`k get ing multiple-path-types -o json | jq -r '.status.loadBalancer.ingress[0].ip'`
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/ | jq
{
"path": "/",
"host": "pathtypes.example.com",
"method": "GET",
"proto": "HTTP/1.1",
"headers": {
"Accept": [
"*/*"
],
"User-Agent": [
"curl/7.81.0"
],
"X-Envoy-External-Address": [
"your-ip-here"
],
"X-Forwarded-For": [
"your-ip-here"
],
"X-Forwarded-Proto": [
"http"
],
"X-Request-Id": [
"6bb145e8-addb-4fd5-a76f-b53d07bd1867"
]
},
"namespace": "default",
"ingress": "",
"service": "",
"pod": "prefixpath-7cb697f5cd-wvv7b"
}
여기서 Ingress가 올바르게 프로비저닝됐고 요청에 응답하고 있음을 볼 수 있어요. 또한 / 경로가 Ingress에서 예상한 대로 prefixpath 디플로이먼트가 제공했음을 볼 수 있어요.
경로가 기대대로 동작하는지 확인 (Check that paths perform as expected)
다음 예제는 jq를 사용해 pod 필드에서 첫 번째 요소를 추출하는데, 이것이 연관된 디플로이먼트의 이름이에요. 즉 prefixpath-7cb697f5cd-wvv7b는 prefixpath를 반환해요.
$ echo Should show "prefixpath"
Should show prefixpath
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/ | jq '.pod | split("-")[0]'
"prefixpath"
$ echo Should show "exactpath"
Should show exactpath
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/exact | jq '.pod | split("-")[0]'
"exactpath"
$ echo Should show "prefixpath2"
Should show prefixpath2
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/prefix | jq '.pod | split("-")[0]'
"prefixpath2"
$ echo Should show "implpath"
Should show implpath
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/impl | jq '.pod | split("-")[0]'
"implpath"
$ echo Should show "implpath2"
Should show implpath2
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/implementation | jq '.pod | split("-")[0]'
"implpath2"
(위의 "Copy Commands" 버튼을 사용해 복사-붙여넣기를 줄일 수 있어요.)
여기서 가장 흥미로운 예제는 마지막 것인데, /implementation을 implpath2 Service로 보내고 /impl은 implpath으로 가요. 이는 /implementation이 /impl.+ 정규식과 일치하고, /impl이 /impl 정규식과 일치하기 때문이에요.
이제 Ingress 객체를 정규식 /impl.* 대신 사용하도록 패치한다면(앞의 +가 하나 이상의 문자와 일치하는 대신 *가 0개 이상의 문자와 일치한다는 점에 주목), 마지막 두 검사에 대해 다른 결과를 얻을 거예요:
$ echo Should show "implpath2"
Should show implpath
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/impl | jq '.pod | split("-")[0]'
"implpath"
$ echo Should show "implpath2"
Should show implpath2
$ curl -s -H "Host: pathtypes.example.com" http://$PATHTYPE_IP/implementation | jq '.pod | split("-")[0]'
"implpath2"
/impl에 대한 요청은 이제 더 긴 패턴 /impl.*과 일치해요.
여기서의 교훈은 정규식을 조심해서 사용하라는 거예요!
예제 정리 (Clean up the example)
마지막으로 예제를 정리할게요:
$ # Apply the base definitions
$ kubectl delete -f https://raw.githubusercontent.com/cilium/cilium/main/examples/kubernetes/servicemesh/ingress-path-types.yaml
$ # Apply the Ingress
$ kubectl delete -f https://raw.githubusercontent.com/cilium/cilium/main/examples/kubernetes/servicemesh/ingress-path-types-ingress.yaml
더 알아보기 (Learn more)
- Cilium Ingress — Cilium Ingress 컨트롤러 (Ingress Path Types and Precedence 섹션)
- HTTP Ingress 예제 — HTTP Ingress 예제
- Ingress 참조 — Ingress 어노테이션 참조