安装网关
除了创建服务网格外,Istio 还允许您管理 网关,它们是在网格边缘运行的 Envoy 代理,可以对进入和离开网格的流量进行细粒度控制。
Istio 的一些内置 配置配置文件 在安装过程中部署网关。例如,使用 默认设置 调用 istioctl install
将部署一个 Ingress 网关以及控制平面。虽然这对于评估和简单的用例来说很好,但这将网关与控制平面耦合在一起,使管理和升级更加复杂。对于生产 Istio 部署,强烈建议将它们解耦,以允许独立操作。
请遵循本指南,在 Istio 的生产安装中单独部署和管理一个或多个网关。
先决条件
本指南要求在继续之前 安装 Istio 控制平面。
部署网关
使用与 Istio Sidecar 注入 相同的机制,网关的 Envoy 代理配置也可以类似地自动注入。
建议对网关部署使用自动注入,因为它可以让开发人员完全控制网关部署,同时简化操作。当有新升级可用或配置发生更改时,只需重新启动网关 Pod 即可更新网关 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
之类的命令。
如果您想更改网关使用的 控制平面修订版,您可以在网关部署上设置 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 Ingress 网关
$ istioctl uninstall --istioNamespace istio-ingress -y --purge $ kubectl delete ns istio-ingress