> 文档中心 > Istio-virtualservice之基于权重分配流量(1)

Istio-virtualservice之基于权重分配流量(1)


Virtual Service实例

基于权重分配流量

在这里插入图片描述

  • 在未使用Istio时,k8s在上述默认情况下,如果要访问httpd-1,就需要访问httpd-1的service,如果需要httpd-1和httpd-2都要访问的话,就需要创建一个service,将httpd-1和httpd-2都selector,但是在这种情况下用户在访问的时候是轮询的(即httpd-1和httpd-2轮流访问,没法去控制其访问流量权重)
  • 在istio之间进行访问时,流量走向是基于istio配置的,如果其中某一方不在istio网格内,那么流量就不会按照istio规则进行分流
# 测试yaml[root@k8s-master-1 access-weight]# cat deployment.yaml ---apiVersion: v1kind: Namespacemetadata:  name: web---apiVersion: apps/v1kind: Deploymentmetadata:  name: httpd-1  namespace: webspec:  selector:    matchLabels:      app: httpd-1      server: web  template:    metadata:      labels: app: httpd-1 server: web    spec:      containers:      - name: busybox image: busybox:1.28 imagePullPolicy: IfNotPresent command: ["/bin/sh","-c","echo 'httpd-1' >> /var/www/index.html; httpd -f -p 80 -h /var/www"]---apiVersion: apps/v1kind: Deploymentmetadata:  name: httpd-2  namespace: webspec:  selector:    matchLabels:      app: httpd-2      server: web  template:    metadata:      labels: app: httpd-2 server: web    spec:      containers:      - name: busybox image: busybox:1.28 imagePullPolicy: IfNotPresent command: ["/bin/sh","-c","echo 'httpd-2' >> /var/www/index.html; httpd -f -p 80 -h /var/www"]---apiVersion: v1kind: Servicemetadata:  name: httpd-1  namespace: webspec:  selector:    app: httpd-1  ports:  - name: http    port: 80    targetPort: 80---apiVersion: v1kind: Servicemetadata:  name: httpd-2  namespace: webspec:  selector:    app: httpd-2  ports:  - name: http    port: 80    targetPort: 80---apiVersion: v1kind: Servicemetadata:  name: httpd  namespace: webspec:  selector:    server: web  ports:  - name: http    port: 80    targetPort: 80
# 查看endpoints[root@k8s-master-1 access-weight]# kubectl get endpoints -n webNAME      ENDPOINTSAGEhttpd     10.70.2.38:80,10.70.2.42:80   15shttpd-1   10.70.2.42:80   15shttpd-2   10.70.2.38:80   15s# 查看svc[root@k8s-master-1 access-weight]# kubectl get svc -n webNAME      TYPE CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGEhttpd     ClusterIP   10.0.21.70    <none> 80/TCP    93shttpd-1   ClusterIP   10.0.42.178   <none> 80/TCP    93shttpd-2   ClusterIP   10.0.18.166   <none> 80/TCP    93s# 对httpd进行访问,可以发现其流量是轮询的[root@k8s-master-1 access-weight]# curl 10.0.21.70httpd-2[root@k8s-master-1 access-weight]# curl 10.0.21.70httpd-1[root@k8s-master-1 access-weight]# curl 10.0.21.70httpd-2[root@k8s-master-1 access-weight]# curl 10.0.21.70httpd-1[root@k8s-master-1 access-weight]# curl 10.0.21.70httpd-2

在这里插入图片描述

# 手工注入一下[root@k8s-master-1 access-weight]# istioctl kube-inject -f deployment.yaml | kubectl apply -f -namespace/web unchangeddeployment.apps/httpd-1 configureddeployment.apps/httpd-2 configuredservice/httpd-1 unchangedservice/httpd-2 unchangedservice/httpd unchanged# 查看virtualservice[root@k8s-master-1 access-weight]# cat virtualservice.yaml apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:  name: httpdspec:  hosts:  - httpd.web.svc.cluster.local  http:  - route:    - destination:  host: httpd-1.web.svc.cluster.local      weight: 70    - destination: host: httpd-2.web.svc.cluster.local      weight: 30# 部署virtualservice[root@k8s-master-1 access-weight]# kubectl apply -f virtualservice.yaml virtualservice.networking.istio.io/httpd created# 查看virtualservice[root@k8s-master-1 access-weight]# kubectl get virtualserviceNAME    GATEWAYS   HOSTS AGEhttpd["httpd.web.svc.cluster.local"]   30s# 查看service[root@k8s-master-1 istio]# kubectl get svc -n webNAME      TYPE CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGEhttpd     ClusterIP   10.0.82.52     <none> 80/TCP    2m48shttpd-1   ClusterIP   10.0.157.208   <none> 80/TCP    2m48shttpd-2   ClusterIP   10.0.179.208   <none> 80/TCP    2m48s# 在istio网格之外进行访问测试,可以发现并没有实现基于权重访问[root@k8s-master-1 access-weight]# curl 10.0.82.52httpd-2[root@k8s-master-1 access-weight]# curl 10.0.82.52httpd-1[root@k8s-master-1 access-weight]# curl 10.0.82.52httpd-2[root@k8s-master-1 access-weight]# curl 10.0.82.52httpd-1[root@k8s-master-1 access-weight]# curl 10.0.82.52httpd-2[root@k8s-master-1 access-weight]# curl 10.0.82.52httpd-1# 在istio网格内进行访问测试,可以发现访问结果并不是之前的轮询了[root@k8s-master-1 access-weight]# kubectl run -it --rm busybox --image=busybox:1.28 -- /bin/shIf you don't see a command prompt, try pressing enter./ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-2/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-2/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-1/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-2/ # wget -q -O - http://httpd.web.svc.cluster.localhttpd-2/ #