티스토리 뷰

k8s

Kubernetes NGINX Ingress Controller 설치

rang-dev 2023. 8. 12. 01:01

요즘 "핵심만 콕 쿠버네티스"라는 책으로 쿠버네티스 공부를 하고 있는데, nginx ingress controller 관련 실습 중에 업데이트가 안된 부분들이 있어서 기록 겸 남겨둔다.

실행 환경

- kubernetes v1.25.4(docker desktop)

namespace 생성

❯ kubectl create ns ctrl                                                                
namespace/ctrl created

nginx ingress controller helm chart 설치

- helm chart

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx-ingress ingress-nginx/ingress-nginx -n ctrl

이미 install 한 상황이라면 아래와 같이 upgrade 명령 실행

helm upgrade --install nginx-ingress ingress-nginx/ingress-nginx -n ctrl

설치가 되면 아래와 같은 설명이 함께 출력되는 것을 확인할 수 있다.

Release "nginx-ingress" has been upgraded. Happy Helming!
NAME: nginx-ingress
LAST DEPLOYED: Fri Aug 11 23:51:41 2023
NAMESPACE: ctrl
STATUS: deployed
...
An example Ingress that makes use of the controller:
  apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
    name: example
    namespace: foo
  spec:
    ingressClassName: nginx
    rules:
      - host: www.example.com
        http:
          paths:
            - pathType: Prefix
              backend:
                service:
                  name: exampleService
                  port:
                    number: 80
              path: /
...

이것을 참고해서 ingress yaml을 작성하게 될 것이다.

먼저 pod와 service가 잘 떴는지 확인한다.

❯ kubectl get pod -n ctrl
NAME                                                      READY   STATUS    RESTARTS   AGE
nginx-ingress-ingress-nginx-controller-65f49c6f65-vd6tz   1/1     Running   0          96s
                                                                                                                                                    23:53:31
❯ kubectl get svc -n ctrl
NAME                                               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
nginx-ingress-ingress-nginx-controller             LoadBalancer   10.100.211.26   localhost     80:31831/TCP,443:32631/TCP   100s
nginx-ingress-ingress-nginx-controller-admission   ClusterIP      10.102.96.145   <none>        443/TCP                      100s

ingress와 연결할 nignx 서비스 생성

❯ kubectl run mynginx --image nginx --expose --port 80              
service/mynginx created
pod/mynginx created
❯ kubectl get pod,svc mynginx                                    
NAME          READY   STATUS    RESTARTS   AGE
pod/mynginx   1/1     Running   0          52m

NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/mynginx   ClusterIP   10.96.225.103   <none>        80/TCP    52m

ingress 리소스 정의

helm chart 설치 후 제공되었던 예시를 참고하여 yaml 파일을 작성하면 아래와 같다. host에는 svc에서 loadbalancer type의 external-ip를 써주면 된다.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mynginx
spec:
  ingressClassName: nginx
  rules:
  - host: localhost
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: mynginx
              port:
                number: 80
          path: /

kubectl apply

❯ kubectl apply -f mynginx-ingress.yaml
ingress.networking.k8s.io/mynginx created

❯ kubectl get ingress                  
NAME      CLASS   HOSTS       ADDRESS   PORTS   AGE
mynginx   nginx   localhost             80      6s

nginx 서비스 연결 확인

❯ curl localhost          
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-seri
...

도메인 기반 라우팅

Pod 생성

❯ kubectl run apache --image httpd --expose --port 80
service/apache created
pod/apache created
                                                                                                                                                        22:05:49
❯ kubectl run nginx --image nginx --expose --port 80
service/nginx created
pod/nginx created

yaml 작성

- domain-based-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: apache-domain
spec:
  ingressClassName: nginx
  rules:
  - host: apache.localhost
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: apache
              port:
                number: 80
          path: /
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
spec:
  ingressClassName: nginx
  rules:
  - host: nginx.localhost
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: nginx
              port:
                number: 80
          path: /
 kubectl apply -f domain-based-ingress.yaml

호출 테스트

❯ curl apache.localhost
<html><body><h1>It works!</h1></body></html>
                                                                                                                                                          22:08:31
❯ curl nginx.localhost
<!DOCTYPE html>
<html>
...

Path 기반 라우팅

yaml 작성

- path-based-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: apache-path
spec:
  ingressClassName: nginx
  rules:
  - host: localhost
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: apache
              port:
                number: 80
          path: /apache
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: nginx
spec:
  ingressClassName: nginx
  rules:
  - host: localhost
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: nginx
              port:
                number: 80
          path: /nginx
 kubectl apply -f path-based-ingress.yaml

호출 테스트

❯ curl localhost/apache
<html><body><h1>It works!</h1></body></html>
                                                                                                                                                          22:25:49
❯ curl localhost/nginx 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
댓글