2023. 5. 15. 00:00ㆍDevOps/Kubernetes
Pod 는 각각이 IP 를 가지고 있고 하나의 pod 안에 여러 container 가 있다면
그 각각의 container 는 localhost 로 통신이 가능하다 (물론 port 는 달라야한다)
이렇게 pod 내에서 통신은 문제 없이 처리된다.
그렇다면 각 pod 간의 통신은 어떻게 할까?
물리적인 기기와 같이 pod 간 통신은 각 pod 가 가지고 있는 ip 를 통해 이루어진다.

그런데 여기에서 문제가 하나 있다.
Pod는 static 하지 않다. 즉 pod 는 언제든지 교환되거나 다시 시작할 수 있다.
이럴경우 Pod 의 IP 는 변하게 된다.
이렇게 pod 의 ip 가 변하게 되므로 이것을 대표하는 역할을 해줄 무언가가 필요하다.
그래서 존재하는 것이 Service 가 되겠다.
kubernetes 에서 pod 를 생성하고 Service Resource 를 통해 label 을 지정해주면
내부의 dns 시스템을 통해서 label (이름) 으로 접근이 가능하다.
service 는 pod 에 대한 대표 ip 가 생성된다.
더쉽게 이야기 한다면 pod 에 loadbalancer 역할을 하는 것이 service 이다.
service 는 request 를 여러 pod (같은 동작을 하는 ) 들에 적절하게 binding 하게 된다.

service 를 만들기 위해 우서 deployment 를 이용해 2개의 replicas 로 pod 를 만들자. (service-test.yaml)
kind: Deployment
apiVersion: apps/v1
metadata:
name: service-test
spec:
replicas: 2
selector:
matchLabels:
app: service_test_pod
template:
metadata:
labels:
app: service_test_pod
spec:
containers:
- name: simple-http
image: python:2.7
imagePullPolicy: IfNotPresent
command: ["/bin/bash"]
args: ["-c", "echo \"<p>Hello from $(hostname)</p>\" > index.html; python -m SimpleHTTPServer 8080"]
ports:
- name: http
containerPort: 8080
그리고 아래를 실행하자
kubectl apply -f service-test.yaml
이제 expose 를 이용해 service 를 만들자
kubectl expose deployment/service-test
이제 위에 service 를 접속하기 위한 client pod 를 생성해 보자.
apiVersion: v1
kind: Pod
metadata:
name: service-test-client
spec:
restartPolicy: Never
containers:
- name: test-client
image: alpine
command: ["/bin/sh"]
args: ["-c", "echo 'GET / HTTP/1.1\r\n\r\n' | nc service-test 8080"]
kubectl apply -f service-test-client.yml
이제 log 를 확인해 보자
kubectl logs service-test-client
그러면 아래와 같은 애요을 볼수 있다.
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/2.7.18
Date: Sun, 14 May 2023 05:28:26 GMT
Content-type: text/html
Content-Length: 47
Last-Modified: Sun, 14 May 2023 05:24:36 GMT
<p>Hello from service-test-cc5bf7c6d-jtq2s</p>
pod 의 ip 를 접근하지 않고 service 를 만들어 service 의 이름으로 접근하였다. (service -test 8080)
이런식으로 service 를 이용하여 pod 간 통신에 활용할 수 있다.
관련영상
'DevOps > Kubernetes' 카테고리의 다른 글
Rancher-Desktop 으로 Kubernetes 사용하기 With WSL (5) - FASTAPI Service 만들기 (0) | 2023.05.22 |
---|---|
Rancher-Desktop 으로 Kubernetes 사용하기 With WSL (2) (0) | 2023.05.01 |
Rancher-Desktop 으로 Kubernetes 사용하기 With WSL (0) | 2023.04.24 |