istio快速开始
Bookinfo应用示例
确认Bookinfo的Service与Pod都已正常
安装istio
1、下载istio
https://github.com/istio/istio/releases
这里我们下载最新版本istio-1.1.7-linux.tar.gz
,解压后结构如下:
istio-1.1.7
├── bin (dir)
├── install (dir)
├── istio.VERSION
├── LICENSE
├── README.md
├── samples (dir)
└── tools (dir)
2、安装crd
$ for i in install/kubernetes/helm/istio-init/files/crd*yaml; do kubectl apply -f $i; done
3、安装一个demo
$ kubectl apply -f install/kubernetes/istio-demo.yaml
4、确认部署结果
确认下列服务(除jaeger-agent
外)已经部署并都具有各自的CLUSTER-IP
$ kubectl get service -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
grafana ClusterIP 10.105.55.147 <none> 3000/TCP 25s
istio-citadel ClusterIP 10.100.67.233 <none> 8060/TCP,15014/TCP 25s
istio-egressgateway ClusterIP 10.109.167.188 <none> 80/TCP,443/TCP,15443/TCP 26s
istio-galley ClusterIP 10.108.40.187 <none> 443/TCP,15014/TCP,9901/TCP 26s
istio-ingressgateway LoadBalancer 10.97.152.170 <pending> 15020:32638/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:31280/TCP,15030:30824/TCP,15031:32571/TCP,15032:32492/TCP,15443:30085/TCP 25s
istio-pilot ClusterIP 10.110.241.218 <none> 15010/TCP,15011/TCP,8080/TCP,15014/TCP 25s
istio-policy ClusterIP 10.110.26.166 <none> 9091/TCP,15004/TCP,15014/TCP 25s
istio-sidecar-injector ClusterIP 10.103.86.111 <none> 443/TCP 24s
istio-telemetry ClusterIP 10.99.96.214 <none> 9091/TCP,15004/TCP,15014/TCP,42422/TCP 25s
jaeger-agent ClusterIP None <none> 5775/UDP,6831/UDP,6832/UDP 22s
jaeger-collector ClusterIP 10.102.127.236 <none> 14267/TCP,14268/TCP 22s
jaeger-query ClusterIP 10.102.4.224 <none> 16686/TCP 22s
kiali ClusterIP 10.100.98.28 <none> 20001/TCP 25s
prometheus ClusterIP 10.103.33.9 <none> 9090/TCP 25s
tracing ClusterIP 10.110.134.92 <none> 80/TCP 21s
zipkin ClusterIP 10.99.173.56 <none> 9411/TCP 22s
如果你的集群在一个没有外部负载均衡器支持的环境中运行,istio-ingressgateway 的 EXTERNAL-IP 会是<pending>
。要访问这个网关,只能通过服务的 NodePort 或者使用端口转发来进行访问
确认必要的 Kubernetes Pod 都已经创建并且其STATUS
的值是Running
$ kubectl get pod -n istio-system
kubectl get pod -n istio-system
NAME READY STATUS RESTARTS AGE
grafana-67c69bb567-tprnt 1/1 Running 0 19h
istio-citadel-fc966574d-psht5 1/1 Running 1 19h
istio-cleanup-secrets-1.1.7-6fz75 0/1 Completed 0 19h
istio-egressgateway-6b4cd4d9f-nxjbs 1/1 Running 0 19h
istio-galley-cf776876f-xbzvx 1/1 Running 0 19h
istio-grafana-post-install-1.1.7-z5jd4 0/1 Completed 0 19h
istio-ingressgateway-59cc6ccbcb-bqdc5 1/1 Running 0 19h
istio-pilot-7b4dd9b748-lxblg 2/2 Running 0 19h
istio-policy-5bcc859488-2r52k 2/2 Running 44 19h
istio-security-post-install-1.1.7-ljpgz 0/1 Completed 0 19h
istio-sidecar-injector-c8ddbb99c-qq8q2 1/1 Running 1 19h
istio-telemetry-7678c9bb4d-l2m49 2/2 Running 44 19h
istio-tracing-5d8f57c8ff-rvkz5 1/1 Running 0 19h
kiali-d4d886dd7-wfh62 1/1 Running 0 19h
prometheus-d8d46c5b5-8pjwl 1/1 Running 0 19h
Bookinfo应用示例
Bookinfo应用的架构图如下,它包含四个微服务:productpage
、reviews
、details
及ratings
,微服务之间的调用关系如图。
其中reviews
服务有三个版本v1
、v2
和v3
:
v2
会调用rating
服务,并且会将评分以黑色的1到5颗星展示出来
v3
会调用rating
服务,并且会将评分以红色的1到5颗星展示出来
1、为命名空间开启自动注入功能
由于我们的Bookinfo是部署在default
命名空间下,我们为default
命名空间打上如下标签
$ kubectl label namespace default istio-injection=enabled
2、部署Bookinfo应用
bookinfo已经在istio的tar包中了
$ kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
上面的命令会启动全部的四个服务,其中也包括了reviews
服务的三个版本(三个Deployment
、一个Service
)
3、确认Bookinfo的Service与Pod都已正常
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
details ClusterIP 10.98.169.211 <none> 9080/TCP 80s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
productpage ClusterIP 10.100.178.46 <none> 9080/TCP 79s
ratings ClusterIP 10.111.212.42 <none> 9080/TCP 80s
reviews ClusterIP 10.98.255.114 <none> 9080/TCP 80s
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
details-v1-65b966b497-nhln8 2/2 Running 0 7m34s
productpage-v1-79458795bc-kfw74 2/2 Running 0 7m33s
ratings-v1-5b7cd6c58f-p2fdc 2/2 Running 0 7m34s
reviews-v1-54c7c79486-dkmrq 2/2 Running 0 7m33s
reviews-v2-7dc5785684-vvbg8 2/2 Running 0 7m33s
reviews-v3-6c464d7bf4-2p27r 2/2 Running 0 7m33s
4、验证从内部能访问应用
从集群的某个台主机上使用ClusterIP
访问productpage
这个服务,ClusterIP
在上面的kubectl get service
中可以查到
$ curl 10.100.178.46:9080/productpage
5、从外部能访问应用
现在Bookinfo
这个应用已经起来了,我们需要让它能够从集群外部访问,比如浏览器。为了达到这个目标,我们需要使用Istio Gateway
5.1 为应用定义入口网关
$ kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
确认gateway
与virtualservice
已创建好
$ kubectl get gateway
NAME AGE
bookinfo-gateway 90s
$ kubectl get virtualservice
NAME GATEWAYS HOSTS AGE
bookinfo [bookinfo-gateway] [*] 98s
5.2 使用NodePort访问应用
通过如下的命令获取istio-ingressgateway
服务的http2
协议的NodePort
$ kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}'
然后在浏览器中使用URLhttp://<IP>:<NodePort>/productpage
来访问应用;其中IP
是任意一个工作节点的IP。多刷新几次,我们会发现productpage
会调用reviews
服务的不同版本
如果productpage
调用了reviews-v1
,界面如下,没有星
如果productpage
调用了reviews-v2
,界面如下,有黑色的星
如果productpage
调用了reviews-v3
,界面如下,有红色的星
6、智能路由示例
6.1 设置目标规则
$ kubectl apply -f samples/bookinfo/networking/destination-rule-all.yaml
验证创建成功
$ kubectl get destinationrule
NAME HOST AGE
details details 50s
productpage productpage 50s
ratings ratings 50s
reviews reviews 50s
6.2 配置一个路由策略
接下来我们配置一个路由策略,让所有的访问都导向reviews
的v1
版本。执行如下命令创建virtual service
$ kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml
确认部署结果
$ kubectl get virtualservice
NAME GATEWAYS HOSTS AGE
bookinfo [bookinfo-gateway] [*] 48m
details [details] 23s
productpage [productpage] 23s
ratings [ratings] 23s
reviews [reviews] 23s
然后从浏览器访问应用,多刷几次发现永远都是v1
版本
6.3 配置另一个路由策略
我们在上面的基础上,再增加一种路由策略:基于用户的路由。在这种策略下,所有用户名为Jason
的请求都会路由到服务reviews:v2
上去。
注意,istio并没有内置的用户身份,该效果主要是通过在productpage
的请求头的添加了一个end-user
字段。
执行以下的创建基于用户的路由策略
$ kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
virtualservice.networking.istio.io/reviews configured
然后在/productpage
页面,用jason
登录,多刷几次
卸载
1、删除Bookinfo示例的所有资源
在istio的根目录下执行
$ kubectl delete -R -f samples/bookinfo
2、去掉命名空间的label
$ kubectl label namespace default istio-injection-
3、卸载istio
$ kubectl delete -f install/kubernetes/istio-demo.yaml
$ for i in install/kubernetes/helm/istio-init/files/crd*yaml; do kubectl delete -f $i; done
Reference
https://istio.io/docs/setup/kubernetes/install/kubernetes/
https://istio.io/docs/examples/bookinfo/
*https://istio.io/docs/tasks/traffic-management/ingress/#determining-the-ingress-ip-and-ports-when-using-a-node-port
https://istio.io/docs/tasks/traffic-management/request-routing/