TIL

10_3.k8s Service 및 Ingress 컨트롤러 실습

꿀승 2025. 3. 3. 16:00
728x90
반응형
SMALL

학습내용

  1. kubelet 컨테이너 진단
  2. k8s Service
  3. Ingress
  4. 무중단 배포 실습

학습정리

1. kubelet 컨테이너 진단

  • 컨테이너의 상태를 모니터링하고 관리하기 위해 여러 종류의 프로브(probe)를 사용함
  • 프로브의 종류
    • Liveness Probes (라이브니스 프로브스)
      • 컨테이너가 정상적으로 실행되고 있는지 확인합니다.
      • 프로브 실패 시 컨테이너를 재시작합니다.
      • 애플리케이션이 죽었지만 프로세스는 실행 중일 때 감지하여 자동 복구에 도움을 줍니다.
  • Readiness Probes
    • 컨테이너가 트래픽을 받을 준비가 되었는지 확인합니다.
    • 실패하면 서비스에서 해당 컨테이너를 제외합니다.
    • 애플리케이션이 초기화되었거나, 일시적으로 응답할 수 없는 상태일 때 유용합니다.
  • Startup Probes
    • 애플리케이션이 시작하는 데 오래 걸릴 때 사용됩니다.
    • Liveness Probe가 너무 빨리 실패하는 문제를 방지합니다.
    • 성공하면 Liveness Probe가 활성화됩니다.
    • 주로 긴 초기화 작업이 필요한 애플리케이션에서 유용합니다.

2. k8s Service

  • Service 개념

    • Service는 여러개 파드에 접근할 수 있는 하나의 IP를 제공하고, 본질적으로 로드밸런서 역할을 합니다.
    • 파드는 controller가 관리하므로 한군데에 고정해서 실행되지 않고, 클러스터 안을 옮겨 다닙니다.
    • 이 과정에서 노드를 옮기면서 실행되기도 하고 클러스터 안 파드의 IP가 변경되기도 합니다.
  • Service 타입

    • ClusterIP (기본)

      • 개념

        • 클러스터 내부에서만 접근 가능한 내부IP 생성
        • 클러스터 안 노드나 파드에서는 클러스터IP를 이용해서 서비스에 연결된 파드에 접근
        • 클러스터 외부에서는 이용 불가
      • 사용법

        • clusterip.yaml 생성

          apiVersion: v1
          kind: Service
          metadata:
            name: clusterip-service
          spec:
            type: ClusterIP
            selector:
              app: nginx-for-svc
            ports:
            - protocol: TCP
              port: 80
              targetPort: 80
        • 서비스 실행

          # deployment로 서비스에 연결한 Pod 생성
          kubectl create deployment nginx-for-svc --image=nginx --replicas=2 --port=80
          
          # service 클러스터에 적용
          kubectl apply -f clusterip.yaml
          
          # service 생성 확인
          kubectl get svc
        • 엔드포인트 조회

          kubectl describe service clusterip-service
          
          --------------------------------------------------
           kubectl describe service clusterip-service
          Name:                     clusterip-service
          Namespace:                default
          Labels:                   <none>
          Annotations:              <none>
          Selector:                 app=nginx-for-svc
          Type:                     ClusterIP
          IP Family Policy:         SingleStack
          IP Families:              IPv4
          IP:                       10.104.145.125
          IPs:                      10.104.145.125
          Port:                     <unset>  80/TCP
          TargetPort:               80/TCP
          Endpoints:                10.244.0.6:80,10.244.0.5:80
          Session Affinity:         None
          Internal Traffic Policy:  Cluster
          Events:                   <none>
        • 동일한 클러스터에서 접근

          kubectl run -it --image nicolaka/netshoot testnet -- bash
          
          testnet:~# curl 10.104.145.125
          
          <!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-serif; }
          </style>
          </head>
          <body>
          <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>
          
          <p><em>Thank you for using nginx.</em></p>
          </body>
          </html>
        • 결론

          • 외부에서는 접근이 불가 하지만 같은 클러스터내에서는 접근 가능
          • 로컬에서 10.104.145.125 접근 불가 -> 같은 클러스터(minikube)에서 접근 가능
      • NodePort

        • 개념

          • 서비스 하나에 모든 노드의 지정된 포트를 할당
          • 노드의 포트를 사용하므로 클러스터 안 뿐만 아니라 클러스터 외부에서도 접근 가능
        • 사용법

          • nodeport.yaml 작성

            apiVersion: v1
            kind: Service
            metadata:
              name: nodeport-service
            spec:
              type: NodePort
              selector:
                app: nginx-for-svc
              ports:
              - protocol: TCP
                port: 80
                targetPort: 80
                nodePort: 30080
          • 서비스 생성

            # deployment로 서비스에 연결한 Pod 생성
            kubectl create deployment nginx-for-svc --image=nginx --replicas=2 --port=80
            
            # service 클러스터에 적용
            kubectl apply -f nodeport.yaml
            
            # service 생성 확인
            kubectl get svc
            ------------------------------------------------------
            clusterip-service   ClusterIP   10.104.145.125   <none>        80/TCP         83m
            nodeport-service    NodePort    10.98.199.220    <none>        80:30080/TCP   5s
            
            # Minikube의 경우, minikube service <service-name> --url 명령어로 서비스 URL 접근 가능
            minikube service nodeport-service --url
            
            open http://127.0.0.1:50048
      • LoadBalancer

        • 퍼블릭,프라이빗 클라우드,쿠버네티스를 지원하는 로드밸런서를 장비에서 서비스를 외부에 노출하기 위해 사용
        • 클라우드에서 제공하는 로드밸런서와 파드를 연결 후 해당 로드밸런스 IP를 이용해 클러스터 외부에서 파드에 접근

