自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

K8s-服務(wù)網(wǎng)格實(shí)戰(zhàn)-配置 Mesh(灰度發(fā)布)

云計(jì)算 云原生
從 Istio 的流量模型中可以看出:Istio 支持管理集群的出入口請(qǐng)求(gateway),同時(shí)也支持管理集群內(nèi)的 Mesh 流量,也就是集群內(nèi)服務(wù)之間的請(qǐng)求。

在上一篇 k8s-服務(wù)網(wǎng)格實(shí)戰(zhàn)-入門Istio中分享了如何安裝部署 Istio,同時(shí)可以利用 Istio 實(shí)現(xiàn) gRPC 的負(fù)載均衡。

今天我們更進(jìn)一步,深入了解使用 Istio 的功能。

從 Istio 的流量模型中可以看出:Istio 支持管理集群的出入口請(qǐng)求(gateway),同時(shí)也支持管理集群內(nèi)的 mesh 流量,也就是集群內(nèi)服務(wù)之間的請(qǐng)求。

本次先講解集群內(nèi)部的請(qǐng)求,配合實(shí)現(xiàn)以下兩個(gè)功能:

  • 灰度發(fā)布(對(duì)指定的請(qǐng)求分別路由到不同的 service 中)
  • 配置 service 的請(qǐng)求權(quán)重

灰度發(fā)布

在開始之前會(huì)部署兩個(gè) deployment 和一個(gè) service,同時(shí)這兩個(gè) deployment 所關(guān)聯(lián)的 Pod 分別對(duì)應(yīng)了兩個(gè)不同的 label,由于在灰度的時(shí)候進(jìn)行分組。

使用這個(gè) yaml 會(huì)部署所需要的 deployment 和 service。

kubectl apply -f https://raw.githubusercontent.com/crossoverJie/k8s-combat/main/deployment/istio-mesh.yaml

首先設(shè)想下什么情況下我們需要灰度發(fā)布,一般是某個(gè)重大功能的測(cè)試,只對(duì)部分進(jìn)入內(nèi)測(cè)的用戶開放入口。

假設(shè)我們做的是一個(gè) App,我們可以對(duì)拿到了內(nèi)測(cè)包用戶的所有請(qǐng)求頭中加入一個(gè)版本號(hào)。

比如 versinotallow=200 表示新版本,versinotallow=100 表示老版本。同時(shí)在服務(wù)端會(huì)將這個(gè)版本號(hào)打印出來(lái),用于區(qū)分請(qǐng)求是否進(jìn)入了預(yù)期的 Pod。

// Client 
version := r.URL.Query().Get("version")  
name := "world"  
ctx, cancel := context.WithTimeout(context.Background(), time.Second)  
md := metadata.New(map[string]string{  
    "version": version,  
})  
ctx = metadata.NewOutgoingContext(ctx, md)  
defer cancel()  
g, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})

// Server
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {  
    md, ok := metadata.FromIncomingContext(ctx)  
    var version string  
    if ok {  
       version = md.Get("version")[0]  
    }    log.Printf("Received: %v, version: %s", in.GetName(), version)  
    name, _ := os.Hostname()  
    return &pb.HelloReply{Message: fmt.Sprintf("hostname:%s, in:%s, version:%s", name, in.Name, version)}, nil  
}

對(duì) service 分組

進(jìn)行灰度測(cè)試時(shí)往往需要新增部署一個(gè)灰度服務(wù),這里我們稱為 v2(也就是上圖中的 Pod2)。

同時(shí)需要將 v1 和 v2 分組:

apiVersion: networking.istio.io/v1alpha3  
kind: DestinationRule  
metadata:  
  name: k8s-combat-service-ds  
spec:  
  host: k8s-combat-service-istio-mesh  
  subsets:  
    - name: v1  
      labels:  
        app: k8s-combat-service-v1  
    - name: v2  
      labels:  
        app: k8s-combat-service-v2

這里我們使用 Istio 的 DestinationRule 定義 subset,也就是將我們的 service 下的 Pod 分為 v1/v2。

使用 標(biāo)簽 app 進(jìn)行分組

