利用Docker容器技术实现如下负载均衡
# 搜索nginx镜像
docker search nginx
# 拉取nginx镜像
docker pull nginx
# 检查镜像是否拉取成功
docker images
# 运行一个测试的nginx
docker run -d --name nginxtest nginx
# 把容器里的nginx目录复制出来,配置是放在/etc/nginx
docker cp nginxtest:/etc/nginx ./
# 把容器里的log目录复制出来,日志是放在/var/log
docker cp nginxtest:/var/log ./
# 删除测试的nginx-test
docker rm -f nginxtest
# 查看nginx配置结构
[root@localhost ~]# tree nginx
nginx
├── conf.d
│ └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params
# 查看log日志结构
[root@localhost ~]# tree log
log
├── apt
│ ├── eipp.log.xz
│ ├── history.log
│ └── term.log
├── btmp
├── dpkg.log
├── faillog
├── lastlog
├── nginx
│ ├── access.log -> /dev/stdout
│ └── error.log -> /dev/stderr
└── wtmp
新建/data目录
# Nginx负载均衡容器
docker run -d --name nginx1 -p 80:80 \
-v /data/nginx1/nginx:/etc/nginx \
-v /data/nginx1/log:/var/log nginx;
# WEB服务器1
docker run -d --name nginx2 -p 8080:80 \
-v /data/nginx2/nginx:/etc/nginx \
-v /data/nginx2/log:/var/log \
-v /data/nginx2/html:/usr/share/nginx/html nginx;
# WEB服务器2
docker run -d --name nginx3 -p 8081:80 \
-v /data/nginx3/nginx:/etc/nginx \
-v /data/nginx3/log:/var/log \
-v /data/nginx3/html:/usr/share/nginx/html nginx;
# Nginx负载均衡容器default.conf配置改成如下
upstream services {
server 192.168.123.199:8080;
server 192.168.123.199:8081;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://services;
}
}
# WEB服务器1、WEB服务器2的default.conf配置改成如下
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index index.html index.php;
}
}
# 重启nginx1、nginx2、nginx3容器
docker restart nginx1 nginx2 nginx3