注:本文基于istio-1.13.4编写
1 关于Istio
Istio是Service Mesh模式的一种实现,多用于微服务的治理。Kubernetes能够覆盖服务的部署、升级、扩容等运行管理能力,但对于服务治理,如服务的熔断、限流、动态路由、调用链追踪就无能为力。因此Istio就能和Kubernetes互补,成为微服务管理的最佳实践之一。
Istio的核心思想就是将服务治理的功能从业务服务中独立出来,作为一个sidecar容器,解耦的同时也能够兼容不同语言,无需和业务服务使用同一套语言。公共的治理能力独立后,所有组件都可以接入,而且采用sidecar的方式让业务无需任何修改即可接入。
Istio主要提供四个特性:
- 流量管理:在实现服务连接的基础上,通过控制服务间的流量和调用,实现请求路由、负载均衡、超时、重试、熔断、故障注入、重定向等功能
- 安全:提供证书配置管理,以及服务访问认证、授权等安全能力
- 策略控制:提供访问速率限制能力。
- 观测:获取服务运行指标和输出,提供调用链追踪和日志收集能力。
2 安装Istio
2.1 下载安装包
以最新release版本为例,
wget https://github.com/istio/istio/releases/download/1.13.4/istio-1.13.4-linux-amd64.tar.gz
解压后,把istioctl拷贝到系统环境变量path路径中,
[root@master istio]# tar -xf istio-1.13.4-linux-amd64.tar.gz
[root@master istio]# cd istio-1.13.4/
[root@master istio-1.13.4]# cp bin/istioctl /usr/local/bin/
2.2 通过istioctl安装istio
[root@master istio-1.13.4]# istioctl install --set profile=demo --set hub=registry-1.docker.io/istio
This will install the Istio 1.13.4 demo profile with ["Istio core" "Istiod" "Ingress gateways" "Egress gateways"] components into the cluster. Proceed? (y/N) y
✔ Istio core installed
✔ Istiod installed
✔ Egress gateways installed
✔ Ingress gateways installed
✔ Installation complete
Making this installation the default for injection and validation.
...
profile设置为demo,此时会安装Istiod,ingressgateway和egressgateway,
hub设置为自己搭建的私有镜像仓库,可以将其他渠道获得的镜像放入自己的registry中,方便使用。
完成后,我们就能看到对应的pod,默认安装在istio-system namespace中,
[root@master istio-1.13.4]# kubectl get pod -n istio-system
NAME READY STATUS RESTARTS AGE
istio-egressgateway-5dc6c98fbc-vdlml 1/1 Running 0 3d1h
istio-ingressgateway-87bbdd549-8776n 1/1 Running 0 3d1h
istiod-56b7b78cb5-94c69 1/1 Running 0 3d1h
3 安装bookinfo应用
3.1 创建bookinfo namespace
我们新建一个namespace用于demo应用的部署
kubectl create ns bookinfo
3.2 添加label
因为Istio proxy的注入是基于label,因此我们需要为demo namespace添加label,
[root@master ~]# kubectl label namespace bookinfo istio-injection=enabled
[root@master ~]# kubectl get ns --show-labels bookinfo
NAME STATUS AGE LABELS
bookinfo Active 4d12h istio-injection=enabled,kubernetes.io/metadata.name=bookinfo
3.3 部署bookinfo
先将镜像仓库替换为自己的库,然后直接apply就行,
[root@master istio-1.13.4]# sed -i 's/image: docker.io/image: my-registry/g' samples/bookinfo/platform/kube/bookinfo.yaml
[root@master istio-1.13.4]# kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -n bookinfo
[root@master istio-1.13.4]# kubectl get pod -n bookinfo
NAME READY STATUS RESTARTS AGE
details-v1-657d49f566-58tfz 2/2 Running 0 4d
productpage-v1-64b66f8976-zpn88 2/2 Running 0 4d
ratings-v1-567b968c8-67tqb 2/2 Running 0 4d10h
reviews-v1-8bff59b-tbkrp 2/2 Running 0 4d
reviews-v2-5c8cdd5b76-st9m9 2/2 Running 0 4d
reviews-v3-dcb96c9fb-t6rj4 2/2 Running 0 4d
然后我们可查看应用pod里的容器信息,可以看到已经被注入istio-proxy,
[root@master istio-1.13.4]# kubectl get pod productpage-v1-64b66f8976-hsvdb -n bookinfo -o jsonpath='{.status.containerStatuses}' | jq
[
{
"containerID": "docker://83156e219de60dd69084744fa75ba4116cd4c804002b0439387adc7bf2f3153e",
"image": "my-registry/istio/proxyv2:1.13.4",
...
"name": "istio-proxy",
"ready": true,
"restartCount": 0,
"started": true,
...
},
{
"containerID": "docker://b99a84708d9fdc967eb3fd2c47726911dba4ffec4e3e32ceb0a67e6ed5d3dbd4",
"image": "my-registry/istio/examples-bookinfo-productpage-v1:1.16.2",
...
"name": "productpage",
"ready": true,
...
}
]
3.4 添加路由规则
服务部署后,还需要添加路由规则,将请求路由到对应的服务,
kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n bookinfo
3.5 访问服务
3.5.1 通过nodeport
- 获取host ip,也就是ingressgateway pod所在机器ip,
kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}'
- 获取port,也就是80端口映射的目的端口,即30579
[root@master istio-1.13.4]# kubectl -n istio-system get service istio-ingressgateway
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.111.130.87 <pending> 15021:31042/TCP,80:30579/TCP,443:32271/TCP,31400:30485/TCP,15443:31231/TCP 4d10h
实际访问:
3.5.2 通过externalip
因为我们是本地测试,肯定没法使用公网的LB,因此我们可以直接将externalip修改为某个node的ip,这样就能通过80端口访问,
[root@master istio-1.13.4]# kubectl -n istio-system get service istio-ingressgateway
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.111.130.87 <pending> 15021:31042/TCP,80:30579/TCP,443:32271/TCP,31400:30485/TCP,15443:31231/TCP 4d10h
[root@master istio-1.13.4]# kubectl patch svc istio-ingressgateway --namespace istio-system --patch '{"spec": { "externalIPs": ["192.168.0.181"] }}'
service/istio-ingressgateway patched
[root@master istio-1.13.4]# kubectl -n istio-system get service istio-ingressgateway
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.111.130.87 192.168.0.181 15021:31042/TCP,80:30579/TCP,443:32271/TCP,31400:30485/TCP,15443:31231/TCP 4d10h
4 卸载istio
istioctl x uninstall --purge
参考文档:
- https://istio.io/latest/docs/setup/getting-started/
- https://istio.io/latest/docs/tasks/traffic-management/ingress/ingress-control/
- https://istio.io/latest/docs/tasks/