티스토리 뷰

<출처>

https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/



이 페이지는 K8S 서비스 오브젝트를 대상으로 외부 IP 주소를 노출키는 방법에 대해 보여준다.





Objectives


  • 5개의 Hello World  애플리케이션을 띄운다.
  • 외부 IP 주소로 노출시는 서비스 오브젝트를 생성한다.
  • 동작 중인 애플리케이션에 접근하기 위해 서비스 오브젝트를 이용한다.



이번 튜토리얼은 Minikube 환경과 On-Premise 로컬 K8S 클러스터 환경에서 진행해봤다. On-Premises(이하 On-Prem.) 의 경우, 원본에서 사용한 "8080" 포트는 이미 사용 중이므로, "5005" 포트로 변경하여 진행하였음을 참고하자. 독자들 또한 상황에 맞게 포트변경이 필요하면 변경하여 진행하면 된다.



Before you begin


  • kubectl 을 설치한다.

  • K8S 클러스터를 생성하기 위해 Google Kubernetes Engine 또는 AWS 와 같은 클라우드 제공사업자를 이용한다. 이 강좌에서는 클라우드 제공사업자가 제공하는 external load balancer 를 생성하게 된다.

  • K8S API 서버와 통신하기 위해 "kubectl" 을 구성한다. 구성에 대한 가이드는 클라우드 제공사업자의 문서를 참조한다.



Creating a service for an application running in five pods


1. 여러분의 클러스터에 Hello World 애플리케이션을 구동 시키자:


- Minikube

1
2
root@zerobig-vm_u:~# kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=8080
deployment.apps/hello-world created
cs


On-Prem.

1
2
[root@docker-registry ~]# kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=5005
deployment.apps/hello-world created
cs

(필자의 On-prem. 환경에서 8080을 이미 사용 중이므로, "5005" 로 변경했다. 독자들도 상황에 맞게 필요에 따라 포트를 변경한다.)


앞선 명령은 디플로이먼트 오브젝트와 연관된 레플리카셋 오브젝트를 생성해준다.  레플리카셋은 5개의 파드를 갖으며 각각은 Hello World 애플리케이션을 실행하도록 동작한다.


2. 디플로이먼트에 대한 정보를 출력해 보자:


- Minikube

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
root@zerobig-vm_u:~# kubectl get deployments hello-world
NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-world   5         5         5            5           4m
root@zerobig-vm_u:~# kubectl describe deployments hello-world
Name:                   hello-world
Namespace:              default
CreationTimestamp:      Sat, 25 Aug 2018 13:14:21 +0900
Labels:                 run=load-balancer-example
Annotations:            deployment.kubernetes.io/revision=1
Selector:               run=load-balancer-example
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=load-balancer-example
  Containers:
   hello-world:
    Image:        gcr.io/google-samples/node-hello:1.0
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   hello-world-5b446dd74b (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  4m    deployment-controller  Scaled up replica set hello-world-5b446dd74b to 5
cs


On-Prem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@docker-registry ~]# kubectl get deployments hello-world
NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-world   5         5         5            5           1m
[root@docker-registry ~]# kubectl get deployments hello-world -o wide
NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS    IMAGES                                 SELECTOR
hello-world   5         5         5            5           1m        hello-world   gcr.io/google-samples/node-hello:1.0   run=load-balancer-example
[root@docker-registry ~]# kubectl describe deployment hello-world
Name:                   hello-world
Namespace:              default
CreationTimestamp:      Sat, 11 Aug 2018 01:07:36 +0900
Labels:                 run=load-balancer-example
Annotations:            deployment.kubernetes.io/revision=1
Selector:               run=load-balancer-example
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=load-balancer-example
  Containers:
   hello-world:
    Image:        gcr.io/google-samples/node-hello:1.0
    Port:         5005/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   hello-world-bd85dd7b (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  1m    deployment-controller  Scaled up replica set hello-world-bd85dd7b to 5
cs



4. 디플로이먼트를 노출하는 서비스 오브젝트를 생성하자:


- Minikube

1
2
root@zerobig-vm_u:~# kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
service/my-service exposed
cs


On-Prem.

1
2
[root@docker-registry ~]# kubectl expose deployment hello-world --port=5005 --target-port=8080 --type=LoadBalancer --name=zerobig-svc
service/zerobig-svc exposed
cs



5. 서비스에 대한 정보를 출력해 보자:


- Minikube

1
2
3
root@zerobig-vm_u:~# kubectl get services/my-service
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
my-service   LoadBalancer   10.111.247.169   <pending>     8080:30816/TCP   8s
cs




On-Prem.

1
2
3
[root@docker-registry ~]# kubectl get services zerobig-svc
NAME          TYPE           CLUSTER-IP     EXTERNAL-IP       PORT(S)          AGE
zerobig-svc   LoadBalancer   10.43.58.217   124.136.171.121   5005:30569/TCP   24s
cs


6. 서비스에 대한 상세 정보를 출력해 보자:


- Minikube

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@zerobig-vm_u:~# kubectl describe services/my-service
Name:                     my-service
Namespace:                default
Labels:                   run=load-balancer-example
Annotations:              <none>
Selector:                 run=load-balancer-example
Type:                     LoadBalancer
IP:                       10.111.247.169
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30816/TCP
Endpoints:                172.17.0.5:8080,172.17.0.6:8080,172.17.0.7:8080 + 2 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
cs


On-Prem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@docker-registry ~]# kubectl describe services zerobig-svc
Name:                     zerobig-svc
Namespace:                default
Labels:                   run=load-balancer-example
Annotations:              <none>
Selector:                 run=load-balancer-example
Type:                     LoadBalancer
IP:                       10.43.58.217
LoadBalancer Ingress:     124.136.171.121
Port:                     <unset>  5005/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30569/TCP
Endpoints:                10.42.252.102:8080,10.42.46.225:8080,10.42.61.215:8080 + 2 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  49s   service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   38s   service-controller  Ensured load balancer
cs


