linux12企业实战 -- 10流量策略

Posted FikL-09-19

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux12企业实战 -- 10流量策略相关的知识,希望对你有一定的参考价值。

一、DestinationRule(目标规则)

DestinationRule定义了在发生路由后应用于服务流量的策略。这些规则指定负载均衡的配置,sidecar的连接池大小以及离线集群检测设置,通过健康检查,可以移除负载均衡池中的不健康的主机。

二、改变流量策略

1、创建两个服务

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-web
  labels:
    app: nginx
    deploy: nginx
    server: web
spec:
  selector:
    matchLabels:
      app: nginx
      deploy: nginx
      server: web
  template:
    metadata:
      labels:
        app: nginx
        deploy: nginx
        server: web
    spec:
      containers:
        - name: nginx
          image: nginx
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-web-svc
  labels:
    app: nginx-web-svc
    deploy: nginx-web-svc
spec:
  ports:
    - port: 80
      targetPort: 80
      name: nginx
      protocol: TCP
  selector:
    server: web
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: httpd-web
  labels:
    app: httpd
    deploy: httpd
    server: web
spec:
  selector:
    matchLabels:
      app: httpd
      deploy: httpd
      server: web
  template:
    metadata:
      labels:
        app: httpd
        deploy: httpd
        server: web
    spec:
      containers:
        - name: httpd
          imagePullPolicy: IfNotPresent
          image: httpd
          ports:
            - containerPort: 80

2、创建Gateway

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: web-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: httpd
        protocol: HTTP
      hosts:
        - "svc.test.com"
    - port:
        number: 80
        name: nginx
        protocol: HTTP
      hosts:
        - "svc.test.com"

3、创建DestinationRule

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: web
spec:
  host: nginx-web-svc
  subsets:
    - name: httpd
      labels:
        app: httpd
    - name: nginx
      labels:
        app: nginx

4、创建VirtualService

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web
spec:
  hosts:
    - "svc.test.com"
  gateways:
    - web-gateway
  http:
    - route:
        - destination:
            host: nginx-web-svc
            subset: nginx
          weight: 80
        - destination:
            host: nginx-web-svc
            subset: httpd
          weight: 20

5、测试

# 通过下方测试,我们可以看到同一个服务,明显改变了流量轮询规则
[root@kubernetes-master-01 ~]# kubectl exec -it test -- sh
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
/ # while true; do wget -q -O- http://svc.test.com:3
2623;sleep 1;done
2623;sleep 1;done
<h1>It is Nginx!</h1>
<h1>It is Nginx!</h1>
<h1>It is Nginx!</h1>
<h1>It is Nginx!</h1>
<h1>It is Nginx!</h1>
<h1>It is Nginx!</h1>
<h1>It is Nginx!</h1>
<html><body><h1>It works!</h1></body></html>
<h1>It is Nginx!</h1>
<html><body><h1>It works!</h1></body></html>

以上是关于linux12企业实战 -- 10流量策略的主要内容,如果未能解决你的问题,请参考以下文章

linux12企业实战 -- 21nginx非200错误

linux12企业实战 -- 25 KFC appcommon 服务器扩容

交通流量预测,EasyDL带你零代码实战

linux12企业实战 -- 07ssh免密

linux12企业实战 -- 23 Bad request 40000接口报错

我的Linux,我做主!企业军工级安全策略--SELinux实战