經過K8S Ingress Controller來實現應用的流量複製

摘要: 本文主要跟你們分享下如何在阿里雲容器服務不一樣K8S集羣內進行應用的流量複製

仿真測試

一般在系統進行重大重構或者發佈新功能時,咱們都須要進行壓測以事先評估新系統的承載能力,傳統狀況下咱們通常會在線下環境來模擬線上各類測試數據來對新系統進行測試,可是這種方式每每並不能有效地模擬出線上真實的訪問量,尤爲是模擬出正常流量中混雜着各色的異常流量,針對這種狀況咱們能夠經過將線上的應用流量複製到指定的線下環境來對新系統進行仿真測試;另一種狀況倘若咱們線上系統遇到了性能瓶頸,可是又不能快速地定位出問題點,咱們一樣能夠採用流量複製的方式來將應用的真實流量引導到線下環境來進行定位。下面主要跟你們分享下如何在阿里雲容器服務不一樣K8S集羣內進行應用的流量複製:
nginx

部署基礎應用

這裏假設您已經在阿里雲容器服務控制檯申請了兩個不一樣的K8S集羣(暫定一個爲K8S Product Cluster,一個爲K8S Stage Cluster)。git

1、在K8S Product Cluster中部署一個應用,並經過Ingress來對外暴露服務訪問:github

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      # 當前配置爲老版本鏡像
      - image: registry.cn-hangzhou.aliyuncs.com/xianlu/old-nginx
        imagePullPolicy: Always
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
  # 這裏配置使用了集羣默認域名(您也可使用自定義域名並做DNS解析)
  - host: nginx.c37bf6b77bded43669ba2fb67448b4146.cn-hangzhou.alicontainer.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80

2、部署完成後可經過以下命令測試訪問:api

# 查看該應用的ingress配置
  kubectl get ing nginx-ingress
NAME            HOSTS                                                                  ADDRESS         PORTS     AGE
nginx-ingress   nginx.c37bf6b77bded43669ba2fb67448b4146.cn-hangzhou.alicontainer.com   47.110.199.44   80        8m

  # 測試訪問應用域名
  curl http://nginx.c37bf6b77bded43669ba2fb67448b4146.cn-hangzhou.alicontainer.com
old

3、在K8S Stage Cluster中部署一個相同應用,一樣經過Ingress來對外暴露服務訪問:服務器

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      # 當前配置爲新版本鏡像
      - image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginx
        imagePullPolicy: Always
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
  # 這裏配置使用了集羣默認域名(您也可使用自定義域名並做DNS解析)
  - host: nginx.c41eb6ca34a3e49f7aea63b8bc9e8ad98.cn-beijing.alicontainer.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80

4、部署完成後可經過以下命令測試訪問:app

# 查看該應用的ingress配置
 kubectl get ing nginx-ingress
NAME            HOSTS                                                                 ADDRESS        PORTS     AGE
nginx-ingress   nginx.c41eb6ca34a3e49f7aea63b8bc9e8ad98.cn-beijing.alicontainer.com   39.106.233.1   80        1m

 # 測試訪問應用域名
 curl http://nginx.c41eb6ca34a3e49f7aea63b8bc9e8ad98.cn-beijing.alicontainer.com
new

配置流量複製

這裏假設咱們但願將K8S Product Cluster中該應用 100% 的訪問流量複製到K8S Stage Cluster中對應的應用服務上,即將全部訪問域名nginx.c37bf6b77bded43669ba2fb67448b4146.cn-hangzhou.alicontainer.com的請求複製轉發一份到nginx.c41eb6ca34a3e49f7aea63b8bc9e8ad98.cn-beijing.alicontainer.com上:
curl

說明:因爲K8S Stage Cluster Ingress僅僅只做爲複製流量的接收方,所以它不須要作任何配置修改,這裏咱們只須要在K8S Product Cluster Ingress上進行配置便可。ide

1、配置複製流量百分比和接收復制流量的應用域名性能

經過以下命令來修改K8S Ingress Controller的ConfigMap配置增長http-snippet配置片斷,配置指望的複製流量百分比和接收復制流量的目標應用域名:測試

~ kubectl -n kube-system edit cm nginx-configuration

配置以下KV對:

http-snippet: |
   split_clients "$date_gmt" $mirror_servers {
      100%    nginx.c41eb6ca34a3e49f7aea63b8bc9e8ad98.cn-beijing.alicontainer.com;
   }

# 配置說明:
# 1\. 流量百分比取值範圍:(0, 100],百分比總和必須不大於100%
# 2\. 支持同時配置多個不一樣的複製流量接收目標應用

2、配置流量複製

這裏咱們經過configuration-snippetserver-snippet來修改源Ingress增長應用的流量複製配置(YAML參考以下):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
        mirror /mirror;
    nginx.ingress.kubernetes.io/server-snippet: |
        location = /mirror {
            internal;
            set $shadow_service_name    "nginx-product-service";
            proxy_set_header X-Shadow-Service  $shadow_service_name;
            proxy_pass http://$mirror_servers$request_uri;
        }
spec:
  rules:
  - host: nginx.c37bf6b77bded43669ba2fb67448b4146.cn-hangzhou.alicontainer.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80

至此,基於K8S Ingress Controller的應用流量複製配置已經完成。

測試流量複製

這裏咱們嘗試訪問K8S Product Cluster中的應用域名nginx.c37bf6b77bded43669ba2fb67448b4146.cn-hangzhou.alicontainer.com,能夠看到每次請求K8S Product Cluster中的Ingress應用域名時,其都將複製一份一樣的請求轉發到K8S Stage Cluster中對應的應用服務中:

同時,因爲前面咱們在流量複製配置中配置了proxy_set_header X-Shadow-Service $shadow_service_name,所以在K8S Stage Cluster目標應用接收的請求中,都會攜帶一個X-Shadow-Service頭來指明該鏡像流量來源於哪一個線上應用。

雲服務器99元拼團購!拉新還可贏現金紅包!300萬等你瓜分!
立刻一鍵開團贏紅包: http://click.aliyun.com/m/100...



本文做者:chenqz

閱讀原文

本文爲雲棲社區原創內容,未經容許不得轉載。

相關文章
相關標籤/搜索