1 Service 類(lèi)型
在定義Service的時(shí)候可以指定一個(gè)自己需要的類(lèi)型的Service叁熔,如果不指定的話默認(rèn)是ClusterIP類(lèi)型寂纪。
可以使用的服務(wù)類(lèi)型如下:
1固棚、ClusterIP:
通過(guò)集群的內(nèi)部 IP 暴露服務(wù)斧抱,選擇該值评甜,服務(wù)只能夠在集群內(nèi)部可以訪問(wèn)活尊,這也是默認(rèn)的Service類(lèi)型隶校。ClusterIP類(lèi)型的service創(chuàng)建時(shí),k8s會(huì)通過(guò)etcd從可分配的IP池中分配一個(gè)IP蛹锰,該IP全局唯一深胳,且不可修改。所有訪問(wèn)該IP的請(qǐng)求铜犬,都會(huì)被iptables轉(zhuǎn)發(fā)到后端的endpoints中舞终。
2、NodePort:
通過(guò)每個(gè) Node 節(jié)點(diǎn)上的 IP 和靜態(tài)端口(NodePort)暴露服務(wù)癣猾。NodePort 服務(wù)會(huì)路由到 ClusterIP 服務(wù)敛劝,這個(gè) ClusterIP 服務(wù)會(huì)自動(dòng)創(chuàng)建。通過(guò)請(qǐng)求 :纷宇,可以從集群的外部訪問(wèn)一個(gè) NodePort 服務(wù)夸盟。
3、LoadBalancer:
需要外部支持(GCP and Azure)像捶,用戶(hù)訪問(wèn)service.spec.external-ip,該IP對(duì)應(yīng)到一個(gè)外部負(fù)載均衡的vip上陕,外部服務(wù)對(duì)這個(gè)vip的請(qǐng)求,會(huì)被loadbalancer通過(guò)健康檢查和轉(zhuǎn)發(fā)拓春,發(fā)送到一個(gè)運(yùn)行著該服務(wù)pod的node上释簿,并同樣通過(guò)nodePort里的端口映射,發(fā)送給容器硼莽。
4庶溶、ExternalName:
用戶(hù)可以指定一個(gè)任意的名字,作為該service被解析的CNAME,這種類(lèi)型的servcie不用指定clusterIP,因此kube-proxy不會(huì)管理這類(lèi)service偏螺,這類(lèi)service需要使用1.7版本以上的kubedns行疏。
2 Cluster IP
(1)創(chuàng)建whoami-deployment.yaml文件
vim whoami-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: whoami-deployment
? labels:
? ? app: whoami
spec:
? replicas: 3
? selector:
? ? matchLabels:
? ? ? app: whoami
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: whoami
? ? spec:
? ? ? containers:
? ? ? - name: whoami
? ? ? ? image: jwilder/whoami
? ? ? ? ports:
? ? ? ? - containerPort: 8000
(2)運(yùn)行yaml文件并查看pod以及service
[root@henry001 network]# kubectl apply -f whoami-deployment.yaml
deployment.apps/whoami-deployment created
`查看生成的pod詳細(xì)信息`
[root@henry001 network]# kubectl get pods -o wide
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE? ? IP? ? ? ? ? ? ? ? NODE? ? ? NOMINATED NODE? READINESS GATES
whoami-deployment-678b64444d-lgvck? 1/1? ? Running? 0? ? ? ? ? 28s? ? 192.168.254.207? henry003? <none>? ? ? ? ? <none>
whoami-deployment-678b64444d-mszwp? 1/1? ? Running? 0? ? ? ? ? 28s? ? 192.168.217.18? ? henry002? <none>? ? ? ? ? <none>
whoami-deployment-678b64444d-xz66k? 1/1? ? Running? 0? ? ? ? ? 28s? ? 192.168.254.208? henry003? <none>? ? ? ? ? <none>
`查看service,此時(shí)并未有whoami相關(guān)的service`
[root@henry001 network]# kubectl get svc
NAME? ? ? ? TYPE? ? ? ? CLUSTER-IP? EXTERNAL-IP? PORT(S)? AGE
kubernetes? ClusterIP? 10.96.0.1? ? <none>? ? ? ? 443/TCP? 23h
(3)在集群內(nèi)正常訪問(wèn)
curl 192.168.254.207:8000
curl 192.168.217.18:8000
curl 192.168.254.208:8000
(4)創(chuàng)建whoami的service
注意:該地址只能在集群內(nèi)部訪問(wèn)
`為deployment的whoami-deployment創(chuàng)建service`
[root@henry001 network]# kubectl expose deployment whoami-deployment
service/whoami-deployment exposed
`查看service`
[root@henry001 network]# kubectl get svc
NAME? ? ? ? ? ? ? ? TYPE? ? ? ? CLUSTER-IP? ? ? EXTERNAL-IP? PORT(S)? ? AGE
kubernetes? ? ? ? ? ClusterIP? 10.96.0.1? ? ? <none>? ? ? ? 443/TCP? ? 24h
whoami-deployment? ClusterIP? 10.97.233.149? <none>? ? ? ? 8000/TCP? 12s
**可以發(fā)現(xiàn)有一個(gè)Cluster IP類(lèi)型的service砖茸,名稱(chēng)為whoami-deployment隘擎,IP地址為10.97.233.149
(5)通過(guò)Service的Cluster IP訪問(wèn)
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-xz66k
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-lgvck
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-mszwp
(6)具體查看一下whoami-deployment的詳情信息,發(fā)現(xiàn)有一個(gè)Endpoints連接了具體3個(gè)Pod
[root@henry001 network]# kubectl describe svc whoami-deployment
Name:? ? ? ? ? ? ? whoami-deployment
Namespace:? ? ? ? default
Labels:? ? ? ? ? ? app=whoami
Annotations:? ? ? <none>
Selector:? ? ? ? ? app=whoami
Type:? ? ? ? ? ? ? ClusterIP
IP:? ? ? ? ? ? ? ? 10.97.233.149
Port:? ? ? ? ? ? ? <unset>? 8000/TCP
TargetPort:? ? ? ? 8000/TCP
Endpoints:? ? ? ? 192.168.217.18:8000,192.168.254.207:8000,192.168.254.208:8000
Session Affinity:? None
Events:? ? ? ? ? ? <none>
(7)下面通過(guò)deployment對(duì)whoami擴(kuò)容成5個(gè)
kubectl scale deployment whoami-deployment --replicas=5
`查看pod,已經(jīng)擴(kuò)容為5個(gè)`
[root@henry001 network]# kubectl get pods
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE? ? IP? ? ? ? ? ? ? ? NODE? ? ? NOMINATED NODE? READINESS GATES
whoami-deployment-678b64444d-lgvck? 1/1? ? Running? 0? ? ? ? ? 23m? ? 192.168.254.207? henry003? <none>? ? ? ? ? <none>
whoami-deployment-678b64444d-mszwp? 1/1? ? Running? 0? ? ? ? ? 23m? ? 192.168.217.18? ? henry002? <none>? ? ? ? ? <none>
whoami-deployment-678b64444d-q4wzx? 1/1? ? Running? 0? ? ? ? ? 4m50s? 192.168.254.209? henry003? <none>? ? ? ? ? <none>
whoami-deployment-678b64444d-xz66k? 1/1? ? Running? 0? ? ? ? ? 23m? ? 192.168.254.208? henry003? <none>? ? ? ? ? <none>
whoami-deployment-678b64444d-zj82s? 1/1? ? Running? 0? ? ? ? ? 4m50s? 192.168.217.19? ? henry002? <none>? ? ? ? ? <none>
(8)再次訪問(wèn):curl 10.97.233.149:8000
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-mszwp
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-xz66k
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-q4wzx
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-zj82s
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-xz66k
[root@henry001 network]# curl 10.97.233.149:8000
I'm whoami-deployment-678b64444d-lgvck
(9)再次查看service具體信息:kubectl describe svc whoami-deployment
[root@henry001 network]# kubectl describe svc whoami-deployment
Name:? ? ? ? ? ? ? whoami-deployment
Namespace:? ? ? ? default
Labels:? ? ? ? ? ? app=whoami
Annotations:? ? ? <none>
Selector:? ? ? ? ? app=whoami
Type:? ? ? ? ? ? ? ClusterIP
IP:? ? ? ? ? ? ? ? 10.97.233.149
Port:? ? ? ? ? ? ? <unset>? 8000/TCP
TargetPort:? ? ? ? 8000/TCP
Endpoints:? ? ? ? 192.168.217.18:8000,192.168.217.19:8000,192.168.254.207:8000 + 2 more...? ? #這里有5個(gè)pod的信息
Session Affinity:? None
Events:? ? ? ? ? ? <none>
(10)其實(shí)對(duì)于Service的創(chuàng)建凉夯,不僅僅可以使用kubectl expose货葬,也可以定義一個(gè)yaml文件
apiVersion: v1
kind: Service
metadata:
? name: my-service
spec:
? selector:
? ? app: MyApp
? ports:
? ? - protocol: TCP
? ? ? port: 80
? ? ? targetPort: 9376
? type: Cluster
總結(jié):其實(shí)Service存在的意義就是為了Pod的不穩(wěn)定性,而上述探討的就是關(guān)于Service的一種類(lèi)型Cluster IP劲够,只能供集群內(nèi)訪問(wèn)震桶。
3 NodePort
因?yàn)橥獠磕軌蛟L問(wèn)到集群的物理機(jī)器IP,所以就是在集群中每臺(tái)物理機(jī)器上暴露一個(gè)相同的IP征绎,從給定的配置范圍內(nèi)(默認(rèn):30000-32767)分配端口
(1)根據(jù)whoami-deployment.yaml創(chuàng)建pod
apiVersion: apps/v1
kind: Deployment
metadata:
? name: whoami-deployment
? labels:
? ? app: whoami
spec:
? replicas: 3
? selector:
? ? matchLabels:
? ? ? app: whoami
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: whoami
? ? spec:
? ? ? containers:
? ? ? - name: whoami
? ? ? ? image: jwilder/whoami
? ? ? ? ports:
? ? ? ? - containerPort: 8000
(2)創(chuàng)建NodePort類(lèi)型的service蹲姐,名稱(chēng)為whoami-deployment
`查看service`
[root@henry001 network]# kubectl get svc
NAME? ? ? ? ? ? ? ? TYPE? ? ? ? CLUSTER-IP? ? ? EXTERNAL-IP? PORT(S)? ? AGE
kubernetes? ? ? ? ? ClusterIP? 10.96.0.1? ? ? <none>? ? ? ? 443/TCP? ? 41h
whoami-deployment? ClusterIP? 10.97.233.149? <none>? ? ? ? 8000/TCP? 17h
`刪除ClusterIP類(lèi)型的service`
[root@henry001 network]# kubectl delete svc whoami-deployment
service "whoami-deployment" deleted
`創(chuàng)建NodePort類(lèi)型的service`
[root@henry001 network]# kubectl expose deployment whoami-deployment --type=NodePort
service/whoami-deployment exposed
[root@henry001 network]# kubectl get svc
NAME? ? ? ? ? ? ? ? TYPE? ? ? ? CLUSTER-IP? ? ? EXTERNAL-IP? PORT(S)? ? ? ? ? AGE
kubernetes? ? ? ? ? ClusterIP? 10.96.0.1? ? ? <none>? ? ? ? 443/TCP? ? ? ? ? 41h
whoami-deployment? NodePort? ? 10.103.129.91? <none>? ? ? ? 8000:31999/TCP? 3s
(3)注意上述的端口31999,實(shí)際上就是暴露在集群中物理機(jī)器上的端口
`在每臺(tái)機(jī)器上查看一下31999端口監(jiān)聽(tīng)情況`
[root@henry001 network]# lsof -i tcp:31999
COMMAND? ? PID USER? FD? TYPE? DEVICE SIZE/OFF NODE NAME
kube-prox 11693 root? 11u? IPv6 15525671? ? ? 0t0? TCP *:31999 (LISTEN)
[root@henry001 network]# netstat -nlp |grep 31999
tcp6? ? ? 0? ? ? 0 :::31999? ? ? ? ? ? ? ? :::*? ? ? ? ? ? ? ? ? ? LISTEN? ? ? 11693/kube-proxy
(4)瀏覽器通過(guò)物理機(jī)器的IP訪問(wèn)
用service訪問(wèn)的時(shí)候端口號(hào)為8000人柿;
使用內(nèi)網(wǎng)Ip或者外網(wǎng)Ip訪問(wèn)時(shí)需要用端口號(hào)31999
`master節(jié)點(diǎn)`
[root@henry001 network]# curl 192.168.0.8:31999
I'm whoami-deployment-678b64444d-q9mzt
[root@henry001 network]# curl 10.103.129.91:8000
I'm whoami-deployment-678b64444d-q9mzt
`henry002節(jié)點(diǎn)`
[root@henry002 ~]# curl 10.103.129.91:8000
I'm whoami-deployment-678b64444d-89dzd
[root@henry002 ~]# curl 192.168.0.8:31999
I'm whoami-deployment-678b64444d-q9mzt
`henry003節(jié)點(diǎn)`
[root@henry003 ~]# curl 10.103.129.91:8000
I'm whoami-deployment-678b64444d-d7w64
[root@henry003 ~]# curl 192.168.0.7:31999
I'm whoami-deployment-678b64444d-89dzd
`集群外機(jī)器`
[root@w1 ~]# curl 182.92.168.144:31999
I'm whoami-deployment-678b64444d-d7w64
使用瀏覽器訪問(wèn):
總結(jié):NodePort雖然能夠?qū)崿F(xiàn)外部訪問(wèn)Pod的需求柴墩,但這種方法有許多缺點(diǎn):
1.每個(gè)端口只能是一種服務(wù)
2.端口范圍只能是 30000-32767
3.如果節(jié)點(diǎn)/VM 的 IP 地址發(fā)生變化,你需要能處理這種情況
基于以上原因凫岖,我不建議在生產(chǎn)環(huán)境上用這種方式暴露服務(wù)江咳。如果你運(yùn)行的服務(wù)不要求一直可用,或者對(duì)成本比較敏感哥放,你可以使用這種方法歼指。這樣的應(yīng)用的最佳例子是 demo 應(yīng)用,或者某些臨時(shí)應(yīng)用甥雕。
因篇幅太長(zhǎng)分為兩章來(lái)寫(xiě)踩身。