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规则进行分流
[root@k8s-master-1 access-weight]---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
[root@k8s-master-1 access-weight]NAME 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[root@k8s-master-1 access-weight]NAME 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[root@k8s-master-1 access-weight]httpd-2[root@k8s-master-1 access-weight]httpd-1[root@k8s-master-1 access-weight]httpd-2[root@k8s-master-1 access-weight]httpd-1[root@k8s-master-1 access-weight]httpd-2
[root@k8s-master-1 access-weight]namespace/web unchangeddeployment.apps/httpd-1 configureddeployment.apps/httpd-2 configuredservice/httpd-1 unchangedservice/httpd-2 unchangedservice/httpd unchanged[root@k8s-master-1 access-weight]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[root@k8s-master-1 access-weight]virtualservice.networking.istio.io/httpd created[root@k8s-master-1 access-weight]NAME GATEWAYS HOSTS AGEhttpd["httpd.web.svc.cluster.local"] 30s[root@k8s-master-1 istio]NAME 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[root@k8s-master-1 access-weight]httpd-2[root@k8s-master-1 access-weight]httpd-1[root@k8s-master-1 access-weight]httpd-2[root@k8s-master-1 access-weight]httpd-1[root@k8s-master-1 access-weight]httpd-2[root@k8s-master-1 access-weight]httpd-1[root@k8s-master-1 access-weight]If you don't see a command prompt, try pressing enter./ httpd-2/ httpd-1/ httpd-1/ httpd-1/ httpd-1/ httpd-2/ httpd-1/ httpd-1/ httpd-1/ httpd-1/ httpd-1/ httpd-2/ httpd-2/