Headless
清单文件
---
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
labels:
app: myapp
release: stable
spec:
selector:
app: myapp
release: stable
# 这里要设置为None,不能是""
clusterIP: None
ports:
- name: myapp
port: 80
protocol: TCP
targetPort: app-port
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
revisionHistoryLimit: 3
selector:
matchLabels:
app: myapp
release: stable
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
name: myapp-container
namespace: default
labels:
app: myapp
release: stable
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
ports:
- name: app-port
containerPort: 80
protocol: TCP
获取无头服务信息
# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d <none>
myapp-svc ClusterIP None <none> 80/TCP 2m app=myapp,release=stable
获取详细信息
# kubectl describe svc myapp-svc
Name: myapp-svc
Namespace: default
Labels: app=myapp
release=stable
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"myapp","release":"stable"},"name":"myapp-svc","namespace":"default"},...
Selector: app=myapp,release=stable
Type: ClusterIP
IP: None
Port: myapp 80/TCP
TargetPort: app-port/TCP
Endpoints: 10.244.1.43:80,10.244.2.132:80,10.244.2.133:80
Session Affinity: None
Events: <none>
获取Pods相关信息
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myapp-deploy-6f96c5bb-d8wkm 1/1 Running 0 12m 10.244.1.43 node002
myapp-deploy-6f96c5bb-mbk46 1/1 Running 0 12m 10.244.2.132 node003
myapp-deploy-6f96c5bb-nz6rf 1/1 Running 0 12m 10.244.2.133 node003
由于是无头服务,也就是没有ClusterIP,所以想要访问就得通过DNS,所以看下DNS解析情况
# dig myapp-svc.default.svc.cluster.local @10.96.0.10
; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7_5.1 <<>> myapp-svc.default.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4995
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;myapp-svc.default.svc.cluster.local. IN A
;; ANSWER SECTION:
myapp-svc.default.svc.cluster.local. 5 IN A 10.244.1.43
myapp-svc.default.svc.cluster.local. 5 IN A 10.244.2.132
myapp-svc.default.svc.cluster.local. 5 IN A 10.244.2.133
访问无头服务,由于DNS缓存,所以请求都到一个Pods上了
# kubectl exec -it client-bbf58867f-tzxhf -- /bin/sh
/ # wget -q -O - http://myapp-svc.default.svc.cluster.local/hostname.html
myapp-deploy-6f96c5bb-d8wkm
/ # wget -q -O - http://myapp-svc.default.svc.cluster.local/hostname.html
myapp-deploy-6f96c5bb-d8wkm