Exercise 7.1: Exposing Applications: Expose a Service

  1. Deployment 생성

    kubectl create deploy nginx --image=nginx
  2. Deployment 및 Pod에 부여된 Label 확인

    kubectl get deploy,pod -l app=nginx --show-labels
  3. Service 생성

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: nginx
    EOF
  4. CURL 명령어를 통해서 Service의 Cluster IP로 HTTP 요청

    curl $(kubectl get service nginx -o=jsonpath='{.spec.clusterIP}')
  5. Deployment 생성

    kubectl create deploy httpd --image=httpd
  6. Service 업데이트

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: httpd
    EOF
  7. CURL 명령어를 통해서 Service의 Cluster IP로 HTTP 요청

    curl $(kubectl get service nginx -o=jsonpath='{.spec.clusterIP}')
  8. Service 업데이트

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: nginx
    EOF
  9. CURL 명령어를 통해서 Service의 Cluster IP로 HTTP 요청

    curl $(kubectl get service nginx -o=jsonpath='{.spec.clusterIP}')
  10. Deployment 삭제

    kubectl delete deploy httpd
  11. Service 업데이트

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      type: NodePort
      ports:
      - port: 80
        protocol: TCP
        nodePort: 32000
      selector:
        app: nginx
    EOF
  12. CURL 명령어를 통해서 Service의 Cluster IP로 HTTP 요청

    curl $(kubectl get service nginx -o=jsonpath='{.spec.clusterIP}')
  13. 웹브라우저에서 ANY_NODE_IP:SERVICE_NODE_PORT 로 접속되는지 확인 - 아래 명령어로 주소 확인 가능

    echo "$(curl -s ifconfig.io):$(kubectl get service nginx -o=jsonpath='{.spec.ports[0].nodePort}')"
  14. Service 업데이트

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      type: LoadBalancer
      ports:
      - port: 80
        protocol: TCP
        nodePort: 32000
      selector:
        app: nginx
    EOF
  15. Service 상태 확인

    kubectl get svc nginx
  16. Service에 발생한 Event 확인

    kubectl describe svc nginx
  17. Pod 생성

    cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: alpine
    spec:
      containers:
      - name: alpine
        image: praqma/network-multitool
        command: [ "sleep" ]
        args: [ "infinity" ]
    EOF
  18. 생성한 Pod로 Bash 연결

    kubectl exec -it alpine -- /bin/bash
  19. CURL 명령어로 위에서 생성한 Service의 이름으로 접근 시도

    curl nginx
    curl nginx.default
    curl nginx.default.svc
  20. 위에서 생성한 Service 이름으로 IP 주소 확인

    nslookup nginx
  21. DNS 서버 정보 확인

    dig 
  22. 로컬 DNS 설정 정보 확인

    cat /etc/resolv.conf
  23. DNS 서버 상세 정보 확인

    dig @10.96.0.10 -x 10.96.0.10
  24. Bash 프로세스 종료

    exit
  25. 데모 애플리케이션 배포

    {
        kubectl create ns demo
        kubectl create deploy httpd --image=httpd -n demo
        kubectl expose deploy httpd --port=80 -n demo
    }
  26. alpine Pod로 Bash 연결

    kubectl exec -it alpine -- /bin/bash
  27. CURL 명령어로 위에서 생성한 Service의 이름으로 접근 시도

    curl httpd
    curl httpd.demo
    curl httpd.demo.svc
  28. Bash 프로세스 종료

    exit
  29. 리소스 삭제

    {
        kubectl delete svc nginx
        kubectl delete deploy nginx
        kubectl delete pod alpine
        kubectl delete ns demo
    }

Last updated