여러분의 서비스에 의해 노출된 외부 IP 주소 ("LoadBalancer Ingress") 에 주목하자. 또한 "Port" 와 "NodePort" 의 값을 주목해 보자. 

(Minikube 의 결과에서는 "LoadBalalncer Ingress" 자체가 없음을 주목하자.)


7. 앞의 출력화면에서, 여러분은 여러 개의 엔드포인트들이 할당된 것을 볼 수 있다: "10.42.252.102:8080,10.42.46.225:8080,10.42.61.215:8080 + 2 more". Hello World 어플리케이션들이 돌고 있는 파드의 내부 주소를 나타낸다. 이들이 파드 주소인지 검증을 위해 다음 명령을 수행해 보자:

(Minikube 의 경우 "172.17.0.5:8080,172.17.0.6:8080,172.17.0.7:8080 + 2 more..." 로 출력되었다.)


On-Prem.

1
2
3
4
5
6
7
root@zerobig-vm_u:~# kubectl get pods -o wide
NAME                           READY     STATUS    RESTARTS   AGE       IP           NODE
hello-world-5b446dd74b-9g7w4   1/1       Running   0          21m       172.17.0.5   minikube
hello-world-5b446dd74b-mc6pk   1/1       Running   0          21m       172.17.0.9   minikube
hello-world-5b446dd74b-nx9s7   1/1       Running   0          21m       172.17.0.7   minikube
hello-world-5b446dd74b-qbzzm   1/1       Running   0          21m       172.17.0.8   minikube
hello-world-5b446dd74b-tfqkk   1/1       Running   0          21m       172.17.0.6   minikube
cs


- On-premise

1
2
3
4
5
6
7
[root@docker-registry ~]# kubectl get pods -o wide
NAME                         READY     STATUS    RESTARTS   AGE       IP              NODE
hello-world-bd85dd7b-75lsr   1/1       Running   0          7m        10.42.88.124    zerodesk-k8s.zerobig.com
hello-world-bd85dd7b-cdlcf   1/1       Running   0          7m        10.42.61.215    zerodesk-k8s.zerobig.com
hello-world-bd85dd7b-n7z78   1/1       Running   0          7m        10.42.252.102   k8s-node1.zerobig.com
hello-world-bd85dd7b-qqz87   1/1       Running   0          7m        10.42.46.225    sptek-devops
hello-world-bd85dd7b-rvhlz   1/1       Running   0          7m        10.42.79.179    k8s-node1.zerobig.com
cs

(참고로, 위 노드 정보를 보면 hello-world 애플리케이션이 여러 노드에 분산되어 배포되어 실행 중임을 확인 할 수 있다. 본 튜토리얼의 범위에서 다루고자 하는 범위는 아니지만 눈 여겨 봐두자. 추후 다른 실습을 통해 다루게 될 것이다.)



8. Hello World 어플리케이션들에 접속하기 위해 외부 IP 주소 ("LoadBalancer Ingress") 를 이용한다:


curl http://<external-ip>:<port>


<external-ip> 는 여러분들의 서비스에 대한 외부 IP 주소 ("LoadBalancer Ingress)이며, <port> 는 여러분의 서비스에 기술된 "Port" 를 의미한다. Minikube 를 사용하는 경우, "minikube service my-service" 를 타이핑하면 자동으로 브라우저가 Hello World 어플리케이션을 띄워줄 것이다.

요청에 대한 응답이 성공적이라면 "hello message" 가 출력될 것이다:



- Minikube

1
2
3
4
root@zerobig-vm_u:~# minikube ip
192.168.99.100
root@zerobig-vm_u:~# curl http://192.168.99.100:30816
Hello Kubernetes!root@zerobig-vm_u:~#
cs


(Vmware 내 Ubuntu 터미널 상에서 다음 명령을 수행한다.)


On-Prem.


Cleaning up


Service 를 제거하기 위해 다음 명령을 입력한다.

1
2
[root@docker-registry ~]# kubectl delete services zero-service
service "my-service" deleted
cs


Hello World 애플리케이션이 돌고 있는 디플로이먼트, 레플리카셋 그리고 파드를 제거하기 위해, 다음 명령을 입력하자:

1
2
[root@docker-registry ~]# kubectl delete deployment hello-world
deployment.extensions "hello-world" deleted
cs



What's next


Learn more about connecting applications with services.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함