3. Ingress 컨트롤러

  • Ingress는 주로 클러스터 외부에서 내부 파드에 접근할 때 사용하는 방법입니다. HTTP/HTTPS 경로를 관리합니다.

  • 외부 요청을 서비스로 라우팅하며, 보통 호스트나 URL 경로, 다른 조건에 따라 라우팅을 구분합니다.

  • Ingress 기능

    • URL 기반 라우팅
    • 가상 호스팅
    • SSL/TLS 종료
  • minikube Ingress 활성화

    minikube addons enable ingress
    ---------------------------------------------------
    💡  ingress is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
    You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS
    💡  애드온이 활성화된 후 "minikube tunnel"을 실행하면 인그레스 리소스를 "127.0.0.1"에서 사용할 수 있습니다
        ▪ Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.4.4
        ▪ Using image registry.k8s.io/ingress-nginx/controller:v1.11.3
        ▪ Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.4.4
    🔎  ingress 애드온을 확인 중입니다 ...
    🌟  'ingress' 애드온이 활성화되었습니다
  • Ingress 설정예제

    • ingress.yaml 작성

      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:
        name: test
        annotations:
          nginx.ingress.kubernetes.io/rewrite-target: / # / 경로로 redirect 하기
      spec:
        rules:
        - host: foo.bar.com # foo.bar.com/foos1 로 오는 요청을 s1 이라는 서비스의 80 포트로 보내기 
          http:
            paths:
            - path: /foos1
              pathType: Prefix
              backend:
                service:
                  name: s1
                  port:
                    number: 80
            - path: /bars2
              pathType: Prefix
              backend:
                service:
                  name: s2
                  port:
                    number: 80
        - host: bar.foo.com
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: s2
                    port:
                      number: 80
    • 생성한 ingress 클러스터 적용

      kubectl apply -f ingress-basic.yaml
      
      kubectl describe ingress test
      --------------------------------------------
      Name:             test
      Labels:           <none>
      Namespace:        default
      Address:          192.168.49.2
      Ingress Class:    nginx
      Default backend:  <default>
      Rules:
        Host         Path  Backends
        ----         ----  --------
        foo.bar.com
                     /foos1   s1:80 (<error: services "s1" not found>)
                     /bars2   s2:80 (<error: services "s2" not found>)
        bar.foo.com
                     /   s2:80 (<error: services "s2" not found>)
      Annotations:   nginx.ingress.kubernetes.io/rewrite-target: /
      Events:
        Type    Reason  Age                From                      Message
        ----    ------  ----               ----                      -------
        Normal  Sync    64s (x2 over 66s)  nginx-ingress-controller  Scheduled for sync

4. 무중단 배포 실습

  • Deployment
    • deployment.yaml 작성
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: my-app
      spec:
        replicas: 3
        strategy:
          type: RollingUpdate
          rollingUpdate:
            maxSurge: 1
            maxUnavailable: 1
        selector:
          matchLabels:
            app: my-app
        template:
          metadata:
            labels:
              app: my-app
          spec:
            containers:
            - name: my-container
              image: nginx:stable
              ports:
              - containerPort: 80
              readinessProbe:
                httpGet:
                  path: /
                  port: 80
                initialDelaySeconds: 5
                periodSeconds: 5
              livenessProbe:
                httpGet:
                  path: /
                  port: 80
                initialDelaySeconds: 15
                periodSeconds: 20
    • service.yaml 생성
      apiVersion: v1
      kind: Service
      metadata:
        name: my-service
      spec:
        selector:
          app: my-app
        ports:
        - protocol: TCP
          port: 80
          targetPort: 80
        type: NodePort
    • 배포실행
      kubectl apply -f deployment.yaml
      kubectl apply -f service.yaml
    • minikube 서비스 접근
      minikube service my-service
    • 롤링 업데이트
      kubectl set image deployment/my-app my-container=nginx:lastes
    • rollout
      kubectl rollout status deployment/my-app
728x90
반응형
LIST