k8s之Istio实现virtualservice基于权重分配流量(1)
Istio-virtualservice之基于权重分配流量小案例
·
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: 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
# 查看endpoints
[root@k8s-master-1 access-weight]# kubectl get endpoints -n web
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
# 查看svc
[root@k8s-master-1 access-weight]# kubectl get svc -n web
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
# 对httpd进行访问,可以发现其流量是轮询的
[root@k8s-master-1 access-weight]# curl 10.0.21.70
httpd-2
[root@k8s-master-1 access-weight]# curl 10.0.21.70
httpd-1
[root@k8s-master-1 access-weight]# curl 10.0.21.70
httpd-2
[root@k8s-master-1 access-weight]# curl 10.0.21.70
httpd-1
[root@k8s-master-1 access-weight]# curl 10.0.21.70
httpd-2
# 手工注入一下
[root@k8s-master-1 access-weight]# istioctl kube-inject -f deployment.yaml | kubectl apply -f -
namespace/web unchanged
deployment.apps/httpd-1 configured
deployment.apps/httpd-2 configured
service/httpd-1 unchanged
service/httpd-2 unchanged
service/httpd unchanged
# 查看virtualservice
[root@k8s-master-1 access-weight]# cat virtualservice.yaml
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
# 部署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 virtualservice
NAME GATEWAYS HOSTS AGE
httpd ["httpd.web.svc.cluster.local"] 30s
# 查看service
[root@k8s-master-1 istio]# kubectl get svc -n web
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
# 在istio网格之外进行访问测试,可以发现并没有实现基于权重访问
[root@k8s-master-1 access-weight]# curl 10.0.82.52
httpd-2
[root@k8s-master-1 access-weight]# curl 10.0.82.52
httpd-1
[root@k8s-master-1 access-weight]# curl 10.0.82.52
httpd-2
[root@k8s-master-1 access-weight]# curl 10.0.82.52
httpd-1
[root@k8s-master-1 access-weight]# curl 10.0.82.52
httpd-2
[root@k8s-master-1 access-weight]# curl 10.0.82.52
httpd-1
# 在istio网格内进行访问测试,可以发现访问结果并不是之前的轮询了
[root@k8s-master-1 access-weight]# kubectl run -it --rm busybox --image=busybox:1.28 -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-2
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-2
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-1
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-2
/ # wget -q -O - http://httpd.web.svc.cluster.local
httpd-2
/ #
更多推荐
已为社区贡献43条内容
所有评论(0)