整个任务中都使用Bookinfo示例应用程序作为示例。
安装好前面的环境
export PATH=$PATH:/root/istio-1.6.2/bin
istioctl version
[root@master ~]# istioctl manifest apply --set profile=demo
[root@master ~]# kubectl get all -nistio-system
[root@master ~]# kubectl edit deployment prometheus -nistio-system
deployment.apps/prometheus edited
image: docker.io/istio/proxyv2:1.6.2
imagePullPolicy: IfNotPresent //如果无法联网,及时修改这里的拉取策略。暂时未找到根源上的修改
[root@master ~]# kubectl edit svc istio-ingressgateway -nistio-system
//这里默认的的service.spec.type字段值为LoadBalancer,需改为NodePort,单机实验
[root@master ~]# kubectl edit svc kiali -n istio-system //账号密码均为admin
type: NodePort
[root@master ~]# kubectl label namespace default istio-injection=enabled
namespace/default labeled //default命名空间将注入istio
[root@master ~]# kubectl edit cm istio-sidecar-injector -nistio-system
configmap/istio-sidecar-injector edited
...
values: |-
{
"global": {
...
"hub": "docker.io/istio",
"imagePullPolicy": "IfNotPresent",
...修改注入时重新拉取镜像的问题
kubectl apply -f istio-1.6.2/samples/bookinfo/platform/kube/bookinfo.yaml
kubectl apply -f istio-1.6.2/samples/bookinfo/networking/bookinfo-gateway.yaml
istio Bookinfo案例包含4个微服务,其中reviews有三个不同的版本。要说明这导致的问题,在浏览器中访问Bookinfo应用程序的/productpage并刷新几次。你会注意到,有时书评输出包含星级评级,有时不包含。这是因为如果没有显式的默认服务版本来路由,Istio就会以循环的方式将请求路由到所有可用的版本。
此任务的初始目标是应用将所有流量路由到微服务的v1(版本1)的规则。稍后,您将根据HTTP请求头的值将规则应用于路由流量。
测试
多刷新几次http://192.168.1.11:30016/productpage,看到多个版本的页面
立刻登录kiali查看graph http://192.168.1.11:31228/,观察到流量从istio-ingressgateway进入后的走向
这里有4个微服务,details、productpage、ratings、reviews,reviews3个版本
要仅路由到一个版本,需要应用虚拟服务来设置微服务的默认版本。在这种情况下,虚拟服务将把所有流量路由到每个微服务的v1。
观察一下destination-rule,与pod的标签匹配关系
[root@master ~]# cat istio-1.6.2/samples/bookinfo/networking/destination-rule-all.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: productpage
spec:
host: productpage //匹配的服务名,查看svc确认
subsets:
- name: v1
labels: //符合svc的pod需有的标签
version: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v2-mysql
labels:
version: v2-mysql
- name: v2-mysql-vm
labels:
version: v2-mysql-vm
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: details
spec:
host: details
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
---
[root@master ~]# kubectl get svc productpage -oyaml
spec:
selector:
app: productpage
[root@master ~]# kubectl get svc reviews -oyaml
spec:
selector:
app: reviews //符合reviews的pod有3个标签,v1,v2,v3
[root@master ~]# kubectl describe svc reviews
Endpoints: 10.244.1.68:9080,10.244.1.70:9080,10.244.1.71:9080
[root@master ~]# kubectl get pod -owide //这与上面的Endpoints对应
NAME READY STATUS RESTARTS AGE IP NODE
reviews-v1-7bccdbbf96-fknxx 2/2 Running 0 140m 10.244.1.71 node1
reviews-v2-7c9685df46-lh5ln 2/2 Running 0 140m 10.244.1.68 node1
reviews-v3-58fc46b64-szd4w 2/2 Running 0 140m 10.244.1.70 node1
root@master ~]# kubectl get pods --show-labels //截取了部分标签
NAME READY STATUS RESTARTS AGE LABELS
productpage-v1-7df7cb7f86-xb7nr app=productpage,version=v1
details-v1-6c9f8bcbcb-qcz2w app=details,version=v1
ratings-v1-65cff55fb8-wmgtm app=ratings,version=v1
reviews-v1-7bccdbbf96-fknxx app=reviews,version=v1
reviews-v2-7c9685df46-lh5ln app=reviews,version=v2
reviews-v3-58fc46b64-szd4w app=reviews,version=v3
[root@master ~]# kubectl apply -f istio-1.6.2/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
[root@master ~]# kubectl get destinationrules
NAME HOST AGE
details details 25s
productpage productpage 25s
ratings ratings 25s
reviews reviews 25s
[root@master ~]# cat istio-1.6.2/samples/bookinfo/networking/virtual-service-all-v1.yaml
//这里的服务规则全部都route到v1上了
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: details
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
---
[root@master ~]# kubectl apply -f istio-1.6.2/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
等待几秒钟,等待虚拟服务生效。多次刷新http://192.168.1.11:30016/productpage,页面将不会发生变化。流量指向到v1版本
接下来,您将更改路由配置,以便将来自特定用户的所有流量路由到特定的服务版本。在本例中,来自一个名为Jason的用户的所有流量将被路由到reviews:v2
。
[root@master ~]# kubectl get VirtualService reviews -oyaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1 //上面的实验产生的转发给v1版本
[root@master ~]# kubectl apply -f istio-1.6.2/samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
[root@master ~]# cat istio-1.6.2/samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason //end-user报头包含jason,route到v2
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
使用以下命令确定规则的创建
[root@master ~]# kubectl get VirtualService reviews -oyaml
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
直接刷新测试结果如下:流量只走向了v1,没有登录jason(无密码),不会转向v2。v3没有配置,也不会route
[root@master ~]# kubectl delete -f istio-1.6.2/samples/bookinfo/networking/virtual-service-all-v1.yaml
//删除virtual-service,再次访问,发现能看到多个画面,恢复到之前的状态