인프라/시스템 구축

[ EKS ] Nginx-Ingress

김붕어87 2023. 4. 18. 10:58
반응형
개요
AWS LoadBalancer Controller(ALB)는 AWS에서만 지원 가능합니다.
AWS 외 다른 kubernetes에서 Ingress를 사용하려면 Nginx-Ingress Controller를 이용해야합니다.

AWS LoadBalancer Controller 링크 : https://dongwook35.tistory.com/52

 

 

1. Nginx Ingress 설치

 

  • yaml 설치
# helm repository 추가

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

 

  • helm 설치 (추천)
# helm repository 추가

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install ingress-nginx ingress-nginx/ingress-nginx \
    --version 4.2.5 \
    --set controller.hostNetwork=true \
    --namespace ingress-nginx --create-namespace

 

 

2. Nginx Ingress 확인

kubectl get all -n ingress-nginx

pod/ingress-nginx-admission-create-4ttsx        0/1     Completed   0          20h
pod/ingress-nginx-admission-patch-6f6k8         0/1     Completed   0          20h
pod/ingress-nginx-controller-7dc75c76db-7mg6b   1/1     Running     0          20h

NAME                                         TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                      AGE
service/ingress-nginx-controller             LoadBalancer   xxx               xxx              80:32135/TCP,443:32124/TCP   20h
service/ingress-nginx-controller-admission   ClusterIP      xxx               <none>           443/TCP                      20h

NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/ingress-nginx-controller   1/1     1            1           20h

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/ingress-nginx-controller-7dc75c76db   1         1         1       20h

NAME                                       COMPLETIONS   DURATION   AGE
job.batch/ingress-nginx-admission-create   1/1           18s        20h
job.batch/ingress-nginx-admission-patch    1/1           18s        20h

 

 

 


[ 테스트용 Nginx Pod 설치 ]

 

1. nginx pod 설치

Nginx Ingress 테스트 용도로 nginx pod 설치

 

  • nginx yaml 생성
vi nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    run: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    run: nginx
spec:
  ports:
  - port: 80
  selector:
    run: nginx
 

 

  • nginx 배포
kubectl apply -f nginx.yaml

 

 

2. ingress 설치

 

  • ingress.yaml 생성
vi ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: nginx
  namespace: nginx
spec:
  rules:
  - host: test.xxx.com
    http:
      paths:
      - backend:
          service:
            name: nginx
            port:
              number: 80
        path: /*
        pathType: ImplementationSpecific

 

 

 

 

  • ingress.yaml 배포
kubectl apply -f ingress.yaml


kubectl get ing 
NAME         CLASS    HOSTS                     ADDRESS          PORTS   AGE
nginx        <none>   test.xxx.com                xxx             80      43m

 

 

 

3. 도메인 설정

test.xxx.com으로 접근하면 nginx pod으로 연결될 수 있도록 도메인 설정

 

  1. Nginx-controller 서비스가 연결된 ELB주소 확인
    1. service/ingress-nginx-controller ELB 도메인 주소 확인
    2. xxx.elb.xxx.com
  2. Route53(도메인 구입한 곳)에서 ELB 주소 CNAME 등록
    1. xxx.com 도메인에서 CNAME 레코드 등록
      1. test.xxx.com CNAME xxx.elb.xxx.com” 설정
    2. nslookup test.xxx.com 도메인 내용 확인
  3. curl test.xxx.com 접속 테스트

 

 

 

4. 테스트

 

  • curl 테스트
curl test.xxx.com

... 중간 생략 ...
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
... 중간 생략 ...

 

 

  • curl -H 헤더값 변경으로 테스트
# 첫번째 방법
curl -H "Host: test.xxx.com" xxx.elb.xxx.com


# 두번째 방법
nslookup xxx.elb.xxx.com
ex) IP : 1.1.1.1

curl --resolve test.xxx.com:80:1.1.1.1 http://test.xxx.com

 

 

반응형