服務發(fā)現(xiàn)與負載均衡機制Service實例創(chuàng)建
什么是Service?
Service是邏輯上的一組Pod,一種可以訪問Pod的策略,而且其他Pod可以通過Service訪問到這個Service代理的Pod,可以把它理解為傳統(tǒng)架構中的反向代理。
相對于Pod而言,Service有一個固定的名稱,不會發(fā)生改變,并且提供了負載均衡的功能。
通過Service的定義,可以對客戶端應用屏蔽后端Pod實例數(shù)量及Pod IP地址的變化,通過負載均衡策略實現(xiàn)請求到后端Pod實例的轉發(fā),為客戶端應用提供一個穩(wěn)定的服務訪問入口地址。
Service實現(xiàn)的是微服務架構中的幾個核心功能:全自動的服務注冊、服務發(fā)現(xiàn)、服務負載均衡等。
創(chuàng)建一個Service實例
apiVersion: v1 kind: Service metadata: labels: app: nginx-svc name: nginx-svc spec: ports: - name: http #service端口名稱 port: 80 #service自己的端口 protocol: TCP #支持TCP UDP SCTP等 targetPort: 80 #后端應用接口 - name: https port: 443 protocol: TCP targetPort: 443 selector: app: nginx #這個就是匹配規(guī)則,代理標簽中有nginx的后端服務器 sessionAffinity: None type: ClusterIP
執(zhí)行上面的yaml文件,創(chuàng)建一個service
[root@k8s-master01 ~]# kubectl create -f nginx-svc.yaml service/nginx-svc created [root@k8s-master01 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d nginx-svc ClusterIP 10.110.150.87 <none> 80/TCP,443/TCP 15s
我們通過訪問svc地址就能訪問到后端的nginx
[root@k8s-master01 ~]# curl 10.110.150.87 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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 rel="external nofollow" >nginx.org</a>.<br/> Commercial support is available at <a rel="external nofollow" >nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
- 在同一個namespace中,其他Pod訪問svc只需要curl http://nginx-svc就可以
- 跨namespace的話,需要在svc名稱后加
.namespace名稱
就可以了,使用需謹慎,沒必要不推薦 - 不建議通過IP地址訪問,因為IP不是固定的,刪除或重建后IP會隨機生成
注意,以上內容只能在集群內部訪問
集群外部訪問svc
- 希望在生產中使用某個固定的名稱而非IP地址進行訪問外部的中間件服務
- 希望Service指向另一個namespace中或其他集群中的服務
- 某項目正在遷移至k8s集群,但是一部分服務仍然在集群外部,此時可以使用service代理外部的服務
創(chuàng)建代理外部應用的Service實例
下面我們定義一個外部應用的service
apiVersion: v1 kind: Service metadata: labels: app: nginx-svc-w name: nginx-svc-w spec: ports: - name: http port: 80 protocol: TCP targetPort: 80 # - name: https # port: 443 # protocol: TCP # targetPort: 443 # selector: # app: nginx sessionAffinity: None type: ClusterIP
區(qū)別就是少了這兩個參數(shù):
selector: app: nginx
創(chuàng)建成功后查看,可以發(fā)現(xiàn)雖然創(chuàng)建成功了但是沒有ENDPOINTS
[root@k8s-master01 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d nginx-svc ClusterIP 10.110.150.87 <none> 80/TCP,443/TCP 31m nginx-svc-w ClusterIP 10.110.144.61 <none> 80/TCP 58s [root@k8s-master01 ~]# kubectl get ep NAME ENDPOINTS AGE kubernetes 192.168.10.2:6443,192.168.10.3:6443,192.168.10.4:6443 9d nginx-svc 172.17.125.10:443,172.18.195.22:443,172.17.125.10:80 + 1 more... 31m
接下來我們需要手動創(chuàng)建一個自定義的ENDPOINTS借用存在的ep生成一個新的ep文件
[root@k8s-master01 ~]# kubectl get ep nginx-svc -o yaml > nginx-ep-w.yaml
apiVersion: v1 kind: Endpoints metadata: labels: app: nginx-svc-w name: nginx-svc-w namespace: default subsets: - addresses: - ip: 110.242.68.3 # 代理的外部服務的地址 ports: - name: http port: 80 protocol: TCP
可以看到已經有外部的代理了
[root@k8s-master01 ~]# kubectl create -f nginx-ep-w.yaml endpoints/nginx-svc-w created [root@k8s-master01 ~]# kubectl get ep NAME ENDPOINTS AGE kubernetes 192.168.10.2:6443,192.168.10.3:6443,192.168.10.4:6443 9d nginx-svc 172.17.125.10:443,172.18.195.22:443,172.17.125.10:80 + 1 more... 47m nginx-svc-w 110.242.68.3:80 11s
接下來試試能不能訪問成功
# 直接訪問的話是通的,返回值200 [root@k8s-master01 ~]# curl baidu.com -I HTTP/1.1 200 OK Date: Mon, 14 Feb 2022 01:43:18 GMT Server: Apache Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT ETag: "51-47cf7e6ee8400" Accept-Ranges: bytes Content-Length: 81 Cache-Control: max-age=86400 Expires: Tue, 15 Feb 2022 01:43:18 GMT Connection: Keep-Alive Content-Type: text/html # 通過訪問service的IP可以看到也是通的,返回值200 [root@k8s-master01 ~]# curl 10.110.144.61 -I HTTP/1.1 200 OK Date: Mon, 14 Feb 2022 01:44:20 GMT Server: Apache Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT ETag: "51-47cf7e6ee8400" Accept-Ranges: bytes Content-Length: 81 Cache-Control: max-age=86400 Expires: Tue, 15 Feb 2022 01:44:20 GMT Connection: Keep-Alive Content-Type: text/html
如果業(yè)務變更ep地址變了怎么辦?只需要在ep中將代理的地址更換即可。
比如我們更換一個taobao的地址測試:
# 取出taobao的IP [root@k8s-master01 ~]# ping taobao.com PING taobao.com (140.205.94.189) 56(84) bytes of data. 64 bytes from 140.205.94.189 (140.205.94.189): icmp_seq=1 ttl=128 time=27.4 ms 64 bytes from 140.205.94.189 (140.205.94.189): icmp_seq=2 ttl=128 time=27.4 ms # 修改ep的yaml文件:nginx-ep-w.yaml apiVersion: v1 kind: Endpoints metadata: labels: app: nginx-svc-w name: nginx-svc-w namespace: default subsets: - addresses: - ip: 140.205.94.189 # taobao ports: - name: http port: 80 protocol: TCP # 重新加載 [root@k8s-master01 ~]# kubectl replace -f nginx-ep-w.yaml
訪問測試一下看是否連通:這個返回501是沒問題的。
[root@k8s-master01 ~]# curl 10.110.144.61 -I HTTP/1.1 501 Not Implemented Server: Tengine Date: Mon, 14 Feb 2022 01:39:16 GMT Content-Type: text/html Content-Length: 583 Connection: close
Service反代外部域名
這個不說了,知道怎么配就行了。
Service的yaml文件:
apiVersion: v1 kind: Service metadata: labels: app: nginx-svc-wname name: nginx-svc-wname spec: type: ExternalName externalName: www.baidu.com
然后創(chuàng)建就行了:
kubectl apply -f nginx-svc-wname.yaml
Service 的類型:
ClusterIP
:在集群內部使用,默認
ExternalName
:通過返回定義的CNAME別名
NodePort
:在所有安裝了kube-proxy的節(jié)點上打開一個端口,此端口可以代理至后端Pod,然后集群外部可以使用節(jié)點的IP地址和NodePort端口號訪問到集群Pod服務。端口取值范圍默認30000-32767
LoadBalancer
:使用云服務商提供的負載均衡器公開服務
以上就是服務發(fā)現(xiàn)與負載均衡機制Service實例創(chuàng)建的詳細內容,更多關于服務發(fā)現(xiàn)與負載均衡Service的資料請關注腳本之家其它相關文章!
相關文章
服務器共享文件夾設置軟件、局域網共享文件夾加密工具、文件共享服務器軟件的選擇
這篇文章主要介紹了服務器共享文件夾設置軟件、局域網共享文件夾加密工具、文件共享服務器軟件的選擇,需要的朋友可以參考下2015-12-12服務器封UDP和封國外是什么意思? 封UDP和封海外云服務器選擇!
很多人在租用高防云服務器的時候,都會看到關于防御會有介紹是否封國外、封UDP,封海外就是封住海外的攻擊,而封UDP則使UDP攻擊無效,所以防御上面封UDP的機器比不封UDP的要強一些,下面給大家簡單介紹一下封UDP和封國外是什么意思,有什么用2023-05-05阿里云k8s服務springboot項目應用升級時出現(xiàn)502錯誤
這篇文章主要介紹了阿里云k8s服務springboot項目應用升級時出現(xiàn)502錯誤,需要的朋友可以參考下2022-04-04