关于阿里云:基于阿里云-ASK-的-Istio-微服务应用部署初探

2次阅读

共计 8614 个字符,预计需要花费 22 分钟才能阅读完成。

作者:王飞龙(不物)

目前 Kubernetes 曾经成为业界容器编排零碎的事实标准,基于 Kubernetes 的云原生利用生态(Helm, Istio, Knative, Kubeflow, Spark on Kubernetes 等)更是让 Kubernetes 成为云操作系统。在这样的背景下,Serverless 容器成为现有 Container as a Service 的进化方向之一,一方面通过 Serverless 形式根本性解决了 Kubernetes 本身的治理复杂性,让用户无需受困于 Kubernetes 集群容量布局、平安保护、故障诊断;另一方面也进一步开释了云计算的能力,将平安、可用性、可伸缩性等需要由基础设施实现。

ASK 作为阿里云 Serverless Kubernetes 平台 [ 1],不仅有免运维、秒级弹性、超大 Pod 容量、弹性预测等重磅能力,更重要的是它仍然是一个规范 Kubernetes 平台。

本文会通过在 ASK 上试用 Istio 部署微服务利用的形式,来验证 ASK 对规范 Kubernetes 的兼容性。Istio 作为 Service Mesh(服务网格)的领导解决方案,一方面自身足够简单具备代表性,另一方面它也代表了云原生时代微服务架构的趋势具备参考意义。

当初就让咱们开始吧!

创立集群

试用 Istio 前,须要筹备一个 ASK 集群。登录阿里云控制台,抉择 产品与服务 > 容器服务 Kubernetes 版 ,在左侧边栏抉择 集群 进入集群列表页面。点击右上角 创立集群 开始创立集群,配置集群参数如下:

  • 集群名称:hello-istio
  • 集群规格:Pro 版
  • 地区:美国(硅谷)
  • 付费类型:按量付费
  • Kubernetes 版本:1.20.11-aliyun.1
  • 专有网络:主动创立
  • Service CIDR:172.21.0.0/20
  • API Server 拜访:标准型 I(slb.s2.small)
  • 应用 EIP 裸露 API Server:是
  • 时区:Aisa/Shanghai(UTC+08:00)
  • 服务发现:CoreDNS
  • 应用日志服务:创立新 Project

确认配置后点击 创立集群 进入期待集群创立实现。Istio 依赖 DNS 服务,这里抉择创立集群时默认装置 CoreDNS 组件。

集群创立实现后,进入 集群列表 > hello-istio > 详情 > 集群信息 > 连贯信息 页面,复制 公网拜访 内容到本地 /tmp/kube/config 文件,并通过如下命令配置好 kubelet:

$ export KUBECONFIG=/tmp/kube/config

试用 Istio

kubectl 配置好后就能够开始在集群装置和试用 Istio。

下载 Istio

进入 Istio 发布页面 [ 2] 下载针对操作系统的安装文件,也能够通过如下命令下载并提取最新版本:

$ curl -L https://istio.io/downloadIstio | sh -

因为我本机~/bin 目录已退出 PATH,这里我将提取的 Istio 目录复制~/bin 目录,并建好软链接。

$ cp istio-1.13.3 ~/bin
$ cd ~/bin
$ ln -s istio-1.13.3/bin/istioctl
$ ls -al ~/bin/
total 28
drwxr-xr-x   5 feilong.wfl staff   160  5  4 22:40 ./
drwxr-xr-x+ 95 feilong.wfl staff  3040  5  8 22:30 ../
drwxr-x---   9 feilong.wfl staff   288  4 15 00:48 istio-1.13.3/
lrwxr-xr-x   1 feilong.wfl staff    25  5  4 22:40 istioctl -> istio-1.13.3/bin/istioctl*

如果 istioctl –help 命令输入失常,则 istioctl 已正确配置。

装置 Istio

  1. 本次装置采纳 demo profile [ 3],它蕴含了一组专为测试筹备的性能汇合,另外还有用户生产或性能测试的配置组合。
$ istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Egress gateways installed
✔ Ingress gateways installed
✔ Installation complete
  1. 给命名空间增加标签,批示 Istio 在部署利用的时候,主动注入 Envoy 边车代理:
$ kubectl label namespace default istio-injection=enabled
namespace/default labeled

部署示例利用

  1. 部署 Bookinfo 示例利用 [ 4]
$ kubectl apply -f ~/bin/istio-1.13.3/samples/bookinfo/platform/kube/bookinfo.yaml
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
deployment.apps/reviews-v2 created
deployment.apps/reviews-v3 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created
  1. 查看 Pod 已就绪:
