Kubernetes에서 멀티 포트 서비스 구성
Kubernetes에서 멀티 포트 서비스 구성 (Configure multi-port services on Kubernetes)
이 페이지에서는 여러 포트를 노출하는 Kubernetes Pod를 단일 Consul 멀티 포트 서비스로 등록하고, 메시의 다른 서비스에서 어떻게 연결하는지 설명해요.
출처: 문서
본문
Enterprise
Kubernetes에서 멀티 포트 서비스 등록은 Consul Enterprise가 필요합니다. Consul Community Edition에서 Consul은 첫 번째 포트만 단일 포트 서비스로 등록합니다.
이 페이지에서는 여러 포트를 노출하는 Kubernetes Pod를 단일 Consul 멀티 포트 서비스로 등록하고, 메시의 다른 서비스에서 어떻게 연결하는지 설명합니다.
여러 포트가 있는 Pod를 서비스 메시에 추가하려면 명명된 포트를 노출하는 단일 Kubernetes Service를 만들고 하나의 서비스 계정으로 등록하세요. Consul은 Pod를 단일 멀티 포트 서비스로 등록하여 메시 트래픽을 각 명명된 포트로 라우팅합니다.
요구 사항 (Requirements)
- Consul Enterprise
- 워크로드의 각 포트를 고유한 이름으로 노출하는 Kubernetes Service
멀티 포트 서비스 등록 (Register the multi-port service)
Pod를 멀티 포트 서비스로 등록하려면 다음 단계를 완료하세요:
- 서비스에 대한 단일 서비스 계정을 만듭니다.
- 각 포트를 고유한 이름으로 노출하는 Kubernetes Service를 만듭니다.
- 일치하는 명명된 컨테이너 포트를 노출하고
consul.hashicorp.com/connect-inject주석으로 주입을 활성화하는 Deployment를 만듭니다.
consul.hashicorp.com/connect-service-port 주석을 설정하지 않으면 Consul은 노출된 모든 컨테이너 포트를 단일 멀티 포트 서비스의 명명된 포트로 등록하고 첫 번째 포트를 기본 포트로 사용합니다.
다음 매니페스트는 web 서비스 계정, Kubernetes Service, Deployment를 정의합니다. web 서비스는 9090의 api-port, 9091의 metrics, 9092의 admin-port라는 세 개의 명명된 포트를 노출합니다. Deployment는 세 포트를 모두 제공하는 단일 컨테이너를 실행합니다. 그런 다음 static-client 서비스가 서비스 메시를 통해 각 포트에 연결합니다.
web.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
data:
nginx.conf: |
events {}
http {
server {
listen 9090;
location / {
default_type text/plain;
return 200 'Response from api-port 9090: Hello there!\n';
}
}
server {
listen 9091;
location / {
default_type text/plain;
return 200 'Response from metrics port 9091: Hello again!\n';
}
}
server {
listen 9092;
location / {
default_type text/plain;
return 200 'Response from admin port 9092: Hello again!\n';
}
}
}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: web
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- name: api-port
port: 9090
targetPort: 9090
- name: metrics
port: 9091
targetPort: 9091
- name: admin-port
port: 9092
targetPort: 9092
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
annotations:
'consul.hashicorp.com/connect-inject': 'true'
labels:
app: web
spec:
serviceAccountName: web
volumes:
- name: config-volume
configMap:
name: web-config
containers:
- name: nginx
image: nginx:alpine
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
ports:
- name: api-port
containerPort: 9090
- name: metrics
containerPort: 9091
- name: admin-port
containerPort: 9092
멀티 포트 서비스에 연결 (Connect to the multi-port service)
멀티 포트 서비스에 연결하는 방법은 투명 프록시 모드가 활성화되었는지 여부에 따라 다릅니다.
투명 프록시 활성화됨 (Transparent proxy enabled)
투명 프록시 모드가 활성화되면 <port-name>.<service-name>.virtual.consul 형식의 가상 DNS를 사용하여 업스트림 서비스의 특정 포트를 지정합니다. 다음 static-client 매니페스트는 투명 프록시를 통해 멀티포트 업스트림에 연결하는 서비스의 예입니다. 이 서비스에 대해 활성화된 유일한 Consul 관련 K8s 주석은 connect-inject와 transparent-proxy입니다.
static-client.yaml
apiVersion: v1
kind: Service
metadata:
# This name will be the service name in Consul.
name: static-client
spec:
selector:
app: static-client
ports:
- port: 80
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: static-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: static-client
spec:
replicas: 1
selector:
matchLabels:
app: static-client
template:
metadata:
name: static-client
labels:
app: static-client
annotations:
'consul.hashicorp.com/connect-inject': 'true'
'consul.hashicorp.com/transparent-proxy': 'true'
spec:
containers:
- name: static-client
image: curlimages/curl:latest
# Spin and wait forever; connect with `kubectl exec` to demonstrate the upstreams.
command: ['/bin/sh', '-c', '--']
args: ['while true; do sleep 30; done;']
# If ACLs are enabled, the serviceAccountName must match the Consul service name.
serviceAccountName: static-client
static-client를 배포한 후 웹의 각 포트에 가상 주소로 연결하세요:
$ kubectl exec deploy/static-client -- curl --silent http://api-port.web.virtual.consul:9090
Response from api-port 9090: Hello there!
$ kubectl exec deploy/static-client -- curl --silent http://metrics.web.virtual.consul:9091
Response from metrics port 9091: Hello again!
$ kubectl exec deploy/static-client -- curl --silent http://admin-port.web.virtual.consul:9092
Response from admin port 9092: Hello again!
기본적으로 Consul은 포트 이름 접두사 없이 업스트림 서비스에 연결하는 클라이언트에 대한 이전 버전과의 호환성을 유지하기 위해 첫 번째 등록 포트를 서비스의 기본 포트로 사용합니다. 이 동작을 재정의하고 특정 포트를 기본값으로 표시하려면 업스트림 web Pod에 consul.hashicorp.com/connect-service-default-port 주석을 사용하세요. 다음 주석은 api-port를 기본 포트로 표시합니다:
annotations:
'consul.hashicorp.com/connect-inject': 'true'
'consul.hashicorp.com/connect-service-default-port': 'api-port'
서비스 기본 포트가 작동하는 방식의 예입니다. 서비스가 web.virtual.consul에 연결하려고 시도하면, 특히 호스트 이름에 포트 이름 접두사가 없으면 connect-service-default-port에 정의된 포트로 라우팅됩니다. 이 경우 api-port입니다:
$ kubectl exec deploy/static-client -- curl --silent http://web.virtual.consul:9090
Response from api-port 9090: Hello there!
Consul on Kubernetes가 지원하는 주석 및 라벨에 대한 자세한 내용은 annotations and labels reference를 참조하세요.
투명 프록시 비활성화됨 (Transparent proxy disabled)
투명 프록시 모드가 비활성화되면 consul.hashicorp.com/connect-service-upstreams 주석에 각 업스트림을 정의하고 업스트림별로 destination_port 매개변수를 제공해야 합니다. 그러면 애플리케이션이 원하는 대상 포트의 localhost에 연결하여 업스트림 서비스에 도달합니다. 다음 static-client 매니페스트는 투명 프록시가 비활성화되고 세 개의 업스트림을 정의합니다.
static-client.yaml
apiVersion: v1
kind: Service
metadata:
# This name will be the service name in Consul.
name: static-client
spec:
selector:
app: static-client
ports:
- port: 80
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: static-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: static-client
spec:
replicas: 1
selector:
matchLabels:
app: static-client
template:
metadata:
name: static-client
labels:
app: static-client
annotations:
'consul.hashicorp.com/connect-inject': 'true'
'consul.hashicorp.com/transparent-proxy': 'false'
'consul.hashicorp.com/connect-service-upstreams': 'web.svc.default.ns.default.ap:9090:destination_port=api-port,web.svc.default.ns.default.ap:9091:destination_port=metrics,web.svc.default.ns.default.ap:9092:destination_port=admin-port'
spec:
containers:
- name: static-client
image: curlimages/curl:latest
# Spin and wait forever; connect with `kubectl exec` to demonstrate the upstreams.
command: ['/bin/sh', '-c', '--']
args: ['while true; do sleep 30; done;']
# If ACLs are enabled, the serviceAccountName must match the Consul service name.
serviceAccountName: static-client
static-client를 배포한 후 각 업스트림에 로컬 포트로 연결하세요:
$ kubectl exec deploy/static-client -- curl --silent http://localhost:9090
Response from api-port 9090: Hello there!
$ kubectl exec deploy/static-client -- curl --silent http://localhost:9091
Response from metrics port 9091: Hello again!
$ kubectl exec deploy/static-client -- curl --silent http://localhost:9092
Response from admin port 9092: Hello again!
멀티 포트 서비스의 주의 사항 (Caveats for multi-port services)
멀티 포트 서비스를 등록할 때 다음 주의 사항을 고려하세요:
- 멀티 포트 서비스 등록은 Consul Enterprise가 필요합니다. Consul Community Edition에서는 Consul이 첫 번째 포트만 단일 포트 서비스로 등록합니다. Enterprise
- 멀티 포트 서비스의 모든 포트는 동일한 프로토콜을 사용해야 합니다. Consul은 단일 멀티 포트 서비스에 대한 프로토콜 조합을 지원하지 않습니다.
- 투명 프록시 모드가 비활성화되면 Kubernetes 주석 문자 제한으로 인해 서비스에 정의할 수 있는 업스트림 수가 제한됩니다.
- Consul은 각 Pod에 대한 상태 검사를 수행합니다. Pod의 컨테이너 중 하나에 대해 상태 검사가 실패하면 Consul은 전체 멀티 포트 서비스를 비정상으로 표시합니다.
- Consul 클러스터 피어링은 지원되지 않습니다.
멀티 포트 서비스 제한 사항 및 비-Kubernetes 구성 옵션에 대한 자세한 내용은 multi-port services를 참조하세요. 특정 멀티포트 기능의 현재 크로스 버전, 크로스 플랫폼 지원 상태는 multiport feature support를 참조하세요.
다음 단계 (Next steps)
- Multi-port services
- Multiport feature support
- Kubernetes service mesh workload scenarios
- Annotations and labels reference