nginx负载均衡

#1.拉取nginx镜像

sudo docker pull 
registry.cn-hangzhou.aliyuncs.com/leokim_php_env/leokim_nginx

#2.查看nginx容器是否正常运行

docker run -dit -p 8000:80 --name load_balance_nginx e548f1a579cf

#3.复制nginx配置文件和网页文件到宿主机 方便修改配置文件

docker cp nginx_main:/etc/nginx/conf.d/default.conf ./ 
docker cp nginx_main:/usr/share/nginx/html/index.html ./

修改

#4.修改index.html内容 不同容器展示内容分别改成1、2、3

<html><h1>This is nginx service 3</h1></html>

#5.关闭nginx_main 删除所有已关闭容器,为了名称不会重复

docker stop load_balance_nginx
docker rm $(docker ps -aq)

#6.运行负载均衡服务器 load_balance_nginx

docker run --name load_balance_nginx -dit -p 8000:80 \
-v /Users/leokim/Documents/docker_config/default_lb.conf:/etc/nginx/conf.d/default.conf \
-v /Users/leokim/Documents/docker_config/index.html:/usr/share/nginx/html/index.html \
e548f1a579cf

#7.运行集群服务器1

docker run --name nginx_1 -dit -p 9000:80 \
-v /Users/leokim/Documents/docker_config/default_cs.conf:/etc/nginx/conf.d/default.conf \
-v /Users/leokim/Documents/docker_config/index_1.html:/usr/share/nginx/html/index.html \
e548f1a579cf

#8.运行集群服务器2

docker run --name nginx_2 -dit -p 9001:80 \
-v /Users/leokim/Documents/docker_config/default_cs.conf:/etc/nginx/conf.d/default.conf \
-v /Users/leokim/Documents/docker_config/index_2.html:/usr/share/nginx/html/index.html \
e548f1a579cf


#9.运行集群服务器3

docker run --name nginx_3 -dit -p 9003:80 \
-v /Users/leokim/Documents/docker_config/default_cs.conf:/etc/nginx/conf.d/default.conf \
-v /Users/leokim/Documents/docker_config/index_3.html:/usr/share/nginx/html/index.html \
e548f1a579cf


#10. 访问http://localhost:8000/ 查看负载均衡效果


#11. 配置weight并查看配置权重后负载均衡效果

docker stop load_balance_nginx
docker rm $(docker ps -aq)
docker run --name load_balance_nginx -dit -p 8000:80 \
-v /Users/leokim/Documents/docker_config/default_lb.conf:/etc/nginx/conf.d/default.conf \
-v /Users/leokim/Documents/docker_config/index.html:/usr/share/nginx/html/index.html \
e548f1a579cf