Istio从入门到精通—— 安装 —— 使用 Istio Operator 安装、更新、就地升级、金丝雀升级、卸载

使用 Istio Operator 安装、更新、就地升级、金丝雀升级、卸载

https://istio.io/latest/zh/docs/setup/install/operator/

全新安装 Istio 时不鼓励使用 Operator,请优先使用 Istioctl 和 Helm 安装方法。Operator 仍然会得到维护,但新的功能请求可能不会优先考虑。

除了手动在生产环境中安装、升级、和卸载 Istio,您还可以用 Istio Operator 管理安装。 这样做还能缓解管理不同 Istio 版本的负担。 您只需简单的更新 Operator 自定义资源(CR)即可, Operator 控制器将为您应用更改的相应配置。

使用 Operator 确实存在安全隐患: 这是因为当使用 istioctl install 命令时,操作运行于管理员用户的安全上下文中; 而使用 Operator 时,操作运行于集群内 pod 自己的安全上下文中。 为避免此漏洞,需要确保 Operator 自身部署的足够安全。

一、安装

https://istio.io/latest/zh/docs/setup/install/operator/#install

1.1、部署 Istio Operator

    istioctl命令可用于自动部署 Istio 操作符:

istioctl operator init

此命令运行 Operator 在 istio-operator 命名空间中创建以下资源:

      • Operator 自定义资源定义(CRD)
      • Operator 控制器的 Deployment 对象
      • 一个用来访问 Operator 指标的服务
      • Istio Operator 运行必须的 RBAC 规则

1.2、使用 operator 安装 Istio

    要使用 Operator 安装 Istio demo 配置项(configuration profile),请运行以下命令:

kubectl apply -f - <<EOF
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
  namespace: istio-system
  name: example-istiocontrolplane
spec:
  profile: demo
EOF

    控制器将检测IstioOperator资源,然后安装(demo)配置指定的 Istio 组件。

如果在初始化 Istio Operator 时使用了 --watchedNamespaces, 请将 IstioOperator 资源应用于任一观测的命名空间中,而不是应用于 istio-system 中。

默认情况下,Istio 控制平面(istiod)将安装在 istio-system 命名空间中。

要将其安装到其他命名空间,请如下使用 values.global.istioNamespace 字段:

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
...
spec:
  profile: demo
  values:
    global:
      istioNamespace: istio-namespace1

Istio Operator 控制器在创建 IstioOperator 资源的 90 秒内开始安装 Istio。 Istio 安装过程将在 120 秒内完成。

可以使用以下命令确认 Istio 控制平面服务是否成功:

$ kubectl get services -n istio-system
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)   AGE
istio-egressgateway    ClusterIP      10.96.65.145    <none>           ...       30s
istio-ingressgateway   LoadBalancer   10.96.189.244   192.168.11.156   ...       30s
istiod                 ClusterIP      10.96.189.20    <none>           ...       37s
$ kubectl get pods -n istio-system
NAME                                    READY   STATUS    RESTARTS   AGE
istio-egressgateway-696cccb5-m8ndk      1/1     Running   0          68s
istio-ingressgateway-86cb4b6795-9jlrk   1/1     Running   0          68s
istiod-b47586647-sf6sw                  1/1     Running   0          74s

二、更新

https://istio.io/latest/zh/docs/setup/install/operator/#update

现在,控制器已经运行起来,您可以通过编辑或替换 IstioOperator 资源来改变 Istio 配置。 控制器将检测到改变,继而用相应配置更新安装的 Istio。

例如,使用以下命令将安装切换到 default 配置:

kubectl apply -f - <<EOF
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
  namespace: istio-system
  name: example-istiocontrolplane
spec:
  profile: default
EOF

您还可以启用或禁用组件、修改资源设置。 例如,启用 istio-egressgateway 组件并增加 pilot 的内存请求:

kubectl apply -f - <<EOF
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
  namespace: istio-system
  name: example-istiocontrolplane
