---
# System prepended metadata

title: Docker Shadowsock LoadBalance
tags: [Docker]

---

# Docker Shadowsock Load Balance
## docker 指令
### Step.1 下載 Shadowsocks 鏡像
``` docker
docker pull appso/shadowsocks-libev
```
建立Shadowsocks Container
```
docker run -d -p 8388:8388 -e PASSWORD=rm6666 -e METHOD=aes-256-gcm --name=ss-server3 appso/shadowsocks-libev

docker run -d -p 8488:8388 -e PASSWORD=rm6666 -e METHOD=aes-256-gcm --name=ss-server3 appso/shadowsocks-libev

docker run -d -p 8588:8388 -e PASSWORD=rm6666 -e METHOD=aes-256-gcm --name=ss-server3 appso/shadowsocks-libev
```

### Step.2 下載 HAProxy 鏡像
```
docker pull haproxy
```
設定 HAProxy.cfg 配置，請確保最後結尾是換行符號。
```
global
    maxconn 1000

defaults
    mode tcp
    option dontlognull
    timeout connect 5000
    timeout client 50000
    timeout server 50000

frontend ss_frontend
    bind *:8080
    mode tcp
    default_backend ss_backend

backend ss_backend
    mode tcp
    balance roundrobin
    server server1 172.17.0.2:8388 check
    server server2 172.17.0.3:8388 check
    server server3 172.17.0.4:8388 check


```
建立 HAProxy Container
```
docker run -d -p 8080:8080 -v C:/work/shadowsocks/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --name my-haproxy haproxy:latest
```
![螢幕擷取畫面 2023-11-01 170043.png](https://hackmd.io/_uploads/SJMAQ5kQp.png)

## docker-compose
先建立network，docker-compose會使用bridge因此每次建置ip會不同，所以要先建置network。
```
docker network create --subnet 172.50.0.0/16 mynetwork
```
配置HAProxy.cfg
```
global
    maxconn 1000

defaults
    mode tcp
    option dontlognull
    timeout connect 5000
    timeout client 50000
    timeout server 50000

frontend ss_frontend
    bind *:8080
    mode tcp
    default_backend ss_backend

backend ss_backend
    mode tcp
    balance roundrobin
    server server1 172.50.0.2:8388 check
    server server2 172.50.0.3:8488 check
    server server3 172.50.0.4:8588 check
    server server4 172.50.0.5:8688 check
    server server5 172.50.0.6:8788 check

```
配置 Shadowsocks config.json
```
{
    "server_port":8388,
    "password":"rm8888",
    "timeout":600,
    "method":"aes-256-gcm",
    "fast_open":false,
    "mode":"tcp"
}
```
開始建立docker-compose，這邊使用五個shadowsocks做
```
version: '3'
services:
  shadowsocks1:
    image: appso/shadowsocks-libev
    container_name: ss-server1
    ports:
      - "8388:8388"  
    volumes:
      - ./config1.json:/etc/shadowsocks-libev/config.json
    restart: always 
    networks:
      mynetwork:
        ipv4_address: 172.50.0.2
      
  shadowsocks2:
    image: appso/shadowsocks-libev
    container_name: ss-server2
    ports:
      - "8488:8388"  
    volumes:
      - ./config2.json:/etc/shadowsocks-libev/config.json
    restart: always
    networks:
      mynetwork:
        ipv4_address: 172.50.0.3

  shadowsocks3:
    image: appso/shadowsocks-libev
    container_name: ss-server3
    ports:
      - "8588:8388" 
    volumes:
      - ./config3.json:/etc/shadowsocks-libev/config.json
    restart: always  
    networks:
      mynetwork:
        ipv4_address: 172.50.0.4

  haproxy:
    image: haproxy:latest
    container_name: my-haproxy
    ports:
      - "8080:8080"
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
    restart: always
    networks:
      mynetwork:
        ipv4_address: 172.50.0.7

networks:
  mynetwork:
    external:
      name: mynetwork
```
最後執行docker-compose命令，請先移至docker-compose資料夾。
```
docker-compose up -d 
```