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: v1
kind: Namespace
metadata:
name: web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-1
namespace: web
spec:
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/v1
kind: Deployment
metadata:
name: httpd-2
namespace: web
spec:
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: v1
kind: Service
metadata:
name: httpd-1
namespace: web
spec:
selector:
app: httpd-1
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: httpd-2
namespace: web
spec:
selector:
app: httpd-2
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: httpd
namespace: web
spec:
selector:
server: web
ports:
- name: http
port: 80
targetPort: 80
[root@k8s-master-1 access-weight]
NAME ENDPOINTS AGE
httpd 10.70.2.38:80,10.70.2.42:80 15s
httpd-1 10.70.2.42:80 15s
httpd-2 10.70.2.38:80 15s
[root@k8s-master-1 access-weight]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd ClusterIP 10.0.21.70 <none> 80/TCP 93s
httpd-1 ClusterIP 10.0.42.178 <none> 80/TCP 93s
httpd-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 unchanged
deployment.apps/httpd-1 configured
deployment.apps/httpd-2 configured
service/httpd-1 unchanged
service/httpd-2 unchanged
service/httpd unchanged
[root@k8s-master-1 access-weight]
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: httpd
spec:
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 AGE
httpd ["httpd.web.svc.cluster.local"] 30s
[root@k8s-master-1 istio]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd ClusterIP 10.0.82.52 <none> 80/TCP 2m48s
httpd-1 ClusterIP 10.0.157.208 <none> 80/TCP 2m48s
httpd-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
/