spec:
  profile: default
  components:
    pilot:
      k8s:
        resources:
          requests:
            memory: 3072Mi
    egressGateways:
    - name: istio-egressgateway
      enabled: true
EOF

通过检查 Operator 控制器日志,您可以检测到控制器为了响应 IstioOperator CR 的更新,而在集群中所做的改变:

$ kubectl logs -f -n istio-operator "$(kubectl get pods -n istio-operator -lname=istio-operator -o jsonpath='{.items[0].metadata.name}')"

三、就地升级

https://istio.io/latest/zh/docs/setup/install/operator/#in-place-upgrade

下载并提取希望升级到的 Istio 版本对应的 istioctl。 在目标 Istio 版本的目录中,重新安装 Operator:

<extracted-dir>/bin/istioctl operator init

您会看到 istio-operator 的 Pod 已重新启动,其版本已更改到目标版本:

kubectl get pods --namespace istio-operator \
  -o=jsonpath='{range .items[*]}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{"\n"}{end}'

经过一两分钟后,Istio 控制平面组件也会重新启动为新版本:

kubectl get pods --namespace istio-system \
        -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{"\n"}{end}'
        

四、金丝雀升级

https://istio.io/latest/zh/docs/setup/install/operator/#canary-upgrade

金丝雀升级的过程类似于istioctl版本的金丝雀升级。

首先验证集群中名为example-istiocontrolplaneIstioOperatorCR是否存在:

例如要升级 Istio 1.19.0 到 1.20.2, 首先安装 1.19.0:

curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.19.0 sh -
                

使用 Istio 版本 1.19.0 部署 Operator:

istio-1.19.0/bin/istioctl operator init

安装 Istio 控制平面 demo 配置文件:

kubectl apply -f - <<EOF
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
  namespace: istio-system
  name: example-istiocontrolplane-1-19-0
spec:
  profile: default
EOF

确认您的集群中存在名为 example-istiocontrolplane 的 IstioOperator CR:

$ kubectl get iop --all-namespaces
NAMESPACE      NAME                              REVISION   STATUS    AGE
istio-system   example-istiocontrolplane1-19-0              HEALTHY   11m

下载并提取希望升级到的 Istio 版本对应的 istioctl。 然后,运行以下命令,基于集群内的 IstioOperator CR 的方式,安装 Istio 目标版本的控制平面 (这里,我们假设目标修订版本为 1.20.2):

$ istio-1.20.2/bin/istioctl operator init --revision 1-20-2
        

您也可以通过 Helm 用不同的修订设置部署另一个 Operator:

helm install istio-operator manifests/charts/istio-operator \
  --set watchedNamespaces=istio-system \
  -n istio-operator \
  --set revision=1-20-2

复制 example-istiocontrolplane CR 并将其另存为 example-istiocontrolplane-1-20-1.yaml 文件。 在 CR 中修改该文件的名称为 example-istiocontrolplane-1-20-2,并添加 revision: 1-20-2。 更新后的 IstioOperator CR 如下所示:

$ cat example-istiocontrolplane-1-20-2.yaml
        apiVersion: install.istio.io/v1alpha1
        kind: IstioOperator
        metadata:
          namespace: istio-system
          name: example-istiocontrolplane-1-20-2
        spec:
          revision: 1-20-2
          profile: default

运行该命令后,您将看到两组并排运行的控制平面 Deployment 和 Service:

$ kubectl get pod -n istio-system -l app=istiod
                NAME                             READY   STATUS    RESTARTS   AGE
                istiod-1-20-2-597475f4f6-bgtcz   1/1     Running   0          64s
                istiod-6ffcc65b96-bxzv5          1/1     Running   0          2m11s
    
$ kubectl get services -n istio-system -l app=istiod
                NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                         AGE
                istiod          ClusterIP   10.104.129.150           15010/TCP,15012/TCP,443/TCP,15014/TCP,853/TCP   2m35s
                istiod-1-20-2   ClusterIP   10.111.17.49             15010/TCP,15012/TCP,443/TCP,15014/TCP           88s
    

