본문 바로가기

Cloud

(NGINX Ingress Controller) - TCP, UDP service 노출해 redis cluster와 연결하기

반응형

🍳머리말

TCP를 지원하지 않는 Ingress를 지원하도록 약간의 꼼수를 사용하는 설명글입니다.


📕TCP service 노출하기

📔 설명

k8s Ingress 객체는 TCP, UDP port로 routing하기 위한 service를 지원하지 않습니다. 때문에 Ingress controller는 이를 지원하기 위해 --tcp-services-configmap, --udp-services-configmap flag를 지원하고 있습니다. config map을 미리 생성해 그것을 mount하고 provisioning된 ingress controller의 몇 가지 설정을 바꿔서 가능하도록 하는 방법입니다. 그 중 redis cluster와 nginx ingress controller간 tcp 통신을 하기 위한 설정방법을 작성해보았습니다.

📔 nginx ingress controller설치

https://kubernetes.github.io/ingress-nginx/deploy/

 

Installation Guide - NGINX Ingress Controller

Installation Guide There are multiple ways to install the NGINX ingress controller: with Helm, using the project repository chart; with kubectl apply, using YAML manifests; with specific addons (e.g. for minikube or MicroK8s). On most Kubernetes clusters,

kubernetes.github.io

상기 link에서는 helm으로 설치하는 방법 외 다음과 같은 명령어로 바로 설치할 수 있도록 하고 있습니다.

 

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.2/deploy/static/provider/cloud/deploy.yaml

 

만약 되지 않는다면 각 k8s cluster version에 맞는 deploy yaml을 apply해야 합니다.

https://github.com/kubernetes/ingress-nginx/tree/main/deploy/static/provider

 

GitHub - kubernetes/ingress-nginx: NGINX Ingress Controller for Kubernetes

NGINX Ingress Controller for Kubernetes. Contribute to kubernetes/ingress-nginx development by creating an account on GitHub.

github.com

상기 link는 여러 환경에서 구성된 k8s cluster에 대한 deploy file들이 있는 곳 입니다. 제 경우에는 git clone 후 해당 k8s환경에서 /cloud/1.22/deploy.yaml을 적용했습니다.

 

deploy.yaml apply시 create 출력문 예시

apply하면 ingress-nginx라는 namespace가 생성되고 다음과 같은 resource들이 만들어집니다.

📔 configmap 생성

redis는 기본적으로 TCP 통신을 사용하기 때문에 cluster와 연결하기 위해 다음 configmap을 apply해줘야 합니다. tab이 yaml내용에 들어가면 apply시 오류가 날 수 있으니 주의해 주세요. {}로 감싸진 부분은 custom이 필요한 부분입니다.

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-tcp-services
  namespace: ingress-nginx
data:
  6379: "{namespace명}/{redis cluster가 외부노출된 service명}:{연결할 port번호}" => 예시

📔 설정 file edit

띄워진 deploy를 직접 수정해줍니다.

📑 deployment 이름 확인

다음 명령어로 nginx controller의 deployment를 확인해줍니다.

kubectl get deploy -n ingress-nginx

 

출력예시

 

📑 deployment edit

다음 명령어로 nginx controller의 deployment manifest에 직접 접속합니다.

kubectl edit deploy ingress-nginx-controller -n ingress-nginx

 

.spec.containers[0].args에 다음 설정을 추가합니다. tab을 사용하지 않도록 주의합니다.

- --tcp-services-configmap=ingress-nginx/redis-tcp-services

 

추가 예시

file write명령어(wq)사용 후 나오게 되면 다음 출력문이 나오는데 이와 같다면 edit성공입니다.

📑 service 이름 확인

다음 명령어로 nginx controller가 노출된 service의 이름을 확인합니다.

kubectl get svc -n ingress-nginx

 

 

출력예시

 

📑 service edit

다음 명령어로 nginx controller의 service manifest에 직접 접속합니다.

kubectl edit svc ingress-nginx-controller -n ingress-nginx

.spec.ports에 array형태로 다음 형식의 문구를 추가하고 file write해줍니다.

  - appProtocol: https
    name: {외부로 노출한 redis cluster service명}
    port: {ingress controller service가 사용할 container port}
    protocol: TCP
    targetPort: {ingress controller service가 routing할 목적지 container port}

제 경우는 port와 targetPort를 7777로 설정했습니다.

 

성공적으로 edit이 되었는지 다음 출력문과 같은지를 비교해줍니다.

📔 pod 삭제

📑 pod이름 확인

다음 명령어로 nginx ingress controller의 pod명을 확인해줍니다.

kubectl get pods -n ingress-nginx

 

출력결과

📑 pod삭제

다음 명령어로 ingress-nginx-controller-xxxxxx-xxxx명의 pod를 삭제해줍니다. 상기 사진에서는 ingress-nginx-controller-55c8c6fb84-dsf85 이름의 pod를 삭제합니다. 삭제 이후에는 deployment가 k8s 정책에 의해 pod를 재기동 해줍니다.

kubectl delete pods {삭제할 nginx ingress controller pod명} -n ingress-nginx

📔 script file 작성, 실행

python pip redis module로 외부에서 ingress로 연결한 후 간단한 set, get함수를 실행하는 script file을 작성합니다.

Redis함수에 들어갈 인자는 host={ingress controller external ip}, port={routing할 container port번호}를 custom해 넘겨줍니다.

 

import redis
r = redis.Redis(host='192.168.9.157', port=7777, db=0)
print(r.set('one', 'two'))
print(r.get('one'))

다음과 같이 실행결과를 출력해줍니다.

실행 결과

📔 redis cluster container 접속

 k8s cluster상에 provisioning된 redis-cluster의 container에 접속해 redis-cli명령어로 해당 key, value값이 적재되었는지 확인해줍니다.


📕참조

https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/

 

Exposing TCP and UDP services - NGINX Ingress Controller

Exposing TCP and UDP services Ingress does not support TCP or UDP services. For this reason this Ingress controller uses the flags --tcp-services-configmap and --udp-services-configmap to point to an existing config map where the key is the external port t

kubernetes.github.io