istio1.16.3部署-快速入门
Istio安装与使用
注:本文基于istio-1.16.3编写

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.16.3/istio-1.16.3-linux-amd64.tar.gz
解压后,把istioctl拷贝到系统环境变量path路径中,
[root@k8s111 ~]# tar xf istio-1.16.3-linux-amd64.tar.gz
[root@k8s111 ~]# cd istio-1.16.3/
[root@k8s111 istio-1.16.3]# cp bin/istioctl /usr/local/bin/
2.2 通过istioctl安装istio
[root@k8s111 istio-1.16.3]# istioctl install --set profile=demo --set hub=registry-1.docker.io/istio
This will install the Istio 1.16.3 demo profile with ["Istio core" "Istiod" "Ingress gateways" "Egress gateways"] components into the cluster. Proceed? (y/N) 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.16. Please take a few minutes to tell us about your install/upgrade experience! https://forms.gle/99uiMML96AmsXY5d6
profile设置为demo,此时会安装Istiod,ingressgateway和egressgateway,hub设置为自己搭建的私有镜像仓库,可以将其他渠道获得的镜像放入自己的registry中,方便使用。
完成后,我们就能看到对应的pod,默认安装在istio-system namespace中,
[root@k8s111 istio-1.16.3]# kubectl -n istio-system get po
NAME READY STATUS RESTARTS AGE
istio-egressgateway-75f48846c-2cmbl 1/1 Running 0 4m37s
istio-ingressgateway-5b98b564c-74j7b 1/1 Running 0 4m37s
istiod-6d9cc5bf74-wzvth 1/1 Running 0 5m24s
3 安装bookinfo应用
3.1 创建bookinfo namespace
我们新建一个namespace用于demo应用的部署
[root@k8s111 istio-1.16.3]# kubectl create ns bookinfo
3.2 添加label
因为Istio proxy的注入是基于label,因此我们需要为demo namespace添加label,
[root@k8s111 istio-1.16.3]# kubectl label namespace bookinfo istio-injection=enabled
namespace/bookinfo labeled
[root@k8s111 istio-1.16.3]# kubectl get ns --show-labels bookinfo
NAME STATUS AGE LABELS
bookinfo Active 77s istio-injection=enabled,kubernetes.io/metadata.name=bookinfo
3.3 部署bookinfo
先将镜像仓库替换为自己的库,然后直接apply就行,
# 这里我们没有自己的仓库所以就不替换了。
[root@k8s111 istio-1.16.3]# sed -i 's/image: docker.io/image: my-registry/g' samples/bookinfo/platform/kube/bookinfo.yaml
[root@k8s111 istio-1.16.3]# kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -n bookinfo
[root@k8s111 istio-1.16.3]# 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@k8s111 istio-1.16.3]# 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 添加路由规则
服务部署后,还需要添加路由规则,将请求路由到对应的服务,
[root@k8s111 istio-1.16.3]# kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n bookinfo
3.5 访问服务
3.5.1 通过nodeport
- 获取host ip,也就是ingressgateway pod所在机器ip,
[root@k8s111 istio-1.16.3]# kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}'
10.0.0.112
- 获取port,也就是80端口映射的目的端口,即31087
[root@k8s111 istio-1.16.3]# kubectl -n istio-system get service istio-ingressgateway
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.96.234.27 <pending> 15021:30125/TCP,80:31087/TCP,443:30613/TCP,31400:30321/TCP,15443:32037/TCP 27m
实际访问:
10.0.0.112:31087/productpage

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": ["10.0.0.113"] }}'
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/
istio流量控制有哪些策略
Istio 提供了多种流量控制策略,用于管理和控制微服务之间的通信流量。以下是一些常见的 Istio 流量控制策略:
1、超时控制(Timeouts):定义请求的最大等待时间,超过该时间将被中断。
2、重试控制(Retries):在请求失败时,自动进行重试操作,以增加请求成功的机会。
3、熔断器(Circuit Breakers):监测后端服务的状态,并在服务出现故障时快速中断请求,避免资源浪费和延迟增加。
4、故障注入(Fault Injection):模拟后端服务出现故障的情况,以测试系统的容错能力。
5、分流(Traffic Shifting):根据指定的权重或规则将流量分配给不同版本的服务,实现灰度发布或 A/B 测试。
6、负载均衡(Load Balancing):在多个实例之间分配请求流量,以确保负载均衡和高可用性。
7、策略重定向(Policy Redirection):根据特定的策略将请求流量重定向到不同的服务实例或版本。
8、请求镜像(Request Mirroring):将请求流量复制到另一个目标服务,以进行并行测试或监控。
9、请求限制(Request Throttling):限制对服务的并发请求数量或请求速率,以防止过载。
10、智能路由(Intelligent Routing):根据请求的属性或条件进行动态路由,以实现更高级的流量控制。
这些策略可以通过 Istio 的流量管理功能进行配置和管理。您可以使用 Istio 的配置资源(例如 VirtualService、DestinationRule、Gateway 等)来定义和应用这些策略。请注意,具体的配置和使用方法可能因 Istio 版本和您的需求而有所不同,建议参考 Istio 的官方文档和指南以获取更详细的信息和示例。

浙公网安备 33010602011771号