$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
details-v1-79f774bdb9-t2jhq       2/2     Running   0          2m54s
productpage-v1-6b746f74dc-qc9lg   2/2     Running   0          2m46s
ratings-v1-b6994bb9-tmbh6         2/2     Running   0          2m51s
reviews-v1-545db77b95-xdhp4       2/2     Running   0          2m49s
reviews-v2-7bf8c9648f-4gn6f       2/2     Running   0          2m48s
reviews-v3-84779c7bbc-jfndj       2/2     Running   0          2m48s

要期待并确保所有的 Pod 达到此状态:就绪状态(READY)的值为 2/2、状态(STATUS)的值为 Running。基于平台的不同,这个操作过程可能会破费几分钟的工夫。

  1. 查看 Service 已就绪:
$ kubectl get services
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
details       ClusterIP   172.21.11.168   <none>        9080/TCP   59s
kubernetes    ClusterIP   172.21.0.1      <none>        443/TCP    33m
productpage   ClusterIP   172.21.0.124    <none>        9080/TCP   51s
ratings       ClusterIP   172.21.9.7      <none>        9080/TCP   57s
reviews       ClusterIP   172.21.13.223   <none>        9080/TCP   55s
  1. 确保网页服务失常。如果命令返回页面题目,则利用已在集群中运行。
$ kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -s productpage:9080/productpage | grep -o "<title>.*</title>"
<title>Simple Bookstore App</title>

对外开放服务

当初,BookInfo 利用曾经部署,但还不能被外界拜访。要凋谢拜访,须要创立 Istio 入站网关(Ingress Gateway), 它会把一个门路路由到网格内的服务。

  1. 把利用关联到 Istio 网关:
$ kubectl apply -f ~/bin/istio-1.13.3/samples/bookinfo/networking/bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created
  1. 确保配置文件没有问题:
$ istioctl analyze
✔ No validation issues found when analyzing namespace: default.

确定入站 IP 和端口

应用如下命令为拜访网关设置 INGRESS_HOST 和 INGRESS_PORT 两个变量:

$ export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
$ export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].port}')

设置变量 GATEWAY_URL,并确保 IP 地址和端口均胜利的赋值给了该变量:

$ export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT
$ echo "$GATEWAY_URL"
47.88.21.82:80

验证内部拜访

运行上面命令,获取 Bookinfo 利用的内部拜访地址:

$ echo "http://$GATEWAY_URL/productpage"
http://47.88.21.82:80/productpage

复制下面命令的输入地址到浏览器并拜访,确认 Bookinfo 曾经实现了内部拜访。刷新页面,发现 Book Reviews 的显示款式会一直变动。

查看仪表盘

仪表盘能帮忙理解服务网格的构造、展现网络的拓扑构造、剖析网格的衰弱状态。

  1. 首先装置 Kiali 和其余插件,期待部署实现。
$ kubectl apply -f ~/bin/istio-1.13.3/samples/addons
$ kubectl rollout status deployment/kiali -n istio-system
Waiting for deployment "kiali" rollout to finish: 0 of 1 updated replicas are available...
deployment "kiali" successfully rolled out
  1. 拜访 Kiali 仪表板。
$ istioctl dashboard kiali
  1. 在左侧的导航菜单,抉择 Graph,而后在 Namespace 下拉列表中,抉择 default

Kiali 仪表板展现了网格的概览、以及 Bookinfo 示例利用的各个服务之间的关系。它还提供过滤器来可视化流量的流动。

增加默认指标规定

应用 Istio 管制 Bookinfo 版本路由前,须要先在指标规定 [ 5] 中定义好可用的版本。运行以下命令为 Bookinfo 服务创立默认的指标规定:

$ kubectl apply -f ~/bin/istio-1.13.3/samples/bookinfo/networking/destination-rule-all.yaml
destinationrule.networking.istio.io/productpage created
destinationrule.networking.istio.io/reviews created
destinationrule.networking.istio.io/ratings created
destinationrule.networking.istio.io/details created

期待几秒钟,指标规定失效。您能够应用如下命令查看指标规定:

$ kubectl get destinationrules
NAME          HOST          AGE
details       details       30s
productpage   productpage   32s
ratings       ratings       31s
reviews       reviews       32s

路由所有流量到 v1 版本

运行以下命令创立 Virtual Service 将所有流量路由到微服务的 v1 版本:

$ kubectl apply -f ~/bin/istio-1.13.3/samples/bookinfo/networking/virtual-service-all-v1.yaml
virtualservice.networking.istio.io/productpage created
virtualservice.networking.istio.io/reviews created
virtualservice.networking.istio.io/ratings created
virtualservice.networking.istio.io/details created

