Service
Headless, Endpoint, ExternalName
Dns
Headless
External
Headless
1-1) Service
apiVersion: v1
kind: Service
metadata:
name: clusterip1
spec:
selector:
svc: clusterip
ports:
- port: 80
targetPort: 8080
1-2) Pod
apiVersion: v1
kind: Pod
metadata:
name: pod1
labels:
svc: clusterip
spec:
containers:
- name: container
image: kubetm/app
1-3) Request Pod
apiVersion: v1
kind: Pod
metadata:
name: request-pod
spec:
containers:
- name: container
image: kubetm/init
kubectl exec request-pod -it /bin/bash
nslookup
nslookup clusterip1
nslookup clusterip1.default.svc.cluster.local
curl
curl clusterip1/hostname
curl clusterip1.default.svc.cluster.local/hostname
2-1) Service
apiVersion: v1
kind: Service
metadata:
name: headless1
spec:
selector:
svc: headless
ports:
- port: 80
targetPort: 8080
clusterIP: None
2-2) Pod
apiVersion: v1
kind: Pod
metadata:
name: pod4
labels:
svc: headless
spec:
hostname: pod-a
subdomain: headless1
containers:
- name: container
image: kubetm/app
Nslookup
nslookup headless1
nslookup pod-a.headless1
nslookup pod-b.headless1
Curl
curl pod-a.headless1:8080/hostname
curl pod-b.headless1:8080/hostname
Endpoint
3-1) Service
apiVersion: v1
kind: Service
metadata:
name: endpoint1
spec:
selector:
svc: endpoint
ports:
- port: 8080
3-2) Pod
apiVersion: v1
kind: Pod
metadata:
name: pod7
labels:
svc: endpoint
spec:
containers:
- name: container
image: kubetm/app
kubectl describe endpoints endpoint1
4-1) Service
apiVersion: v1
kind: Service
metadata:
name: endpoint2
spec:
ports:
- port: 8080
4-2) Pod
apiVersion: v1
kind: Pod
metadata:
name: pod9
spec:
containers:
- name: container
image: kubetm/app
4-3) Endpoint
apiVersion: v1
kind: Endpoints
metadata:
name: endpoint2
subsets:
- addresses:
- ip: 20.109.5.12
ports:
- port: 8080
curl endpoint2:8080/hostname
5-1) Service
apiVersion: v1
kind: Service
metadata:
name: endpoint3
spec:
ports:
- port: 80
Github - Ip Address
nslookup https://www.github.com
curl -O 185.199.110.153:80/kubetm/kubetm.github.io/blob/master/documents/intermediate/Service-Headless_ExternalName.pdf
5-2) Endpoint
apiVersion: v1
kind: Endpoints
metadata:
name: endpoint3
subsets:
- addresses:
- ip: 185.199.110.153
ports:
- port: 80
curl -O endpoint3/kubetm/kubetm.github.io/blob/master/documents/intermediate/Service-Headless_ExternalName.pdf
ExternalName
6-1) Service
apiVersion: v1
kind: Service
metadata:
name: externalname1
spec:
type: ExternalName
externalName: github.github.io
curl -O externalname1/kubetm/kubetm.github.io/blob/master/documents/intermediate/Service-Headless_ExternalName.pdf
yaml
Service
apiVersion: v1
kind: Service
metadata:
name: headless1
spec:
selector: # 생략시 Endpoints 직접 생성해서 사용
svc: headless
ports:
- port: 80
targetPort: 8080
clusterIP: None # headless 서비스
type: ExternalName # ExternalName Service 설정시 사용
externalName: github.github.io # ExternalName사용시 연결 Domain지정
Endpoints
apiVersion: v1
kind: Endpoints
metadata:
name: headless1 # Service의 이름과 동일하게 지정
subsets:
- addresses:
- ip: 20.109.5.12 # Pod의 ClusterIp
ports:
- port: 8080 # Pod의 Container Port
Pod
apiVersion: v1
kind: Pod
metadata:
name: pod4
labels:
svc: headless
spec:
hostname: pod-a # 호스트네임 설정, 생략시 Pod Name이 적용됨
subdomain: headless1 # headless 서비스 사용시 Service의 이름으로 지정
containers:
- name: container
image: kubetm/app
kubectl
Exec
# Pod이름이 request-pod인 Container로 들어가기 (나올땐 exit)
kubectl exec request-pod -it /bin/bash
Describe
# Endpoints 상세보기
kubectl describe endpoints endpoint1
Tips
Dns
- Kubernetes 버전 1.11 이전의 Kubernetes DNS 서비스는 kube-dns를 기반
- 버전 1.11은 kube-dns의 일부 보안 및 안정성 문제를 해결하기 위해 CoreDNS 를 도입
Referenece
Kubernetes
- DNS for Services and Pods : https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/
- Customizing DNS Service : https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/
Others
- Kubernetes DNS-Based Service Discovery : https://github.com/kubernetes/dns/blob/master/docs/specification.md
- Kubernetes best practices: mapping external services : https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services