安裝閘道
除了建立服務網格之外,Istio 還允許您管理 閘道器,它們是在網格邊緣執行的 Envoy 代理,提供對進出網格流量的細粒度控制。
Istio 的一些內建 配置設定檔會在安裝期間部署閘道器。例如,使用 預設設定呼叫 istioctl install
將部署一個入口閘道器以及控制平面。雖然對於評估和簡單的使用案例來說足夠好,但這會將閘道器耦合到控制平面,使管理和升級更加複雜。對於生產 Istio 部署,強烈建議將它們解耦以允許獨立操作。
按照本指南,在 Istio 的生產安裝中單獨部署和管理一個或多個閘道器。
先決條件
本指南要求在繼續之前安裝 Istio 控制平面。
部署閘道器
使用與 Istio sidecar 注入相同的機制,可以類似地自動注入閘道器的 Envoy 代理配置。
建議對閘道器部署使用自動注入,因為它使開發人員可以完全控制閘道器部署,同時簡化操作。當有新的升級可用或配置變更時,只需重新啟動閘道器 Pod 即可更新它們。這使得操作閘道器部署的體驗與操作 sidecar 相同。
為了支援使用現有部署工具的使用者,Istio 提供了幾種不同的閘道器部署方式。每種方法都會產生相同的結果。選擇您最熟悉的方法。
下面列出的所有方法都依賴 注入在執行時填入其他 Pod 設定。為了支援這一點,部署閘道器的命名空間不得具有 istio-injection=disabled
標籤。如果有的話,您會看到 Pod 無法啟動,並嘗試提取 auto
映像,這是一個佔位符,旨在在建立 Pod 時被替換。
首先,設定一個 IstioOperator
配置檔案,這裡稱為 ingress.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
name: ingress
spec:
profile: empty # Do not install CRDs or the control plane
components:
ingressGateways:
- name: istio-ingressgateway
namespace: istio-ingress
enabled: true
label:
# Set a unique label for the gateway. This is required to ensure Gateways
# can select this workload
istio: ingressgateway
values:
gateways:
istio-ingressgateway:
# Enable gateway injection
injectionTemplate: gateway
然後使用標準 istioctl
命令安裝
$ kubectl create namespace istio-ingress
$ istioctl install -f ingress.yaml
使用標準 helm
命令安裝
$ kubectl create namespace istio-ingress
$ helm install istio-ingressgateway istio/gateway -n istio-ingress
要查看可能支援的配置值,請執行 helm show values istio/gateway
。Helm 儲存庫 README 包含有關使用方式的其他資訊。
首先,設定 Kubernetes 配置,這裡稱為 ingress.yaml
apiVersion: v1
kind: Service
metadata:
name: istio-ingressgateway
namespace: istio-ingress
spec:
type: LoadBalancer
selector:
istio: ingressgateway
ports:
- port: 80
name: http
- port: 443
name: https
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: istio-ingressgateway
namespace: istio-ingress
spec:
selector:
matchLabels:
istio: ingressgateway
template:
metadata:
annotations:
# Select the gateway injection template (rather than the default sidecar template)
inject.istio.io/templates: gateway
labels:
# Set a unique label for the gateway. This is required to ensure Gateways can select this workload
istio: ingressgateway
# Enable gateway injection. If connecting to a revisioned control plane, replace with "istio.io/rev: revision-name"
sidecar.istio.io/inject: "true"
spec:
# Allow binding to all ports (such as 80 and 443)
securityContext:
sysctls:
- name: net.ipv4.ip_unprivileged_port_start
value: "0"
containers:
- name: istio-proxy
image: auto # The image will automatically update each time the pod starts.
# Drop all privileges, allowing to run as non-root
securityContext:
capabilities:
drop:
- ALL
runAsUser: 1337
runAsGroup: 1337
---
# Set up roles to allow reading credentials for TLS
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: istio-ingressgateway-sds
namespace: istio-ingress
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: istio-ingressgateway-sds
namespace: istio-ingress
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: istio-ingressgateway-sds
subjects:
- kind: ServiceAccount
name: default
接下來,將其套用至叢集
$ kubectl create namespace istio-ingress
$ kubectl apply -f ingress.yaml
管理閘道器
以下說明如何在安裝後管理閘道器。有關其使用方式的更多資訊,請遵循 Ingress 和 Egress 工作。
閘道器選擇器
閘道器部署 Pod 上的標籤由 Gateway
配置資源使用,因此您的 Gateway
選擇器必須與這些標籤相符。
例如,在上述部署中,istio=ingressgateway
標籤設定在閘道器 Pod 上。要將 Gateway
套用至這些部署,您需要選擇相同的標籤
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: gateway
spec:
selector:
istio: ingressgateway
...
閘道器部署拓樸
根據您的網格配置和使用案例,您可能希望以不同的方式部署閘道器。下面顯示了一些不同的閘道器部署模式。請注意,同一叢集中可以使用多種模式。
共用閘道器
在此模型中,許多應用程式(可能跨越多個命名空間)使用單個集中式閘道器。ingress
命名空間中的閘道器將路由的所有權委派給應用程式命名空間,但保留對 TLS 配置的控制。
當您有許多想要在外部公開的應用程式時,此模型非常有效,因為它們能夠使用共用基礎設施。它也適用於許多應用程式共用相同網域或 TLS 憑證的使用案例。
專用應用程式閘道器
在此模型中,應用程式命名空間有其自己的專用閘道器安裝。這允許將完全控制權和所有權授予單個命名空間。這種隔離級別對於具有嚴格效能或安全要求的關鍵應用程式很有幫助。
除非 Istio 前面有另一個負載平衡器,否則這通常表示每個應用程式都將有自己的 IP 位址,這可能會使 DNS 配置複雜化。
升級閘道器
就地升級
由於閘道器利用 Pod 注入,因此建立的新閘道器 Pod 將自動注入最新的配置,其中包括版本。
要取得對閘道器配置的變更,只需重新啟動 Pod 即可,使用例如 kubectl rollout restart deployment
等命令。
如果您想變更閘道器使用的 控制平面修訂,則可以在閘道器 Deployment 上設定 istio.io/rev
標籤,這也會觸發滾動重新啟動。
金絲雀升級 (進階)
如果您想更緩慢地控制新控制平面修訂的推出,則可以執行多個版本的閘道器部署。例如,如果您想推出新的修訂版 canary
,請建立閘道器部署的複本,並設定 istio.io/rev=canary
標籤
apiVersion: apps/v1
kind: Deployment
metadata:
name: istio-ingressgateway-canary
namespace: istio-ingress
spec:
selector:
matchLabels:
istio: ingressgateway
template:
metadata:
annotations:
inject.istio.io/templates: gateway
labels:
istio: ingressgateway
istio.io/rev: canary # Set to the control plane revision you want to deploy
spec:
containers:
- name: istio-proxy
image: auto
當建立此部署時,您將有兩個版本的閘道器,它們都由相同的服務選擇
$ kubectl get endpoints -n istio-ingress -o "custom-columns=NAME:.metadata.name,PODS:.subsets[*].addresses[*].targetRef.name"
NAME PODS
istio-ingressgateway istio-ingressgateway-...,istio-ingressgateway-canary-...
與部署在網格內的應用程式服務不同,您無法使用 Istio 流量轉移在閘道器版本之間分配流量,因為它們的流量直接來自 Istio 無法控制的外部用戶端。相反,您可以使用每個部署的副本數來控制流量分配。如果 Istio 前面使用了另一個負載平衡器,您也可以使用它來控制流量分配。
使用外部流量轉移的金絲雀升級 (進階)
金絲雀升級方法的一種變體是使用 Istio 外部的高階結構(例如外部負載平衡器或 DNS)在版本之間轉移流量。
這提供了細粒度的控制,但在某些環境中可能不適合或設置過於複雜。
清除
清除 Istio 入口閘道器
$ istioctl uninstall --istioNamespace istio-ingress -y --purge $ kubectl delete ns istio-ingress