網站首頁 編程語言 正文
什么是Service?
Service是邏輯上的一組Pod,一種可以訪問Pod的策略,而且其他Pod可以通過Service訪問到這個Service代理的Pod,可以把它理解為傳統架構中的反向代理。
相對于Pod而言,Service有一個固定的名稱,不會發生改變,并且提供了負載均衡的功能。
通過Service的定義,可以對客戶端應用屏蔽后端Pod實例數量及Pod IP地址的變化,通過負載均衡策略實現請求到后端Pod實例的轉發,為客戶端應用提供一個穩定的服務訪問入口地址。
Service實現的是微服務架構中的幾個核心功能:全自動的服務注冊、服務發現、服務負載均衡等。
創建一個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 #這個就是匹配規則,代理標簽中有nginx的后端服務器 sessionAffinity: None type: ClusterIP
執行上面的yaml文件,創建一個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.1443/TCP 9d nginx-svc ClusterIP 10.110.150.87 80/TCP,443/TCP 15s
我們通過訪問svc地址就能訪問到后端的nginx
[root@k8s-master01 ~]# curl 10.110.150.87Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
- 在同一個namespace中,其他Pod訪問svc只需要curl http://nginx-svc就可以
- 跨namespace的話,需要在svc名稱后加
.namespace名稱
就可以了,使用需謹慎,沒必要不推薦 - 不建議通過IP地址訪問,因為IP不是固定的,刪除或重建后IP會隨機生成
注意,以上內容只能在集群內部訪問
集群外部訪問svc
- 希望在生產中使用某個固定的名稱而非IP地址進行訪問外部的中間件服務
- 希望Service指向另一個namespace中或其他集群中的服務
- 某項目正在遷移至k8s集群,但是一部分服務仍然在集群外部,此時可以使用service代理外部的服務
創建代理外部應用的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
區別就是少了這兩個參數:
selector: app: nginx
創建成功后查看,可以發現雖然創建成功了但是沒有ENDPOINTS
[root@k8s-master01 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1443/TCP 9d nginx-svc ClusterIP 10.110.150.87 80/TCP,443/TCP 31m nginx-svc-w ClusterIP 10.110.144.61 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
接下來我們需要手動創建一個自定義的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
如果業務變更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
然后創建就行了:
kubectl apply -f nginx-svc-wname.yaml
Service 的類型:
ClusterIP
:在集群內部使用,默認
ExternalName
:通過返回定義的CNAME別名
NodePort
:在所有安裝了kube-proxy的節點上打開一個端口,此端口可以代理至后端Pod,然后集群外部可以使用節點的IP地址和NodePort端口號訪問到集群Pod服務。端口取值范圍默認30000-32767
LoadBalancer
:使用云服務商提供的負載均衡器公開服務
原文鏈接:https://yyang.blog.csdn.net/article/details/122878503
相關推薦
- 2022-02-22 SWT線程訪問無效:org.eclipse.swt.SWTException: Invalid th
- 2022-04-24 25個值得收藏的Python文本處理案例_python
- 2023-01-11 Pytorch如何把Tensor轉化成圖像可視化_python
- 2022-08-23 .net中的Span<T>類和Memory<T>類介紹_基礎應用
- 2022-04-28 C++實現簡單班級成績管理系統_C 語言
- 2023-01-03 Android?輸入框被擋問題完美解決方案_Android
- 2022-07-19 Python數據分析之NumPy常用函數使用詳解_python
- 2022-10-23 C#優雅的實現INotifyPropertyChanged接口_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支