istio
Istio 是什么?
云平台令使用它们的公司受益匪浅。但不可否认的是,上云会给 DevOps 团队带来压力。为了可移植性,开发人员必须使用微服务来构建应用,同时运维人员也正在管理着极端庞大的混合云和多云的部署环境。 Istio 允许您连接、保护、控制和观察服务。
从较高的层面来说,Istio 有助于降低这些部署的复杂性,并减轻开发团队的压力。它是一个完全开源的服务网格,作为透明的一层接入到现有的分布式应用程序里。它也是一个平台,拥有可以集成任何日志、遥测和策略系统的 API 接口。Istio 多样化的特性使您能够成功且高效地运行分布式微服务架构,并提供保护、连接和监控微服务的统一方法。
为什么使用 Istio?
通过负载均衡、服务间的身份验证、监控等方法,Istio 可以轻松地创建一个已经部署了服务的网络,而服务的代码只需很少更改甚至无需更改。通过在整个环境中部署一个特殊的 sidecar 代理为服务添加 Istio 的支持,而代理会拦截微服务之间的所有网络通信,然后使用其控制平面的功能来配置和管理 Istio,这包括:
-
为 HTTP、gRPC、WebSocket 和 TCP 流量自动负载均衡。
-
通过丰富的路由规则、重试、故障转移和故障注入对流量行为进行细粒度控制。
-
可插拔的策略层和配置 API,支持访问控制、速率限制和配额。
-
集群内(包括集群的入口和出口)所有流量的自动化度量、日志记录和追踪。
-
在具有强大的基于身份验证和授权的集群中实现安全的服务间通信。
部署istio
前提条件
kubernetes 集群 基于centos7 搭建 1.14 kubernetes 集群
下载 istio
//解压
[root@master ~]# tar -xf istio-1.13.3-linux-amd64.tar.gz -C /usr/local/
[root@master istio]# mv istio-1.13.3/ istio
[root@master istio]# ls
bin LICENSE manifests manifest.yaml README.md samples tools
//添加环境变量
[root@master ~]# echo 'export PATH=/usr/local/istio/bin:$PATH' > /etc/profile.d/istio.sh
[root@master ~]# source /etc/profile.d/istio.sh
[root@master ~]# echo $PATH
/usr/local/istio/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@master istio]# istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Ingress gateways installed
✔ Egress gateways installed
✔ Installation complete Making this installation the default for injection and validation.
Thank you for installing Istio 1.13. Please take a few minutes to tell us about your install/upgrade experience! https://forms.gle/pzWZpAvMVBecaQ9h9
[root@master istio]# kubectl get ns
NAME STATUS AGE
default Active 31d
istio-system Active 46h
kube-node-lease Active 31d
kube-public Active 31d
kube-system Active 31d
[root@master istio]# kubectl describe ns default
Name: default
Labels: istio-injection=enabled
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
//添加一个命名空间标签,以指示 Istio 在稍后部署应用程序时自动注入 Envoy sidecar 代理
[root@master istio]# kubectl label namespace default istio-injection=enabled
namespace/default labeled
//到这里安装设置就已经完成了,我们使用bookinfo进行一下测试
[root@master istio]# kubectl apply -f 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
注:这里当前目录还在istio下 可以看到里面自带了samples文件
//检查运行情况
[root@master istio]# kubectl get svc,pod
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/details ClusterIP 10.98.204.154 <none> 9080/TCP 3m46s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29d
service/productpage ClusterIP 10.100.29.167 <none> 9080/TCP 3m46s
service/ratings ClusterIP 10.99.198.48 <none> 9080/TCP 3m46s
service/reviews ClusterIP 10.106.113.109 <none> 9080/TCP 3m46s
NAME READY STATUS RESTARTS AGE
pod/details-v1-79f774bdb9-ckph9 2/2 Running 0 3m46s
pod/productpage-v1-6b746f74dc-qsmm6 2/2 Running 0 3m46s
pod/ratings-v1-b6994bb9-j5fb5 0/2 Running 0 3m46s
pod/reviews-v1-545db77b95-jfhr7 0/2 Running 0 3m46s
pod/reviews-v2-7bf8c9648f-sdlvj 0/2 Running 0 3m46s
pod/reviews-v3-84779c7bbc-dz6w6 0/2 Running 0 3m46s
//验证启动是否成功
[root@master istio]# kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -sS productpage:9080/productpage | grep -o "<title>.*</title>"
<title>Simple Bookstore App</title> #返回这个命令则证明成功启动
//创建 Ingress Gateway 用以管理入口流量,这样我们就可以直接本地访问bookinfo了,同样文件也在sample中 注意执行时的位置
[root@master istio]# kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created
//检查istio是否有问题,显示No validation issues found when analyzing namespace: default则证明没问题
[root@master istio]# istioctl analyze
✔ No validation issues found when analyzing namespace: default.
//查看getway
[root@master istio]# kubectl get gateway
NAME AGE
bookinfo-gateway 100s
bookinfo 部署
操作步骤
在使用 kubectl apply 进行应用部署的时候,如果目标命名空间已经打上了标签 istio-injection=enabled,Istio sidecar injector 会自动把 Envoy 容器注入到你的应用 Pod 之中。
创建命名空间
[root@master istio]# kubectl create ns bookinfo
namespace/bookinfo created
添加label
//添加 label:
[root@master istio]# kubectl label ns bookinfo istio-injection=enabled
namespace/bookinfo labeled
//查看 label:
[root@master istio]# kubectl describe ns bookinfo
Name: bookinfo
Labels: istio-injection=enabled
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
部署 bookinfo
//部署
[root@master istio]# kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -n bookinfo
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
//获取 pod:
[root@master istio]# kubectl get pod -n bookinfo
NAME READY STATUS RESTARTS AGE
details-v1-79f774bdb9-z2wt5 2/2 Running 0 24s
productpage-v1-6b746f74dc-mp7sw 2/2 Running 0 23s
ratings-v1-b6994bb9-x2tsm 2/2 Running 0 24s
reviews-v1-545db77b95-scxtm 2/2 Running 0 24s
reviews-v2-7bf8c9648f-lh9hq 2/2 Running 0 24s
reviews-v3-84779c7bbc-g9cfz 2/2 Running 0 24s
//确认app是否正常:
[root@master istio]# kubectl exec -it $(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}') -c ratings -- curl productpage:9080/productpage | grep -o "<title>.*</title>"
<title>Simple Bookstore App</title>
确认集群是否支持 负载均衡
执行以下命令以确定您的 Kubernetes 集群是否在支持外部负载均衡器的环境中运行
[root@master istio]# kubectl get svc istio-ingressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.108.251.167 <pending> 15021:31331/TCP,80:31258/TCP,443:30957/TCP,31400:31680/TCP,15443:30788/TCP 3m18s
如果 EXTERNAL-IP 有值(IP 地址或主机名),则说明您的环境具有可用于 Ingress 网关的外部负载均衡器。如果EXTERNAL-IP 值是 (或一直是 ),则说明可能您的环境并没有为 Ingress 网关提供外部负载均衡器的功能。在这种情况下,您可以使用 Service 的 node port 方式访问网关
设置 istio-ingressgateway service 为 node port
//修改 service type
[root@master istio]# kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}'
service/istio-ingressgateway patched
//查看service
[root@master istio]# kubectl get svc istio-ingressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway NodePort 10.108.251.167 <none> 15021:31331/TCP,80:31258/TCP,443:30957/TCP,31400:31680/TCP,15443:30788/TCP 4m2s
部署 istio gateway
[root@master istio]# kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n bookinfo
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created
//查看 gateway
[root@master istio]# kubectl get gateway -n bookinfo
NAME AGE
bookinfo-gateway 21s
//设置 INGRESS_PORT
[root@master istio]# export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
//确认 port
[root@master istio]# echo $INGRESS_PORT
31258
//应用缺省目标规则
[root@master istio]# kubectl apply -f samples/bookinfo/networking/destination-rule-all-mtls.yaml -n bookinfo
destinationrule.networking.istio.io/productpage created
destinationrule.networking.istio.io/reviews created
destinationrule.networking.istio.io/ratings created
destinationrule.networking.istio.io/details created
测试
浏览器访问 http://hostip:PORT/productpage
刷新浏览器,看到星级评论发生变化,好了你已经成功部署istio 的demo。
权重路由以及监控
前提
- kubernetes 集群 基于centos7 搭建 1.14 kubernetes 集群
- Istio 1.2.4: istio 实战 一 Kubernetes 中快速搭建 istio
- bookinfo: istio 实战 二 bookinfo 部署
本任务将演示如何逐步将流量从一个版本的微服务迁移到另一个版本。例如,您可以将流量从旧版本迁移到新版本。
一个常见的用例是将流量从一个版本的微服务逐渐迁移到另一个版本。在 Istio 中,您可以通过配置一系列规则来实现此目标,这些规则将一定百分比的流量路由到一个或另一个服务。在此任务中,您将 50% 的流量发送到 reviews:v1,另外 50% 的流量发送到 reviews:v3。然后将 100% 的流量发送到 reviews:v3 来完成迁移。
任务
将 50% 的流量从 reviews:v1 转移到 reviews:v3
操作步骤
1. 流量全部切换到 reviews:v1 版本
[root@master istio]# kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml -n bookinfo
virtualservice.networking.istio.io/productpage created
virtualservice.networking.istio.io/reviews created
virtualservice.networking.istio.io/ratings created
virtualservice.networking.istio.io/details created
浏览器访问 http://hostip:port/productpage
不管刷新多少次,页面的评论部分都不会显示评级星号,因为 Istio 被配置为将 reviews 服务的的所有流量都路由到了 reviews:v1 版本, 而该版本的服务不会访问带星级的 ratings 服务。
2. 切换50%流量到 reviews:v3 版本
[root@master istio]# kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml -n bookinfo
virtualservice.networking.istio.io/reviews configured
浏览器访问 http://hostip:port/productpage
刷新浏览器中的 /productpage 页面,大约有 50% 的几率会看到页面中出带红色星级的评价内容。这是因为 v3 版本的 reviews 访问了带星级评级的 ratings 服务,但 v1 版本却没有。
通过 Dashboard 监控流量
istio 内置了 Grafana 和 prometheus。
prometheus: 用来收集指标,并在 Prometheus 服务中查询 Istio 指标。
Grafana: 通过 Grafana Dashboard 对服务网格中的流量进行监控。
[root@master istio]# kubectl get pod -n istio-system
NAME READY STATUS RESTARTS AGE
grafana-69ccf87b97-g49f7 1/1 Running 0 3m39s
istio-egressgateway-5f686bbdfd-f59sv 1/1 Running 0 63m
istio-ingressgateway-699d557cd4-cd7jq 1/1 Running 0 63m
istiod-5458bd7bc4-6nrtr 1/1 Running 0 63m
jaeger-648f4f4ddb-rpt46 1/1 Running 0 3m39s
kiali-5ccffdfd97-kzrwn 1/1 Running 0 3m38s
prometheus-f675ff955-cdjnj 0/2 Running 0 3m38s
1. 检测 prometheus
[root@master istio]# kubectl -n istio-system get svc prometheus
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
prometheus ClusterIP 10.102.164.121 <none> 9090/TCP 5m25s
2. 检测 Grafana
[root@master istio]# kubectl -n istio-system get svc grafana
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
grafana ClusterIP 10.98.87.3 <none> 3000/TCP 5m40s
3. 添加端口
由于使用的是ClusterIP 类型集群外部无法访问 ,通过端口转发映射本地端口到指定的应用端口.
kubectl -n istio-system port-forward $(kubectl -n istio-system get pod -l app=grafana -o jsonpath='{.items[0].metadata.name}') --address 0.0.0.0 3000:3000 &
在 Web 浏览器中访问 http://localhost:3000/dashboard/db/istio-mesh-dashboard
卸载
//删除项目脚本
[root@master istio]# samples/bookinfo/platform/kube/cleanup.sh
//删除istio
[root@master istio]# istioctl manifest generate --set profile=demo | kubectl delete -f -
//删除istio的命名空间
[root@master istio]# kubectl delete namespace istio-system
//删除istio的自动注入sidecar
[root@master istio]# kubectl label namespace default istio-injection-
istio内安装Prometheus,Grafana等插
部署istio有多种模式,即便以demo模式部署仍然发现无法成功部署部分组件
[root@master istio]# kubectl get pods -n istio-system
NAME READY STATUS RESTARTS AGE
istio-egressgateway-5f686bbdfd-f59sv 1/1 Running 0 59m
istio-ingressgateway-699d557cd4-cd7jq 1/1 Running 0 59m
istiod-5458bd7bc4-6nrtr 1/1 Running 0 59m
那么如何安装其他组件了?
[root@master istio]# kubectl apply -f samples/addons #添加其他组件,所有组件全部部署
serviceaccount/grafana created
configmap/grafana created
service/grafana created
deployment.apps/grafana created
configmap/istio-grafana-dashboards created
configmap/istio-services-grafana-dashboards created
deployment.apps/jaeger created
service/tracing created
service/jaeger-collector configured
serviceaccount/kiali created
configmap/kiali created
clusterrole.rbac.authorization.k8s.io/kiali-viewer created
clusterrole.rbac.authorization.k8s.io/kiali created
clusterrolebinding.rbac.authorization.k8s.io/kiali created
role.rbac.authorization.k8s.io/kiali-controlplane created
rolebinding.rbac.authorization.k8s.io/kiali-controlplane created
service/kiali created
deployment.apps/kiali created
serviceaccount/prometheus created
configmap/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
service/prometheus created
deployment.apps/prometheus created
例如运行kiali
[root@master istio]# 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