-
Ingress를 이용한 쿠버네티스 연결 기본 실습인프라 2022. 12. 16. 10:48728x90반응형SMALL
이번 포스팅은 쿠버네티스로 각 컴포넌트(frontend, backend)를 연결해본 과정에 대해 작성해보려고 한다.
※ 실습은 ubuntu 20.04 버전 on-premise 서버에서 진행했습니다.
목차
1. ingress 생성 yaml 파일 및 설명
2. ingress controller 생성
3. NodePort 생성
4. 요청 받을 deployment와 svc를 생성
5. 연결 확인
6. 알게된 점
1. ingress 생성
# ingress-example.yaml apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-example annotations: nginx.ingress.kubernetes.io/rewrite-target: / kubernetes.io/ingress.class: "nginx" spec: rules: # host: 해당 도메인 이름으로 접근하는 요청에 대해 처리규칙 적용 - host: cotton.example.com http: paths: # path: 해당 경로에 들어온 요청을 어느 서비스로 전달할 것인지 정의 - path: /test backend: # serviceName, servicePort: path 로 들어온 요청이 전달될 서비스와 포트 serviceName: hostname-service servicePort: 8080
설명
- annotation 안의 내용은 잘은 모르지만 밑의 path로 들어오는 요청을 서비스의 ‘/’로 보내겠다는 뜻인 것 같다.
- host - 해당 도메인 이름으로 접근하는 요청에 대한 처리 규칙을 적용한다.
- /test라는 경로로 요청이 들어올 경우 어느 서비스로 전달할 것인지 정의한다.
- 위와 같은 경우 hostname-service라는 서비스로 8080포트로 전달한다.
2. ingress controller 생성
kubectl apply -f <https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/aws/deploy.yaml>
쿠버네티스에서 제공해주는 nginx ingress관련 yaml파일을 apply해준다.
→nginx ingress관련 리소스들이 생성된다.(ingress-nginx namespace에 pod, deployment, svc가 생성된다. 이 때, svc는 loadbalancer타입이 생성된다.)
온프레미스 환경에서 실습을 진행할 경우 '3. NodePort 생성'을 진행한다.
aws와 같은 클라우드 환경에서 실습을 진행할 경우 '3. NodePort 생성'을 진행하지 않아도 된다.
3. NodePort 생성
(aws 환경에서 실습을 진행한다면 넘어가도 된다.)
apiVersion: v1 kind: Service metadata: name: ingress-nginx-controller-nodeport namespace: ingress-nginx spec: ports: - name: http nodePort: 31000 # 인그레스 url 의 외부포트 port: 8080 # 서비스에 접근하는 포트 targetPort: http # 파드에 설정된 컨테이너 포트 - name: https nodePort: 32000 port: 443 protocol: TCP targetPort: https selector: app.kubernetes.io/component: controller app.kubernetes.io/instance: ingress-nginx app.kubernetes.io/name: ingress-nginx type: NodePort
http 요청은 31000포트와 8080포트를 연결해준다.
https 요청은 32000포트와 443포트를 연결해준다.
앞 서 생성한 ingress nginx의 셀렉터들을 연결해준다.
4. ingress를 통해 외부의 요청을 받을 deployment와 svc를 생성
# hostname-deployment.yaml 22,27 모두 apiVersion: apps/v1 kind: Deployment metadata: name: hostname-deployment spec: replicas: 3 selector: matchLabels: app: webserver template: metadata: name: my-webserver labels: app: webserver spec: containers: - name: my-webserver image: httpd:latest imagePullPolicy: IfNotPresent ports: - containerPort: 80 name: beomseok-port # hostname-service.yaml apiVersion: v1 kind: Service metadata: name: hostname-service spec: ports: - name: web-port port: 8080 targetPort: beomseok-port selector: app: webserver type: ClusterIP
deployment
- pod 3개
- containerPort는 beomseok-port를 이름으로 하고 80번을 열어둔다.
service
- 내부와의 연결만을 다루기 때문에 ClusterIP로 선언해둔 상태이다.
- 8080포트로 연결할 수 있다.
- tergetPort는 deployment의 port와 연결할 계획이므로 beomseok-port와 연결해준다.
5. 연결 확인
sudo vi /etc/hosts에서 host를 저장한다.
<현재 ip> cotton.example.com
크롬에 cotton.example.com:31000/test를 연결해서 it works! 가 뜨면 성공이다.
6. 알게 된 점
- deployment에서 service와 연결할 포트의 이름을 지정해줄 수 있고 service를 생성할 경우 해당 포트를 특정한 넘버가 아닌 이름으로 지정해줄 수 있다.
- 쿠버네티스의 nginx ingress를 사용할 경우 wget을 통해 해당 yaml파일을 가져올 수 있다.
- yaml파일에는 nginx ingress를 위한 여러 리소스들이 선언되어 있다.
- 지정된 리소스들은 namespace, configMap, Service(LoadBalancer), Deployment, … 등이 있다.
- Ingress 설정 yaml파일에 있었던 annotation
- kubernetes.io/ingress.class - 해당 인그레스 규칙을 어떤 인그레스 컨트롤러에 적용할 것인지를 의미
- 쿠버네티스 클러스터 자체에서 기본적으로 사용하도록 설정된 인그레스 컨트롤러가 존재하는 경우가 있는데, 이 경우에는 어떤 인그레스 컨트롤러를 사용할 것인지 반드시 인그레스에 명시해주는 것이 좋다.
- nginx.ingress.kubernetes.io/rewrite-target - 인그레스에 정의된 경로로 들어오는 요청을 rewrite-target에 설정된 경로로 전달한다.ex) 위에서 ‘/test’ 로 접근할 경우 hostname-service에는 ‘/’경로로 전달된다.rewrite target은 Nginx의 캡처 그룹과 함께 사용할 때 유용한 기능이다.(캡처 그룹: 정규표현식 형태로 요청 경로 등의 값을 변수로서 사용할 수 있는 방법)
- 아까의 nginx 설정을 살짝 수정해보면
# ingress-example.yaml apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-example annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 kubernetes.io/ingress.calss: "nginx" spec: rules: # host: 해당 도메인 이름으로 접근하는 요청에 대해 처리규칙 적용 - host: cotton.example.com http: paths: # path: 해당 경로에 들어온 요청을 어느 서비스로 전달할 것인지 정의 - path: /test(/|$)(.*) backend: # serviceName, servicePort: path 로 들어온 요청이 전달될 서비스와 포트 serviceName: hostname-service servicePort: 8080
- path에서 (.*)이라는 Nginx의 정규표현식을 통해 /test/ 뒤에 오는 경로를 얻은 뒤, 이 값을 rewrite-target에서 사용한다.
- 위의 yaml파일을 사용하게 된다면 /test/beomseok으로 접근하면 /beomseok으로 접근할 수 있게 됩니다. (요청 경로를 다시 쓰는(rewrite)하는 것이라고 생각하면 좋다.)
반응형LIST'인프라' 카테고리의 다른 글
서버 안정화를 위한 조사 (0) 2022.12.12 aws 인스턴스 & 도커 자동 시작 (0) 2022.12.12 쿠버네티스 시작 (0) 2022.12.12 dockerfile 관련 정리 (0) 2022.12.09 Nginx Load balancing, 도커 컨테이너 실습 (0) 2022.12.08