본문 바로가기

리눅스

쿠버네티스에서 NGINX Ingress Controller를 설정하는 방법

반응형

쿠버네티스에서 NGINX Ingress Controller를 설정하는 방법

NGINX Ingress Controller를 설정하는 방법

NGINX Ingress Controller 배포

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
kubectl get all -l app.kubernetes.io/name=ingress-nginx -n ingress-nginx
$ kubectl get all -l app.kubernetes.io/name=ingress-nginx -n ingress-nginx
NAME                                            READY   STATUS      RESTARTS   AGE
pod/ingress-nginx-admission-create-wzgps        0/1     Completed   0          2m24s
pod/ingress-nginx-admission-patch-7xwf9         0/1     Completed   0          2m24s
pod/ingress-nginx-controller-684995db94-h7hvj   1/1     Running     0          2m24s

NAME                                         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx-controller             LoadBalancer   10.111.88.173    <pending>     80:30893/TCP,443:30671/TCP   2m24s
service/ingress-nginx-controller-admission   ClusterIP      10.107.210.205   <none>        443/TCP                      2m24s

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

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/ingress-nginx-controller-684995db94   1         1         1       2m24s

NAME                                       COMPLETIONS   DURATION   AGE
job.batch/ingress-nginx-admission-create   1/1           66s        2m24s
job.batch/ingress-nginx-admission-patch    1/1           66s        2m24s
kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx
$ kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx
NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-wzgps        0/1     Completed   0          3m11s
ingress-nginx-admission-patch-7xwf9         0/1     Completed   0          3m11s
ingress-nginx-controller-684995db94-h7hvj   1/1     Running     0          3m11s
kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
$ kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
namespace "ingress-nginx" deleted
serviceaccount "ingress-nginx" deleted
serviceaccount "ingress-nginx-admission" deleted
role.rbac.authorization.k8s.io "ingress-nginx" deleted
role.rbac.authorization.k8s.io "ingress-nginx-admission" deleted
clusterrole.rbac.authorization.k8s.io "ingress-nginx" deleted
clusterrole.rbac.authorization.k8s.io "ingress-nginx-admission" deleted
rolebinding.rbac.authorization.k8s.io "ingress-nginx" deleted
rolebinding.rbac.authorization.k8s.io "ingress-nginx-admission" deleted
clusterrolebinding.rbac.authorization.k8s.io "ingress-nginx" deleted
clusterrolebinding.rbac.authorization.k8s.io "ingress-nginx-admission" deleted
configmap "ingress-nginx-controller" deleted
service "ingress-nginx-controller" deleted
service "ingress-nginx-controller-admission" deleted
deployment.apps "ingress-nginx-controller" deleted
job.batch "ingress-nginx-admission-create" deleted
job.batch "ingress-nginx-admission-patch" deleted
ingressclass.networking.k8s.io "nginx" deleted
validatingwebhookconfiguration.admissionregistration.k8s.io "ingress-nginx-admission" deleted

Ingress Controller 테스트

httpd-deployment.yaml 편집

$ vim httpd-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      run: httpd-deployment
  template:
    metadata:
      labels:
        run: httpd-deployment
    spec:
      containers:
      - image: httpd
        name: httpd-webserver

---
apiVersion: v1
kind: Service
metadata:
  name: httpd-service
spec:
  type: NodePort
  selector:
    run: httpd-deployment
  ports:
    - port: 80
   

$ kubectl create -f httpd-deployment.yaml
deployment.apps/httpd-deployment created
service/httpd-service created


$ kubectl get deployments httpd-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
httpd-deployment   3/3     3            3           42s

 

nginx-deployment.yaml 편집

$ vim nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx-deployment
  template:
    metadata:
      labels:
        run: nginx-deployment
    spec:
      containers:
      - image: nginx
        name: nginx-webserver

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    run: nginx-deployment
  ports:
    - port: 80
    

$ kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
service/nginx-service created


$ kubectl get deployments nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           19s

 

Ingress 리소스 생성 및 배포

myweb-ingress.yaml 편집

$ vim myweb-ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: myweb-ingress
  annotations:
    kubernetes.io/ingress.class: myweb-ingress
spec:
  rules:
  - host: a.lb.4wxyz.com
    http:
      paths:
      - path: /
        backend:
          serviceName: httpd-service
          servicePort: 80
      - path: /test
        backend:
          serviceName: nginx-service
          servicePort: 80

  - host: b.lb.4wxyz.com
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 80
  
  
$ kubectl create -f myweb-ingress.yaml
ingress.networking.k8s.io/myweb-ingress created


$ kubectl get ingress myweb-ingress
NAME            CLASS    HOSTS                           ADDRESS   PORTS   AGE
myweb-ingress   <none>   a.lb.4wxyz.com,b.lb.4wxyz.com             80      33s


$ kubectl describe ingress myweb-ingress
Name:             myweb-ingress
Namespace:        default
Address:
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host            Path  Backends
  ----            ----  --------
  a.lb.4wxyz.com
                  /       httpd-service:80   10.244.1.21:80,10.244.2.31:80,10.244.3.27:80)
                  /test   nginx-service:80   10.244.1.22:80,10.244.2.32:80,10.244.3.28:80)
  b.lb.4wxyz.com
                     nginx-service:80   10.244.1.22:80,10.244.2.32:80,10.244.3.28:80)
Annotations:      kubernetes.io/ingress.class: myweb-ingress
Events:           <none>

 

서비스 포트 확인

$ kubectl get service httpd-service nginx-service
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
httpd-service   NodePort   10.97.143.252   <none>        80:30997/TCP   9m33s
nginx-service   NodePort   10.103.154.29   <none>        80:30339/TCP   7m17s

 

웹브라우저에서이 URL에 접근 테스트

http://a.lb.4wxyz.com:30997

 

http://b.lb.4wxyz.com:30339

 

 

원본URL : https://www.linuxtechi.com/setup-nginx-ingress-controller-in-kubernetes/

 

How to Setup NGINX Ingress Controller in Kubernetes

How to Setup NGINX Ingress Controller in Kubernetes by Pradeep Kumar · Updated July 8, 2020 Ingress is one of the important concepts in Kubernetes, which allows external users to access containerized application using FQDN (fully qualified domain name). T

www.linuxtechi.com

 

728x90
반응형