阿里雲容器服務TCP的負載均衡配置

阿里雲容器服務在使用的過程中,針對TCP負載均衡的場景,會遇到這樣的問題:如果一個應用的客戶端鏡像和服務端鏡像均部署在同一個節點(ECS)上面,由於受SLB的限制,該應用的客戶端不能通過SLB訪問本機的服務端。本文試圖以常用的基於TCP協議的redis為例,逐步深入的方式來解決這個問題,同時帶大家了解一下容器服務的概念。

解法一:通過調度容器,避免客戶端和服務端容器部署在同一個節點

示例應用模板(使用了lb標籤,和swarm fileter功能)

redis-master: ports:

- 6379:6379/tcp image: 'redis:alpine'

labels:

aliyun.lb.port_6379: tcp://proxy_test:6379redis-client: image: 'redis:alpine'

Advertisements

links:

- redis-master environment:

- 'affinity:aliyun.lb.port_6379!=tcp://proxy_test:6379'

command: redis-cli -h 120.25.131.64

stdin_open: true tty: true

注意事項:

  • 如果發現調度不生效,進入服務列表,選擇你需要調度的服務,選擇重新調度,選擇強制重新調度

  • 強制重新調度會丟棄已有容器的volume,請做好相應的備份遷移工作

解法二:容器集群內部客戶端使用link訪問服務端,集群外部使用SLB

示例應用模板(使用了lb標籤)

redis-master: ports:

Advertisements

- 6379:6379/tcp image: 'redis:alpine'

labels:

aliyun.lb.port_6379: tcp://proxy_test:6379redis-client: image: 'redis:alpine'

links:

- redis-master command: redis-cli -h redis-master stdin_open: true tty: true

解法三:容器集群內部客戶端使用自定義路由(基於HAProxy)作為代理訪問服務端,集群外部使用SLB

示例應用模板(使用了lb標籤,自定義路由鏡像)

lb: image: registry.aliyuncs.com/acs/proxy:0.5

ports:

- '6379:6379/tcp'

restart: always labels:

# addon 使得proxy鏡像有訂閱註冊中心的能力,動態載入服務的路由

aliyun.custom_addon: "proxy"

# 每台vm 部署一個該鏡像的容器

aliyun.global: "true"

# 前端綁定SLB,使用lb標籤

aliyun.lb.port_6379: tcp://proxy_test:6379

# 告訴系統,自定義路由需要等待master和slave啟動之後再啟動,並且對master和slave有依賴

aliyun.depends: redis-master,redis-slave environment:

# 支持載入路由的後端容器的範圍,"*"表示整個集群,默認為應用內的服務 ADDITIONAL_SERVICES: "*"

EXTRA_DEFAULT_SETTINGS: "log rsyslog local0,log global,option httplog"

# 配置HAProxy工作於tcp模式 MODE: "tcp"

links:

- rsyslog:rsyslogrsyslog: image: registry.cn-hangzhou.aliyuncs.com/linhuatest/rsyslog:latestredis-master: ports:

- 6379/tcp image: 'redis:alpine'

labels:

# 告訴自定義路由需要暴露6379埠

aliyun.proxy.TCP_PORTS: "6379"

# 告訴系統,該服務的路由需要添加到自定義路由服務中

aliyun.proxy.required: "true"redis-slave: ports:

- 6379/tcp image: 'redis:alpine'

links:

- redis-master labels:

# 告訴自定義路由需要暴露6379埠

aliyun.proxy.TCP_PORTS: "6379"

# 告訴系統,該服務的路由需要添加到自定義路由服務中

aliyun.proxy.required: "true"

# 告訴系統,slave需要等待master啟動之後再啟動,並且對master有依賴

aliyun.depends: redis-master command: redis-server --slaveof redis-master 6379redis-client: image: 'redis:alpine'

links:

- lb:www.example.com labels:

aliyun.depends: lb command: redis-cli -h www.example.com stdin_open: true tty: true

該解決方案,做到了redis的主從架構,同時經過自定義路由鏡像做負載均衡,做到了一定程度的高可用。

Advertisements

你可能會喜歡