欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

服務(wù)發(fā)現(xiàn)與負(fù)載均衡機(jī)制Service實(shí)例創(chuàng)建

 更新時(shí)間:2022年03月18日 15:54:54   作者:、重明  
這篇文章主要為大家介紹了服務(wù)發(fā)現(xiàn)與負(fù)載均衡機(jī)制Service實(shí)例創(chuàng)建有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

什么是Service?

Service是邏輯上的一組Pod,一種可以訪問Pod的策略,而且其他Pod可以通過Service訪問到這個(gè)Service代理的Pod,可以把它理解為傳統(tǒng)架構(gòu)中的反向代理。

相對(duì)于Pod而言,Service有一個(gè)固定的名稱,不會(huì)發(fā)生改變,并且提供了負(fù)載均衡的功能。

通過Service的定義,可以對(duì)客戶端應(yīng)用屏蔽后端Pod實(shí)例數(shù)量及Pod IP地址的變化,通過負(fù)載均衡策略實(shí)現(xiàn)請(qǐng)求到后端Pod實(shí)例的轉(zhuǎn)發(fā),為客戶端應(yīng)用提供一個(gè)穩(wěn)定的服務(wù)訪問入口地址。

Service實(shí)現(xiàn)的是微服務(wù)架構(gòu)中的幾個(gè)核心功能:全自動(dòng)的服務(wù)注冊(cè)、服務(wù)發(fā)現(xiàn)、服務(wù)負(fù)載均衡等。

創(chuàng)建一個(gè)Service實(shí)例

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 #后端應(yīng)用接口
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
  selector:
    app: nginx  #這個(gè)就是匹配規(guī)則,代理標(biāo)簽中有nginx的后端服務(wù)器
  sessionAffinity: None
  type: ClusterIP

執(zhí)行上面的yaml文件,創(chuàng)建一個(gè)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>
  • 在同一個(gè)namespace中,其他Pod訪問svc只需要curl http://nginx-svc就可以
  • 跨namespace的話,需要在svc名稱后加.namespace名稱就可以了,使用需謹(jǐn)慎,沒必要不推薦
  • 不建議通過IP地址訪問,因?yàn)镮P不是固定的,刪除或重建后IP會(huì)隨機(jī)生成

注意,以上內(nèi)容只能在集群內(nèi)部訪問

集群外部訪問svc

  • 希望在生產(chǎn)中使用某個(gè)固定的名稱而非IP地址進(jìn)行訪問外部的中間件服務(wù)
  • 希望Service指向另一個(gè)namespace中或其他集群中的服務(wù)
  • 某項(xiàng)目正在遷移至k8s集群,但是一部分服務(wù)仍然在集群外部,此時(shí)可以使用service代理外部的服務(wù)

創(chuàng)建代理外部應(yīng)用的Service實(shí)例

下面我們定義一個(gè)外部應(yīng)用的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ū)別就是少了這兩個(gè)參數(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

接下來我們需要手動(dòng)創(chuàng)建一個(gè)自定義的ENDPOINTS借用存在的ep生成一個(gè)新的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 # 代理的外部服務(wù)的地址
  ports:
  - name: http
    port: 80
    protocol: TCP

可以看到已經(jīng)有外部的代理了

[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è)務(wù)變更ep地址變了怎么辦?只需要在ep中將代理的地址更換即可。

比如我們更換一個(gè)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 

訪問測試一下看是否連通:這個(gè)返回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反代外部域名

這個(gè)不說了,知道怎么配就行了。
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:在集群內(nèi)部使用,默認(rèn)

ExternalName:通過返回定義的CNAME別名

NodePort:在所有安裝了kube-proxy的節(jié)點(diǎn)上打開一個(gè)端口,此端口可以代理至后端Pod,然后集群外部可以使用節(jié)點(diǎn)的IP地址和NodePort端口號(hào)訪問到集群Pod服務(wù)。端口取值范圍默認(rèn)30000-32767

LoadBalancer:使用云服務(wù)商提供的負(fù)載均衡器公開服務(wù)

以上就是服務(wù)發(fā)現(xiàn)與負(fù)載均衡機(jī)制Service實(shí)例創(chuàng)建的詳細(xì)內(nèi)容,更多關(guān)于服務(wù)發(fā)現(xiàn)與負(fù)載均衡Service的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論