注意這里的 host: k8s-combat-service-istio-mesh 通常配置的是 service 名稱。

apiVersion: v1  
kind: Service  
metadata:  
  name: k8s-combat-service-istio-mesh  
spec:  
  selector:  
    appId: "12345"  
  type: ClusterIP  
  ports:  
    - port: 8081  
      targetPort: 8081  
      name: app  
    - name: grpc  
      port: 50051  
      targetPort: 50051

也就是這里 service 的名稱,同時(shí)也支持配置為 host: k8s-combat-service-istio-mesh.default.svc.cluster.local,如果使用的簡(jiǎn)寫Istio 會(huì)根據(jù)當(dāng)前指定的 namespace 進(jìn)行解析。

Istio 更推薦使用全限定名替代我們這里的簡(jiǎn)寫,從而避免誤操作。

當(dāng)然我們也可以在 DestinationRule 中配置負(fù)載均衡的策略,這里我們先略過(guò):

apiVersion: networking.istio.io/v1alpha3  
kind: DestinationRule  
metadata:  
  name: k8s-combat-service-ds  
spec:  
  host: k8s-combat-service-istio-mesh 
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN

這樣我們就定義好了兩個(gè)分組:

  • v1:app: k8s-combat-service-v1
  • v2:app: k8s-combat-service-v2

之后就可以配置路由規(guī)則將流量分別指定到兩個(gè)不同的組中,這里我們使用 VirtualService 進(jìn)行配置。

apiVersion: networking.istio.io/v1alpha3  
kind: VirtualService  
metadata:  
  name: k8s-combat-service-vs  
spec:  
  gateways:  
    - mesh  
  hosts:  
    - k8s-combat-service-istio-mesh # match this host
http:  
  - name: v1  
    match:  
      - headers:  
          version:  
            exact: '100'  
    route:  
      - destination:  
          host: k8s-combat-service-istio-mesh  
          subset: v1  
  - name: v2  
    match:  
      - headers:  
          version:  
            exact: '200'  
    route:  
      - destination:  
          host: k8s-combat-service-istio-mesh  
          subset: v2  
  - name: default  
    route:  
      - destination:  
          host: k8s-combat-service-istio-mesh  
          subset: v1

這個(gè)規(guī)則很簡(jiǎn)單,會(huì)檢測(cè) http 協(xié)議的 header 中的 version 字段值,如果為 100 這路由到 subset=v1 這個(gè)分組的 Pod 中,同理為 200 時(shí)則路由到 subset=v2 這個(gè)分組的 Pod 中。

當(dāng)沒(méi)有匹配到 header 時(shí)則進(jìn)入默認(rèn)的 subset:v1

gRPC 也是基于 http 協(xié)議,它的 metadata 也就對(duì)應(yīng)了 http 協(xié)議中的 header。

header=100

Greeting: hostname:k8s-combat-service-v1-5b998dc8c8-hkb72, in:world, version:100istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=100"
Greeting: hostname:k8s-combat-service-v1-5b998dc8c8-hkb72, in:world, version:100istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=100"
Greeting: hostname:k8s-combat-service-v1-5b998dc8c8-hkb72, in:world, version:100istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=100"
Greeting: hostname:k8s-combat-service-v1-5b998dc8c8-hkb72, in:world, version:100istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=100"

header=200

Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"

根據(jù)以上的上面的測(cè)試請(qǐng)求來(lái)看,只要我們請(qǐng)求頭里帶上指定的 version 就會(huì)被路由到指定的 Pod 中。

利用這個(gè)特性我們就可以在灰度驗(yàn)證的時(shí)候單獨(dú)發(fā)一個(gè)灰度版本的 Deployment,同時(shí)配合客戶端指定版本就可以實(shí)現(xiàn)灰度功能了。

配置權(quán)重

同樣基于 VirtualService 我們還可以對(duì)不同的 subset 分組進(jìn)行權(quán)重配置。

apiVersion: networking.istio.io/v1alpha3  
kind: VirtualService  
metadata:  
  name: k8s-combat-service-vs  
