DevOps/Kubernetes
Kubernetes - Headless Service
yscho03
2021. 11. 30. 16:19
728x90
반응형
Headless Service
정의
- ClusterIP가 없는 서비스로 단일 진입점이 필요 없을 때 사용
- Service와 연결된 Pod의 endpoint로 DNS 레코드가 생성됨
- 쿠버네티스 coreDNS에 등록되며 DNS resolving Service로 요청이 가능해진다.
- Pod의 DNS 주소: pod-ip-addr.namespace.pod.cluster.local
생성
사전에 Deployment를 먼저 생성하고 그 다음에 Service를 생성하여 묶어준다.
root@master:~# cat > deploy-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webui
spec:
replicas: 3
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
root@master:~# kubectl create -f deploy-nginx.yaml
deployment.apps/webui create
root@master:~# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 1 (6s ago) 107s 10.36.0.2 node1.example.com <none> <none>
webui-6d4c4cc4b8-6mzj8 1/1 Running 0 22m 10.44.0.1 node2.example.com <none> <none>
webui-6d4c4cc4b8-m8g4f 1/1 Running 0 22m 10.36.0.1 node1.example.com <none> <none>
webui-6d4c4cc4b8-vhj96 1/1 Running 0 22m 10.44.0.2 node2.example.com <none> <none>
root@master:~# cat > headless-service.yaml
apiVersion: v1
kind: Service
metadata:
name: headless-service
spec:
type: ClusterIP
clusterIP: None
selector:
app: webui
ports:
- protocol: TCP
port: 80
targetPort: 80
root@master:~# kubectl create -f headless-service.yaml
service/headless-service created
조회
root@master:~# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
headless-service ClusterIP None <none> 80/TCP 9s app=webui
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 117m <none>
root@master:~# kubectl describe svc headless-service
Name: headless-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=webui
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: None
IPs: None
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.36.0.1:80,10.44.0.1:80,10.44.0.2:80
Session Affinity: None
Events: <none>
확인
centos pod를 생성하고 파드 안으로 진입하여 도메인 (pod-ip-addr.namespace.pod.cluster.local) 을 호출해본다.
root@master:~# kubectl run testpod -it --image=centos:7
If you dont see a command prompt, try pressing enter.
[root@testpod /]# cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
[root@testpod /]# curl 10-44-0-01.default.pod.cluster.local
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
728x90
반응형