Exercise 9.1: Deploy a New Service

  1. CP 노드에 연결된 터미널로 이동

  2. Deployment 생성 시도

    cat <<EOF | kubectl apply -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-one
      labels:
        system: secondary
      namespace: accounting
    spec:
      selector:
        matchLabels:
          system: secondary
      replicas: 2
      template:
        metadata:
          labels:
            system: secondary
        spec:
          containers:
          - image: nginx:1.20.1
            imagePullPolicy: Always
            name: nginx
            ports:
            - containerPort: 8080
              protocol: TCP
          nodeSelector:
            system: secondOne
    EOF
  3. Namespace 생성

    kubectl create ns accounting
  4. Deployment 생성

    cat <<EOF | kubectl apply -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-one
      labels:
        system: secondary
      namespace: accounting
    spec:
      selector:
        matchLabels:
          system: secondary
      replicas: 2
      template:
        metadata:
          labels:
            system: secondary
        spec:
          containers:
          - image: nginx:1.20.1
            imagePullPolicy: Always
            name: nginx
            ports:
            - containerPort: 8080
              protocol: TCP
          nodeSelector:
            system: secondOne
    EOF
  5. 생성된 Pod 상태 확인

    kubectl get pod -n accounting
  6. Pod 상세 내용에서 이벤트 확인

    kubectl describe pod POD_NAME -n accounting

    OR

    kubectl describe pod $(kubectl get pod -l system=secondary -o=jsonpath='{.items[0].metadata.name}' -n accounting) -n accounting
  7. 노드에 부여된 Label 확인

    kubectl get node --show-labels
  8. Worker 노드에 Label 부여

    kubectl label node worker system=secondOne 
  9. 노드에 부여된 Label 확인

    kubectl get node --show-labels
  10. Pod가 배포 되는지 확인

    kubectl get pod -n accounting
  11. 위에서 생성한 Deployment에 서비스 생성

    kubectl expose deployment/nginx-one -n accounting
  12. 위에 서비스에 연결된 Endpoints 확인

    kubectl get ep nginx-one -n accounting
  13. CURL 명령어를 통해서 Endpoint 주소로 접근 시도

    curl ENDPOINT_IP:ENDPOINT_PORT

    OR

    curl $(kubectl get ep nginx-one -o=jsonpath='{.subsets[0].addresses[0].ip}:{.subsets[0].ports[0].port}' -n accounting) 
  14. CURL 명령어를 통해서 Endpoint 주소의 80포트로 접근

    curl ENDPOINT_IP:80

    OR

    curl $(kubectl get ep nginx-one -o=jsonpath='{.subsets[0].addresses[0].ip}:80' -n accounting)
  15. Deployment 수정

    cat <<EOF | kubectl apply -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-one
      labels:
        system: secondary
      namespace: accounting
    spec:
      selector:
        matchLabels:
          system: secondary
      replicas: 2
      template:
        metadata:
          labels:
            system: secondary
        spec:
          containers:
          - image: nginx:1.20.1
            imagePullPolicy: Always
            name: nginx
            ports:
            - containerPort: 80
              protocol: TCP
          nodeSelector:
            system: secondOne
    EOF
  16. 생성된 Service의 ClusterIP로 HTTP 요청

    curl $(kubectl -n accounting get svc nginx-one -o=jsonpath='{.spec.clusterIP}')
  17. 생성된 Service 확인

    kubectl -n accounting get svc nginx-one
  18. Port 변경

    kubectl -n accounting patch svc nginx-one --type=json -p='[
    {"op": "replace", "path": "/spec/ports/0/port", "value": 80},
    {"op": "replace", "path": "/spec/ports/0/targetPort", "value": 80}]'
  19. Port가 변경되었는지 확인

    kubectl -n accounting get svc nginx-one
  20. Service의 ClusterIP로 HTTP 요청

    curl $(kubectl -n accounting get svc nginx-one -o=jsonpath='{.spec.clusterIP}')
  21. Iptable의 모든 규칙 확인

    sudo iptables-save
  22. Iptable의 NAT 규칙 확인

    sudo iptables -L -t nat
  23. KUBE-SERVICES 규칙 확인

    sudo iptables -t nat -L KUBE-SERVICES -n  | column -t
  24. 생성된 Service의 ClusterIP 확인

    kubectl -n accounting get svc nginx-one
  25. Service의 Cluster IP로 연결된 규칙의 상세내용 확인

    {
        export SERVICE_CHAIN=$(sudo iptables -t nat -L KUBE-SERVICES -n  | column -t | grep "accounting/nginx-one" | grep -oE "^KUBE-SVC-[A-Z0-9]+")
        sudo iptables -t nat -L $SERVICE_CHAIN -n  | column -t
    }
  26. 위의 명령어로 나온 결과중의 한개의 Chain 규칙 확인

    sudo iptables -t nat -L $(sudo iptables -t nat -L $SERVICE_CHAIN -n  | column -t | grep -oE "^KUBE-SEP-[A-Z0-9]+" | head -1) \
    -n  | column -t
  27. 생성된 Pod의 IP주소 확인

    kubectl -n accounting get pod -l system=secondary -o wide
  28. 생성된 Deployment의 Replica 갯수를 6개로 조정

    kubectl -n accounting scale deployment nginx-one --replicas=6
  29. Service의 Cluster IP로 연결된 규칙의 상세내용 확인

    sudo iptables -t nat -L $SERVICE_CHAIN -n  | column -t
  30. kube-proxy 로그 확인

    kubectl -n kube-system logs ds/kube-proxy
  31. kube-proxy 로그 레벨 변경

    kubectl -n kube-system patch ds kube-proxy --type=json \
    -p='[{"op": "add", "path": "/spec/template/spec/containers/0/command/-", "value": "--v=10"}]'
  32. kube-proxy Pod가 새로 생성되는지 확인

    kubectl -n kube-system get pod -l k8s-app=kube-proxy
  33. 생성된 Deployment의 Replica 갯수를 2개로 조정

    kubectl -n accounting scale deployment nginx-one --replicas=2
  34. Service의 Cluster IP로 연결된 규칙의 상세내용 확인

    sudo iptables -t nat -L $SERVICE_CHAIN -n  | column -t
  35. kube-proxy 로그 확인

    kubectl -n kube-system logs ds/kube-proxy

Last updated