목차

Kubernetes Service - Ingress

  • Nginx Controller, Envoy Controller, Traefik Ingress Controlle 등
  • AWS Load Balancer Controller(EKS 사용시)
  • Ingress object 및 Controller를 사용 시 서비스당 로드밸런서 하나에서 Ingress당 로드밸런서 하나로 전환하고 여러 서비스로 라우팅이 가능하며 트래픽은 경로기반 라우팅을 사용하여 적적한 서비스로 사용이 가능하다.
  • 외부 트래픽(Internet) → Ingress(routing roule) → one or more Services → one or more web APP Pods → container
    • Ingress는 단일 접점과 서비스 라우팅
    • Serive는 Pod 로드 밸런싱
    • Pod는 비즈니스로직 처리하는 역할
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.0/deploy/static/provider/baremetal/deploy.yaml

#확인
$ kubectl get all -n ingress-nginx
NAME                                            READY   STATUS      RESTARTS   AGE
pod/ingress-nginx-admission-create-2pf9q        0/1     Completed   0          30s
pod/ingress-nginx-admission-patch-nkt6f         0/1     Completed   0          30s
pod/ingress-nginx-controller-5bc4d46fcf-hw8hv   0/1     Running     0          30s

NAME                                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx-controller             NodePort    10.108.80.246    <none>        80:32516/TCP,443:32055/TCP   30s
service/ingress-nginx-controller-admission   ClusterIP   10.108.117.123   <none>        443/TCP                      30s

NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/ingress-nginx-controller   0/1     1            0           30s

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/ingress-nginx-controller-5bc4d46fcf   1         1         0       30s

NAME                                       COMPLETIONS   DURATION   AGE
job.batch/ingress-nginx-admission-create   1/1           9s         30s
job.batch/ingress-nginx-admission-patch    1/1           10s        30s
#service/ingress-nginx-controller             NodePort    10.108.80.246    <none>        80:32516/TCP,443:32055/TCP   30s
#적힌 포트를 통해 통신함 기록 필요
  • - - hello-app.yaml
apiVersion: v1
kind: Pod
metadata:
  name: hello-app-pod
  labels:
    app: hello
spec:
  containers:
    - name: hello-container
      image: dbgurum/ingress:hi
      args:
        - \"-text=Hello, ingress.\"
---
apiVersion: v1
kind: Service
metadata:
  name: hello-app-svc
spec:
  selector:
    app: hello
  ports:
    - port: 5678
  • hello-ing.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: http-hello
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /hello-app
        pathType: Prefix
        backend:
          service:
            name: hello-app-svc
            port:
              number: 5678
#설치
$ kubectl apply -f hello-app.yaml

$ kubectl apply -f hello-ing.yaml

#확인
$ kubectl get po,ing,svc -A -o wide | grep hello
default                pod/hello-app-pod                                1/1     Running     0          82s     10.109.131.10   k8s-node2    <none>           <none>
default     ingress.networking.k8s.io/http-hello   nginx   *       10.10.10.91   80      70s
default                service/hello-app-svc                        ClusterIP   10.105.23.166    <none>        5678/TCP                     82s     app=hello

$ kubectl describe ingress http-hello
Name:             http-hello
Labels:           <none>
Namespace:        default
Address:          10.10.10.91
Ingress Class:    nginx
Default backend:  <default>
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /hello-app   hello-app-svc:5678 (10.109.131.10:5678)
Annotations:  ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age                    From                      Message
  ----    ------  ----                   ----                      -------
  Normal  Sync    2m54s (x2 over 2m56s)  nginx-ingress-controller  Scheduled for sync

#curl 확인
#path: /hello-app
$ curl 10.10.10.91:32516/hello-app
Hello, ingress.
  • - - nginx-app.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: nginx-path
  name: nginx-app-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app-svc
spec:
  selector:
    app: nginx-path
  ports:
    - port: 88
      targetPort: 80
  • nginx-ing.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: http-nginx-name
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: nginx.ingress.k8s
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-app-svc
            port:
              number: 88
~
#설치
$ kubectl apply -f nginx-app.yaml

$ kubectl apply -f nginx-ing.yaml

$ kubectl describe ingress http-nginx-name
Name:             http-nginx-name
Labels:           <none>
Namespace:        default
Address:          10.10.10.91
Ingress Class:    nginx
Default backend:  <default>
Rules:
  Host               Path  Backends
  ----               ----  --------
  nginx.ingress.k8s
                     /   nginx-app-svc:88 (10.109.131.11:80)
Annotations:         ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age                  From                      Message
  ----    ------  ----                 ----                      -------
  Normal  Sync    119s (x2 over 2m4s)  nginx-ingress-controller  Scheduled for sync

#curl 확인
#host를 통한 접근 nginx.ingress.k8s

$ curl 10.10.10.91:32516 -H \"Host: nginx.ingress.k8s\"