您能够通过再次刷新 Bookinfo 应用程序的 /productpage 页面测试新配置。请留神,无论您刷新多少次,页面的评论局部都不会显示评级星标。这是因为以后已将 Istio 配置为评论服务的所有流量路由到版本 reviews:v1,而此版本的服务不拜访星级评分服务。

基于用户身份的路由

接下来将更改路由配置,实现将来自特定用户的所有流量路由到特定的服务版本。示例中来自名为 Jason 用户的所有流量将被路由到服务 review:v2。

Istio 对用户身份没有任何非凡的内置机制。本例中,productpage 服务在所有到 reviews 服务的 HTTP 申请中都减少了一个自定义的 end-user 申请头,从而达到成果。

  1. 运行以下命令以启用基于用户的路由:
$ kubectl apply -f ~/bin/istio-1.13.3/samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
virtualservice.networking.istio.io/reviews created
  1. 确保规定已创立:
$ kubectl get virtualservice reviews -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"reviews","namespace":"default"},"spec":{"hosts":["reviews"],"http":[{"match":[{"headers":{"end-user":{"exact":"jason"}}}],"route":[{"destination":{"host":"reviews","subset":"v2"}}]},{"route":[{"destination":{"host":"reviews","subset":"v1"}}]}]}}
  creationTimestamp: "2022-05-15T16:05:55Z"
  generation: 1
  name: reviews
  namespace: default
  resourceVersion: "1984849"
  uid: f3bd3dcb-d83c-4a75-9511-1fc9308ca05b
spec:
  hosts:
  - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1
  1. 在 Bookinfo 应用程序的 /productpage 上,以用户 jason 身份登录。刷新浏览器,看到每个评论旁边显示星级评分。

  1. 以其余用户身份登录,刷新浏览器。发现星级评分隐没了。

原理和限度

数据立体,Istio 通过向 Pod 注入的 Sidecar 代理(istio-proxy)来负责协调和管制微服务之前的所有网络通信。为了让 Sidecar 代理(istio-proxy)劫持业务容器流量,Istio 须要向 Pod 所在网络下发 iptables 规定。惯例装置模式下,iptables 规定下发是由 Istio 向 Pod 注入的初始化容器 istio-init 实现。向 Pod 网络下发 iptables 规定须要容器能够应用 NET_ADMIN 和 NET_RAW 两个高权限的能力(Capabilities)。ASK 集群中这两个高权限能力受 ASK Pod Security Policy [ 6] 和 ECI Container Security Policy [ 7] 的影响。

ASK Pod Security Policy 的 CAPS 为 *,示意没有限度。

$ kubectl get psp
NAME             PRIV   CAPS   SELINUX    RUNASUSER   FSGROUP    SUPGROUP   READONLYROOTFS   VOLUMES
ack.privileged   true   *      RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            *

ECI Container Security Policy 容许通过容器平安上下文配置即可。Istio 中 Pod 注入模板文件~/bin/istio-1.13.3/manifests/charts/istio-control/istio-discovery/files/injection-template.yaml 蕴含如下代码:不启用 Istio CNI 插件 [ 8] 将增加 NET_ADMIN 和 NET_RAW 两个高权限能力:

securityContext:
  allowPrivilegeEscalation: {{.Values.global.proxy.privileged}}
  privileged: {{.Values.global.proxy.privileged}}
  capabilities:
{{- if not .Values.istio_cni.enabled}}
    add:
    - NET_ADMIN
    - NET_RAW
{{- end}}
    drop:
    - ALL

所以从原理上剖析以后 ASK 集群应用 Istio 没有兼容性问题。

总结

本次在 ASK 上试用 Istio 这类高复杂度的软件,未发现兼容性问题。又从原理上侧面剖析论证了产生兼容性问题的可能性较低。因而,ASK 对原生 Kubernetes 还是有着极好的兼容性。后续将在 ASK 集群上深刻摸索 Istio 其余性能,以进一步验证 ASK 对原生 Kubernetes 的兼容性。

参考链接:

[1] 阿里云 Serverless Kubernetes

https://help.aliyun.com/docum…

[2] Istio 发布页面

https://github.com/istio/isti…

[3] demo profile**

https://istio.io/latest/docs/…

[4] Bookinfo 示例利用

https://istio.io/latest/zh/do…

[5] 指标规定

https://istio.io/latest/docs/concepts/traffic-management/#destination-rules)中定义好可用的版本 中定义好可用的版本)

[6] ASK Pod Security Policy

https://help.aliyun.com/docum…

[7] ECI Container Security Policy

https://help.aliyun.com/docum…

[8] Istio CNI 插件

https://istio.io/latest/docs/…

点击此处,理解阿里云 ASK 更多详情和最佳实际

正文完
 0