spec:  
  gateways:  
    - mesh  
  hosts:  
    - k8s-combat-service-istio-mesh # match this host  
  http:  
    - match:  
        - uri:  
            exact: /helloworld.Greeter/SayHello  
      route:  
        - destination:  
            host: k8s-combat-service-istio-mesh  
            subset: v1  
          weight: 10  
        - destination:  
            host: k8s-combat-service-istio-mesh  
            subset: v2  
          weight: 90  
      timeout: 5000ms

這里演示的是針對(duì) SayHello 接口進(jìn)行權(quán)重配置(當(dāng)然還有多種匹配規(guī)則),90% 的流量會(huì)進(jìn)入 v2 這個(gè) subset,也就是在 k8s-combat-service-istio-mesh service 下的 app: k8s-combat-service-v2 Pod。

Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-**v1**-5b998dc8c8-hkb72, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$ curl "http://127.0.0.1:8081/grpc_client?name=k8s-combat-service-istio-mesh&versinotallow=200"
Greeting: hostname:k8s-combat-service-v2-5db566fb76-xj7j6, in:world, version:200istio-proxy@k8s-combat-service-v1-5b998dc8c8-hkb72:/$

經(jīng)過(guò)測(cè)試會(huì)發(fā)現(xiàn)大部分的請(qǐng)求都會(huì)按照我們的預(yù)期進(jìn)入 v2 這個(gè)分組。

當(dāng)然除之外之外我們還可以:

  • 超時(shí)時(shí)間
  • 故障注入
  • 重試 具體的配置可以參考 Istio 官方文檔:

  • 當(dāng)然在一些云平臺(tái)也提供了可視化的頁(yè)面,可以更直觀的使用。

以上是 阿里云的截圖

但他們的管理的資源都偏 kubernetes,一般是由運(yùn)維或者是 DevOps 來(lái)配置,不方便開發(fā)使用,所以還需要一個(gè)介于云廠商和開發(fā)者之間的管理發(fā)布平臺(tái),可以由開發(fā)者以項(xiàng)目維度管理維護(hù)這些功能。

本文的所有源碼在這里可以訪問(wèn):https://github.com/crossoverJie/k8s-combat。

責(zé)任編輯:姜華 來(lái)源: crossoverJie
相關(guān)推薦

2023-11-01 08:08:22

k8s服務(wù)網(wǎng)格

2021-04-25 08:48:36

Traefik mes服務(wù)網(wǎng)格Kubernetes集

2021-06-05 10:16:55

Linkerd 服務(wù)網(wǎng)格Kubernetes

2020-01-31 14:12:53

云計(jì)算HPC無(wú)服務(wù)器

2022-11-24 14:21:27

微服務(wù)ISTIO

2023-06-18 19:21:04

技術(shù)架構(gòu)服務(wù)網(wǎng)格

2020-11-15 23:48:57

服務(wù)網(wǎng)格微服務(wù)網(wǎng)絡(luò)網(wǎng)絡(luò)技術(shù)

2019-08-29 08:00:00

微服務(wù)架構(gòu)服務(wù)網(wǎng)格

2021-07-21 05:23:06

Linkerd Emoji.voto服務(wù)網(wǎng)格

2022-05-16 08:00:00

服務(wù)網(wǎng)格架構(gòu)Kuma

2023-09-26 00:59:54

零配置服務(wù)網(wǎng)格

2020-01-07 09:25:02

服務(wù)網(wǎng)格微服務(wù)Kubernetes

2022-08-09 08:00:00

服務(wù)網(wǎng)格云原生工具

2020-07-13 07:00:03

微服務(wù)服務(wù)網(wǎng)格架構(gòu)

2023-09-08 08:09:12

k8sservice服務(wù)

2021-08-27 11:42:51

Nacos云原生阿里云

2020-08-26 05:45:40

服務(wù)網(wǎng)格DevOps開發(fā)

2024-09-27 10:05:02

2021-04-02 22:00:50

服務(wù)網(wǎng)格微服務(wù)

2020-10-21 13:31:53

服務(wù)網(wǎng)格開源微服務(wù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)