要完成升级,请给工作负载的命名空间打这个标签:istio.io/rev=1-20-2,并重新启动工作负载, 就如数据平面升级文档的描述。

五、卸载

如果您使用 Operator 完成了控制平面的金丝雀升级,请运行以下命令卸载旧版本的控件平面,并保留新版本:

[root@k8s-master-node1 ~]# kubectl get istiooperators -n istio-system
NAME                REVISION   STATUS    AGE
example-istiocontrolplane              HEALTHY   21h
[root@k8s-master-node1 ~]# kubectl delete istiooperators.install.istio.io -n istio-system example-istiocontrolplane
istiooperator.install.istio.io "istiocontrolplane" deleted
[root@k8s-master-node1 ~]#

  等到 Istio 卸载完成 - 这可能需要一些时间。 然后删除 Istio Operator:

[root@k8s-master-node1 ~]# kubectl get istiooperators -n istio-system
No resources found in istio-system namespace.
[root@k8s-master-node1 ~]# kubectl get pods -n istio-system
NAME                          READY   STATUS    RESTARTS     AGE
grafana-86b7d46c86-kv2rv      1/1     Running   0            21h
jaeger-594658fc5b-s4bt5       1/1     Running   2 (9h ago)   21h
kiali-79749c5b84-tctdt        1/1     Running   1 (9h ago)   21h
loki-0                        1/1     Running   0            21h
prometheus-67599c8d5c-6gp25   2/2     Running   4 (9h ago)   21h
[root@k8s-master-node1 ~]# istioctl uninstall -y --purge
All Istio resources will be pruned from the cluster

  Removed Deployment:istio-operator:istio-operator.
  Removed Service:istio-operator:istio-operator.
  Removed ServiceAccount:istio-operator:istio-operator.
  Removed ClusterRole::istio-operator.
  Removed ClusterRoleBinding::istio-operator.
  Removed CustomResourceDefinition::authorizationpolicies.security.istio.io.
  Removed CustomResourceDefinition::destinationrules.networking.istio.io.
  Removed CustomResourceDefinition::envoyfilters.networking.istio.io.
  Removed CustomResourceDefinition::gateways.networking.istio.io.
  Removed CustomResourceDefinition::istiooperators.install.istio.io.
  Removed CustomResourceDefinition::peerauthentications.security.istio.io.
  Removed CustomResourceDefinition::proxyconfigs.networking.istio.io.
  Removed CustomResourceDefinition::requestauthentications.security.istio.io.
  Removed CustomResourceDefinition::serviceentries.networking.istio.io.
  Removed CustomResourceDefinition::sidecars.networking.istio.io.
  Removed CustomResourceDefinition::telemetries.telemetry.istio.io.
  Removed CustomResourceDefinition::virtualservices.networking.istio.io.
  Removed CustomResourceDefinition::wasmplugins.extensions.istio.io.
  Removed CustomResourceDefinition::workloadentries.networking.istio.io.
  Removed CustomResourceDefinition::workloadgroups.networking.istio.io.
✔ Uninstall complete[root@k8s-master-node1 ~]#
  #----------------------------------------------------------
  上面卸载的命令是1.20.2的,我在1.14.6中使用提示我没有uninstall命令,1.14.6命令如下:
  # 使用 --purge 进行完全卸载,包含集群范围的资源,这些资源有可能是和其他Istio控制面共享的
    istioctl x uninstall --purge

  # 只卸载某个特定的Istio控制面
    istioctl x uninstall 
  # 或 
    istioctl manifest generate  | kubectl delete -f -

  如果省略revision标志,则 Istio Operator 的所有修订版本都将被删除。

  注意:在 Istio 完全移除之前删除 Operator 可能会导致 Istio 资源残留。 需要清理 Operator 未删除的内容:

$ istioctl uninstall -y --purge
$ kubectl delete ns istio-system istio-operator
posted @ 2024-01-15 15:16  左扬  阅读(125)  评论(0编辑  收藏  举报
levels of contents