動態配置(控制平面)

此範例逐步說明如何使用 Go Control Plane 參考實作來設定 Envoy。

它示範了提供給 Envoy 的配置如何持續存在,即使控制平面不可用,並提供了一個如何動態更新 Envoy 配置的簡單範例。

步驟 1:啟動代理容器

將目錄變更為 Envoy 儲存庫中的 examples/dynamic-config-cp

首先建置容器並啟動 proxy 容器。

這也應該啟動兩個上游 HTTP echo 伺服器,service1service2

控制平面尚未啟動。

$ pwd
envoy/examples/dynamic-config-cp
$ docker compose pull
$ docker compose up --build -d proxy
$ docker compose ps

       Name                            Command                State                     Ports
------------------------------------------------------------------------------------------------------------------------
dynamic-config-cp_proxy_1      /docker-entrypoint.sh /usr ... Up      0.0.0.0:10000->10000/tcp, 0.0.0.0:19000->19000/tcp
dynamic-config-cp_service1_1   /bin/echo-server               Up      8080/tcp
dynamic-config-cp_service2_1   /bin/echo-server               Up      8080/tcp

步驟 2:檢查初始配置和 Web 回應

由於我們尚未啟動控制平面,因此在連接埠 10000 上應該沒有任何回應。

$ curl https://127.0.0.1:10000
curl: (56) Recv failure: Connection reset by peer

傾印代理的 static_clusters 配置,您應該會看到名為 xds_cluster 的集群已針對控制平面配置

$ curl -s https://127.0.0.1:19000/config_dump  | jq '.configs[1].static_clusters'
[
  {
    "cluster": {
      "@type": "type.googleapis.com/envoy.api.v2.Cluster",
      "name": "xds_cluster",
      "type": "STRICT_DNS",
      "connect_timeout": "1s",
      "http2_protocol_options": {},
      "load_assignment": {
        "cluster_name": "xds_cluster",
        "endpoints": [
          {
            "lb_endpoints": [
              {
                "endpoint": {
                  "address": {
                    "socket_address": {
                      "address": "go-control-plane",
                      "port_value": 18000
                    }
                  }
                }
              }
            ]
          }
        ]
      }
    },
    "last_updated": "2020-10-25T20:20:54.897Z"
  }
]

尚未配置任何 dynamic_active_clusters

$ curl -s https://127.0.0.1:19000/config_dump  | jq '.configs[1].dynamic_active_clusters'
null

步驟 3:啟動控制平面

啟動 go-control-plane 服務。

您可能需要稍等片刻才能使其變成 healthy

$ docker compose up --build -d go-control-plane
$ docker compose ps

        Name                                Command                  State                    Ports
-------------------------------------------------------------------------------------------------------------------------------------
dynamic-config-cp_go-control-plane_1  bin/example -debug             Up (healthy)
dynamic-config-cp_proxy_1             /docker-entrypoint.sh /usr ... Up            0.0.0.0:10000->10000/tcp, 0.0.0.0:19000->19000/tcp
dynamic-config-cp_service1_1          /bin/echo-server               Up            8080/tcp
dynamic-config-cp_service2_1          /bin/echo-server               Up            8080/tcp

步驟 4:查詢代理

一旦控制平面啟動且 healthy,您應該可以向連接埠 10000 發出請求,該請求將由 service1 提供服務。

$ curl https://127.0.0.1:10000
Request served by service1

HTTP/1.1 GET /

