본문 바로가기
Cloud/Kubernetes

kubernetes환경에 nginx ingress 설치하기

by _Nate 2023. 4. 12.
반응형

Nginx Ingress는 Kubernetes환경에서 Application load balancer의 역할을 한다.

 

이번 포스팅에서는 Kubernetes 환경에 Nginx Ingress를 설치하고 간단한 예시를 통해 동작방식을 설명한다.


1. Nginx Ingress 설치

  • helm을 통해 간단하게 설치가 완료된다.
helm repo add nginx-stable https://helm.nginx.com/stable
helm repo update
  • nginx repo를 추가 한다.

 

test@DESKTOP-F55SF6V:~/ing (⎈|test-cluster:default)# helm install nginx-ingress nginx/nginx-ingress -n kube-system
NAME: nginx-ingress
LAST DEPLOYED: Wed Apr 12 20:51:04 2023
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The NGINX Ingress Controller has been installed.


test@DESKTOP-F55SF6V:~/ing (⎈|test-cluster:default)# k get svc -n kube-system
NAME                                TYPE           CLUSTER-IP      EXTERNAL-IP                                PORT(S)
nginx-ingress-controller            LoadBalancer   10.100.21.211   a7d36...ap-northeast-2.elb.amazonaws.com   80:31514/TCP,443:30816/TCP


test@DESKTOP-F55SF6V:~/ing (⎈|test-cluster:default)# k get po -n kube-system
NAME                                            READY   STATUS    RESTARTS        AGE
nginx-ingress-controller-6fc8889877-fdwsp       1/1     Running   0               116s
  • helm install 후 controller deploy와 service가 생성된 것을 확인한다.

 

2. Ingress 설정 및 sample application 호출

  • sample deploy와 svc를 생성한다.
  • nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80
반응형
  • nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

 

  • sample nginx를 호출할 Ingress resource를 생성한다.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
  name: test-ing
  namespace: test
spec:
  ingressClassName: nginx
  rules:
  - host: example.test.com
    http:
      paths:
      - backend:
          service:
            name: nginx-svc
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific
  • ingressClassName에 nginx를 지정해 줘야 Nginx Ingress Controller에서 해당 Ingress resource가 생성된다.

 

  • test환경이기 때문에 example.test.com을 hosts파일에 넣고 nginx-ingress-controller service에 port forward하여 호출한다.
test@DESKTOP-F55SF6V:~/ (⎈|test-cluster:default)# k port-forward -n kube-system svc/nginx-ingress-controller 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80

 

 

 

3. 여러 기능 사용하기

 

Advanced Configuration with Annotations | NGINX Ingress Controller

Advanced Configuration with Annotations This document explains how to use advanced features using annotations. The Ingress resource only allows you to use basic NGINX features – host and path-based routing and TLS termination. Thus, advanced features lik

docs.nginx.com

 

4. nginx controller 살펴보기

  • 위에 언급한 바와 같이 Nginx Ingress Controller는 Nginx를 kubernetes환경에서 사용할 수 있게 만들어 둔 버전 정도로 생각할 수 있다.
  • Ingress resource가 생성되면 Nginx Ingress Controller pod내부에 관련 config파일이 생성되고 Nginx가 해당 config파일을 동적으로 로딩한다.
  • 위에서 생성한 test-ing Ingress를 기준으로 살펴보겠다.
test@DESKTOP-F55SF6V:~/ (⎈|test-cluster:default)# k exec -ti -n kube-system nginx-ingress-controller-6fc8889877-fdwsp -- /bin/bash
nginx@nginx-ingress-controller-6fc8889877-fdwsp:/$
  • nginx-ingress-controller pod내부로 들어왔다.

 

nginx@nginx-ingress-controller-6fc8889877-fdwsp:/etc/nginx/conf.d$ pwd
/etc/nginx/conf.d
nginx@nginx-ingress-controller-6fc8889877-fdwsp:/etc/nginx/conf.d$ ls -arlt
-rw-r--r-- 1 nginx nginx 1032 Apr 12 12:17 test-test-ing.conf
drwxr-xr-x 1 nginx root    80 Apr 12 12:17 ..
  • /etc/nginx/conf.d 디렉토리 아래 위에서 생성한 test-ing에 대한 conf파일이 존재한다.

 

upstream test-test-ing-example.test.com-nginx-svc-80 {
        server 172.10.4.198:80 max_fails=1 fail_timeout=10s max_conns=0;
}
server {
        listen 80;
        server_name example.test.com;
        set $resource_type "ingress";
        set $resource_name "test-ing";
        set $resource_namespace "test";

        location / {
                set $service "nginx-svc";
                proxy_pass http://test-test-ing-example.test.com-nginx-svc-80;
        }
}
  • 내용을 보면 test-test-ing-example.test.com-nginx-svc-80으로 proxy하도록 되어있으며
    test-test-ing-example.test.com-nginx-svc-80은 172.10.4.198:80 에 맵핑되어 있다.

 

test@DESKTOP-F55SF6V:~ (⎈|test-cluster:default)# k get po -n test -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP   
nginx-deploy-544dc8b7c4-j64gs   1/1     Running   0          15m   172.10.4.198
  • 172.10.4.198은 sample pod의 IP주소이며 80포트는 container의 port이다.
  • Ingress resource를 읽어 nginx에 conf파일이 로딩되면 해당 conf파일을 기준으로 nginx가 proxy를 해주는 형태이다.

구성이 간편하고 트러블슈팅하기에도 nginx ingress 가 좋은 것 같다.

이 후 포스팅에서는 HTTPS통신을 위한 TLS설정을 설명하겠다.

반응형