在学习完istio注入后mac 上学习k8s系列(41)istio 注入,我们如何用istio来进行流量管理呢,用到了istio的crd VirtualService,首先我们来搭建学习环境:
apiVersion: apps/v1kind: Deploymentmetadata:name: clientspec:replicas: 1selector:matchLabels:app: clienttemplate:metadata:labels:app: clientspec:containers:- name: busyboximage: busyboximagePullPolicy: IfNotPresentcommand: ["/bin/sh", "-ce", "sleep 3600;tail -f dev/null"]
apiVersion: apps/v1kind: Deploymentmetadata:name: httpdlabels:server: httpdapp: webspec:replicas: 2selector:matchLabels:server: httpdapp: webtemplate:metadata:name: httpdlabels:server: httpdapp: webspec:containers:- name: busyboximage: busyboximagePullPolicy: IfNotPresentcommand: ["/bin/sh","-ce","touch index.html;echo 'hello httpd' >index.html;httpd -p 8080;tail -f dev/null"]---apiVersion: apps/v1kind: Deploymentmetadata:name: tomcatlabels:server: tomcatapp: webspec:replicas: 2selector:matchLabels:server: tomcatapp: webtemplate:metadata:name: tomcatlabels:server: tomcatapp: webspec:containers:- name: tomcatimage: docker.io/library/tomcat:latestimagePullPolicy: IfNotPresent
apiVersion: v1kind: Servicemetadata:name: tomcat-svcspec:selector:server: tomcatports:- name: httpport: 8080targetPort: 8080protocol: TCP---apiVersion: v1kind: Servicemetadata:name: httpd-svcspec:selector:server: httpdports:- name: httpport: 8080targetPort: 8080protocol: TCP---apiVersion: v1kind: Servicemetadata:name: web-svcspec:selector:app: webports:- name: httpport: 8080targetPort: 8080protocol: TCP
启动服务
% kubectl create ns vcnamespace/vc created% kubectl apply -f ./vitural-service/client.yaml -n vcdeployment.apps/client created% kubectl apply -f ./vitural-service/deploy.yaml -n vcdeployment.apps/httpd createddeployment.apps/tomcat created% kubectl apply -f ./vitural-service/service.yaml -n vcservice/tomcat-svc createdservice/httpd-svc created
启动服务的时候如果出现下面的错误,原因是kubelet会检测是否有前台的服务,没有的话会重启
Warning BackOff 37s (x10 over 2m15s) kubelet Back-off restarting failed container
所以需要在command上加一个前台服务
command: [ "/bin/bash", "-ce", "tail -f dev/null" ]
查看服务
% kubectl -n vc get endpointsNAME ENDPOINTS AGEhttpd-svc 10.1.4.164:8080,10.1.4.165:8080 9htomcat-svc 10.1.4.162:8080,10.1.4.163:8080 9hweb-svc 10.1.4.162:8080,10.1.4.163:8080,10.1.4.164:8080 + 1 more... 2m3s
检测下服务,分别看下svc-httpd和svc-tomcat
% kubectl -n vc exec -it client-5469d56b7f-9kdz8 -- wget -q -O - http://httpd-svc:8080hello httpd% kubectl -n vc exec -it client-5469d56b7f-9kdz8 -- wget -q -O - http://tomcat-svc:8080wget: server returned error: HTTP/1.1 404command terminated with exit code 1
看下web-svc,发现它访问两个下游服务的概率都是50%
% kubectl -n vc exec -it client-5469d56b7f-9kdz8 -- wget -q -O - http://web-svc:8080hello httpd% kubectl -n vc exec -it client-5469d56b7f-9kdz8 -- wget -q -O - http://web-svc:8080wget: server returned error: HTTP/1.1 404command terminated with exit code 1
那么,我们应该如何控制流量不均匀分布呢?一个服务20% 一个80%?通过label的方式就解决不了了,需要vitural service
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: web-svc-vsspec:hosts:- web-svchttp:- route:- destination:host: tomcat-svcweight: 20- destination:host: httpd-svcweight: 80
配置文件和nginx的location 的配置很像,它包括两个部分host fields,匹配存在的一个服务名,routing rules可以定义规则实现流量的分发。同时需要注意的是,vitural service必须在istio注入后才能生效。
% kubectl -n vc apply -f vitural-service/vs.yamlvirtualservice.networking.istio.io/web-svc-vs created
% kubectl -n vc get VirtualServiceNAME GATEWAYS HOSTS AGEweb-svc-vs ["web-svc"] 45s
开始注入
% istioctl kube-inject -f vitural-service/client.yaml |kubectl -n vc apply -f -deployment.apps/client configured% istioctl kube-inject -f vitural-service/deploy.yaml |kubectl -n vc apply -f -deployment.apps/httpd configureddeployment.apps/tomcat configured
可以看到我们的服务注入成功
% kubectl get pods -n vcNAME READY STATUS RESTARTS AGEclient-78b5c977-xb27z 2/2 Running 0 3m48shttpd-6cbf88b69c-l4dwn 2/2 Running 0 2m43shttpd-6cbf88b69c-sm5xn 2/2 Running 0 3m28stomcat-7d754cc9b4-49sd2 2/2 Running 0 3m28stomcat-7d754cc9b4-rqz5t 2/2 Running 0 2m28s
试下,可以看到流量的分配比例是8:2
% kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpdxiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080wget: server returned error: HTTP/1.1 404 Not Foundcommand terminated with exit code 1xiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080wget: server returned error: HTTP/1.1 404 Not Foundcommand terminated with exit code 1xiazemin@xiazemindeMBP 实战 % kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080hello httpd
除了可以按照比例分配流量外,还可以按照条件来分配流量
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: web-svc-vs-headerspec:hosts:- web-svchttp:- match:- headers:end-user:exact: xiazeminroute:- destination:host: httpd-svc- route:- destination:host: tomcat-svc
上面的例子,就是通过header来做流量的分配。
% kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080wget: server returned error: HTTP/1.1 404 Not Foundcommand terminated with exit code 1% kubectl -n vc exec -it client-78b5c977-xb27z -- wget -q -O - http://web-svc:8080 --header 'end-user:xiazemin'hello httpd


文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