Host: localhost:10000
Accept: */*
X-Forwarded-Proto: http
X-Request-Id: 1d93050e-f39c-4602-90f8-a124d6e78d26
X-Envoy-Expected-Rq-Timeout-Ms: 15000
Content-Length: 0
User-Agent: curl/7.72.0

步驟 5:傾印 Envoy 的 dynamic_active_clusters 配置

如果您現在傾印代理的 dynamic_active_clusters 配置,您應該會看到它已配置為指向 service1example_proxy_cluster,以及版本 1

$ curl -s https://127.0.0.1:19000/config_dump  | jq '.configs[1].dynamic_active_clusters'
[
  {
    "version_info": "1",
    "cluster": {
      "@type": "type.googleapis.com/envoy.config.cluster.v3.Cluster",
      "name": "example_proxy_cluster",
      "type": "LOGICAL_DNS",
      "connect_timeout": "5s",
      "dns_lookup_family": "V4_ONLY",
      "load_assignment": {
        "cluster_name": "example_proxy_cluster",
        "endpoints": [
          {
            "lb_endpoints": [
              {
                "endpoint": {
                  "address": {
                    "socket_address": {
                      "address": "service1",
                      "port_value": 8080
                    }
                  }
                }
              }
            ]
          }
        ]
      }
    },
    "last_updated": "2020-10-25T20:37:05.838Z"
  }
]

步驟 6:停止控制平面

停止 go-control-plane 服務

$ docker compose stop go-control-plane

Envoy 代理應該繼續代理來自 service1 的回應。

$ curl https://127.0.0.1:10000 | grep "served by"
Request served by service1

步驟 7:編輯 go 檔案並重新啟動控制平面

範例設定使用自訂的 resource.go 檔案啟動 go-control-plane 服務,該檔案指定提供給 Envoy 的配置。

更新此檔案以讓 Envoy 改為代理到 service2

在動態配置範例資料夾中編輯 resource.go,並將 UpstreamHostservice1 變更為 service2

35const (
36	ClusterName  = "example_proxy_cluster"
37	RouteName    = "local_route"
38	ListenerName = "listener_0"
39	ListenerPort = 10000
40	UpstreamHost = "service1"
41	UpstreamPort = 8080
42)

在此檔案的更下方,您還必須將配置快照版本號碼從 "1" 變更為 "2",以確保 Envoy 將配置視為較新的版本

175func GenerateSnapshot() cache.Snapshot {
176	return cache.NewSnapshot(
177		"1",
178		[]types.Resource{}, // endpoints
179		[]types.Resource{makeCluster(ClusterName)},
180		[]types.Resource{makeRoute(RouteName, ClusterName)},
181		[]types.Resource{makeHTTPListener(ListenerName, RouteName)},
182		[]types.Resource{}, // runtimes
183		[]types.Resource{}, // secrets
184		[]types.Resource{}, // extensions configs
185	)
186}

現在重新建置並重新啟動控制平面

$ docker compose up --build -d go-control-plane

您可能需要稍等片刻才能讓 go-control-plane 服務再次變成 healthy

步驟 8:檢查 Envoy 是否使用更新的配置

現在當您向代理發出請求時,應該由 service2 上游服務提供服務。

$ curl https://127.0.0.1:10000 | grep "served by"
Request served by service2

傾印 dynamic_active_clusters,您應該會看到集群配置現在的版本為 2,並且 example_proxy_cluster 已配置為代理到 service2

$ curl -s https://127.0.0.1:19000/config_dump  | jq '.configs[1].dynamic_active_clusters'
[
  {
    "version_info": "2",
    "cluster": {
      "@type": "type.googleapis.com/envoy.config.cluster.v3.Cluster",
      "name": "example_proxy_cluster",
      "type": "LOGICAL_DNS",
      "connect_timeout": "5s",
      "dns_lookup_family": "V4_ONLY",
      "load_assignment": {
        "cluster_name": "example_proxy_cluster",
        "endpoints": [
          {
            "lb_endpoints": [
              {
                "endpoint": {
                  "address": {
                    "socket_address": {
                      "address": "service2",
                      "port_value": 8080
                    }
                  }
                }
              }
            ]
          }
        ]
      }
    },
    "last_updated": "2020-10-26T14:35:17.360Z"
  }
]

注意

在此範例中,我們為了簡單起見而遞增版本。

對版本的任何變更都會觸發 Envoy 中的更新,並且排序並不重要(請參閱 xDS 協定文件以取得有關更新的更多資訊)。

另請參閱

動態配置(控制平面)快速入門指南

使用控制平面動態配置 Envoy 的快速入門指南。

Envoy 管理快速入門指南

Envoy 管理介面的快速入門指南。

動態配置(檔案系統)沙箱

使用基於檔案系統的動態配置來設定 Envoy。

Go 控制平面

使用 go 撰寫的 Envoy 控制平面參考實作。