강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của mijungko
mijungko

câu hỏi đã được viết

Bậc thầy Kubernetes cho DevOps

Thực hành điểm cuối giao tiếp bên ngoài bằng ClusterIP

pod 내부에서의 통신

Viết

·

854

0

안녕하세요 실습중 궁금한 내용이 생겨서 질문합니다.

# nslookup 을 통해 naver.com 의 IP 주소 가져오기
mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ nslookup naver.com
Server:         169.254.169.254
Address:        169.254.169.254#53

Non-authoritative answer:
Name:   naver.com
Address: 223.130.200.107
Name:   naver.com
Address: 223.130.195.95
Name:   naver.com
Address: 223.130.195.200
Name:   naver.com
Address: 223.130.200.104

# yaml 파일 생성
mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ cat endpoint.yaml
apiVersion: v1
kind: Service
metadata:
  name: external-service
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

---

apiVersion: v1
kind: Endpoints
metadata:
  name: external-service
subsets:
  - addresses:
    - ip: 223.130.200.107
    - ip: 223.130.195.95
    - ip: 223.130.195.200
    - ip: 223.130.200.104
    ports:
    - port: 80

mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ kubectl create -f endpoint.yaml
service/external-service created
endpoints/external-service created

mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ kubectl get svc
NAME               TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
external-service   ClusterIP   10.8.10.232   <none>        80/TCP    71s
kubernetes         ClusterIP   10.8.0.1      <none>        443/TCP   73s

# 통신 확인을 위한 어플리케이션이 뜨는 pod 생성
ijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ kubectl create deploy --image=gasbugs/http-go http-go2
deployment.apps/http-go2 created

# pod 내부로 들어가서 통신을 해보면, 성공한 것을 볼 수 있다 (302 를 뱉긴하는데 통신 여부만 판단하겠음)
mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ kubectl get pod
NAME                        READY   STATUS         RESTARTS   AGE
http-go2-7f5469bc46-lc5bb   1/1     Running        0          34s
mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ kubectl exec -it http-go2-7f5469bc46-lc5bb -- bash
root@http-go2-7f5469bc46-lc5bb:/usr/src/app# curl external-service
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center> NWS </center>
</body>
</html>

여기까지 따라왔습니다.

그런데, pod 안에서 curl 명령어를 다른 사이트에 날려봐도 200 OK 가 떨어지는데, 이건 왜 이렇게 동작하는 건가요? ㅠㅠ

mijung_ko_11st@cloudshell:~/yaml (crypto-snow-361311)$ kubectl exec -it http-go2-7f5469bc46-lc5bb -- bash
root@http-go2-7f5469bc46-lc5bb:/usr/src/app# curl -v www.11st.co.kr
...
*   Trying 113.217.247.90...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x5603f970bdd0)
* Connected to www.11st.co.kr (113.217.247.90) port 80 (#0)
> GET / HTTP/1.1
> Host: www.11st.co.kr
> User-Agent: curl/7.64.0
> Accept: */*
>
< HTTP/1.1 200
< Date: Sat, 03 Sep 2022 15:26:11 GMT
< Server: Apache
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Type: text/html;charset=UTF-8
< Content-Language: ko-KR
< Set-Cookie: WMONID=DUZRgT4PfJn; Expires=Mon, 04-Sep-2023 00:26:11 GMT; Path=/
< Vary: Accept-Encoding,User-Agent
< Access-Control-Allow-Credentials: true
< Transfer-Encoding: chunked
< Via: STON Edge Server/22.06.1
...

뭔가 pod 에서 외부와의 통신을 위해서 endpoint 리소스를 생성해야 하는데, 그렇다는 것은 이러한 통신은 불가해야하는거 아닐까? 라는 생각이 들어서요 ㅠㅠ

이게 올바른 동작인가요? 아니라면 왜 이럴까요...?

dockerkubernetes

Câu trả lời 2

0

안녕하세요 미정님, endpoint에 대해서 오해를 하신 것 같습니다.

endpoint는 외부와 통신을 위한 것이 아니라 k8s내부 service라는 리소스가 내부 ip만 보는 것이 아니라 외부 ip를 볼 수도 있다는 점을 알려주는 수업내용인 것 같습니다.

k8s내부 pod에서 service를 호출하게 될 경우 endpoint에 설정된 ip를 라운드로빈 방식으로 호출하게 됩니다.

 

수업 초반 ppt에 설명된 내용으로 조금 더 첨언해보면 아래와 같은 상황을 염두할 수 있습니다

dev, alpha, prod cluster가 있을 때 여기에 배포하는 pod가 mysql이라는 이름을 가진 service를 호출하도록 설정 되어 있습니다.
이때 각 클러스터 service의 endpoint를 클러스터에 맞는 DB ip를 넣어주게되면 pod는 수정할 필요없이 각 클러스터가 지정한 DB를 바라보게 되는 것입니다.

이 수업에서는 DB가 k8s외부에 있다고 가정하고 실습을 진행한 것입니다. 그래서 endpoint가 외부 호출을 위한 서비스라고 오해를 하신 것이 아닐까 싶습니다.

0

mijungko님의 프로필 이미지
mijungko
Người đặt câu hỏi

mijung_ko_11st@cloudshell:~ (crypto-snow-361311)$ kubectl get all
NAME                            READY   STATUS             RESTARTS   AGE
pod/http-go2-7f5469bc46-lc5bb   1/1     Running            0          16m

NAME                       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/external-service   ClusterIP   10.8.10.232   <none>        80/TCP    21m
service/kubernetes         ClusterIP   10.8.0.1      <none>        443/TCP   21m

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/http-go2   1/1     1            1           16m

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/http-go2-7f5469bc46   1         1         1       16m
Hình ảnh hồ sơ của mijungko
mijungko

câu hỏi đã được viết

Đặt câu hỏi