Docker模擬Nginx負(fù)載均衡
- 直接上dockerfile
- 目錄結(jié)構(gòu):
├── build
│ ├── lb1
│ │ └── Dockerfile
│ ├── lb2
│ │ └── Dockerfile
│ └── slb
│ ├── Dockerfile
│ └── default.conf
└── docker-compose.yml
- vim build/lb1/Dockerfile
FROM daocloud.io/library/nginx:1.13.0-alpine
RUN echo "I am web1" > /usr/share/nginx/html/index.html
- vim build/lb2/Dockerfile
FROM daocloud.io/library/nginx:1.13.0-alpine
RUN echo "I am web2" > /usr/share/nginx/html/index.html
- vim build/slb/Dockerfile
FROM daocloud.io/library/nginx:1.13.0-alpine
RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
COPY default.conf /etc/nginx/conf.d/default.conf
- vim build/slb/default.conf
upstream allserver {
server lb1;
server lb2;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://allserver;
}
}
- vim docker-compose.yml
version: '2.3'
services:
slb:
build:
context: ./build/slb
privileged: true
ports:
- "8080:80"
links:
- "lb1"
lb1:
build:
context: ./build/lb1
privileged: true
ports:
- "8081:80"
lb2:
build:
context: ./build/lb2
privileged: true
ports:
- "8082:80"
啟動(dòng)服務(wù)并在后臺(tái)運(yùn)行, 在 docker-compose.yml 所在目錄執(zhí)行:
docker-compose up -d
查看容器是否啟動(dòng)運(yùn)行
docker ps -a
測(cè)試, 執(zhí)行多次觀察輸出結(jié)果
curl localhost:8080
輸出結(jié)果:
I am web1
I am web2