1. 系统环境:
底层系统:CentOS Linux release 8.1.1911 (Core
负载均衡:192.168.0.115: 9117
web1: 192.168.0.115:9113
web2: 192.168.0.115:9111
注意:关闭selinux, 防火墙
2. docker环境部署
2.1 安装docker-ce
dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
dnf -y install docker-ce
2.2 安装nginx
docker run --name nginxlb -p 9117:80 -d nginx
docker run --name nginx01 -p 9113:80 -d nginx
docker run --name nginx02 -p 9111:80 -d nginx
3. 配置nginx负载均衡
3.1 配置负载均衡容器, 在/etc/nginx/nginx.conf的http中插入以下内容
upstream test01 {
server 192.168.0.115:9111 weight=2;
server 192.168.0.115:9113 weight=7;
}
server {
listen 9117;
server_name 192.168.0.115;
root /usr/share/nginx/html;
index index.html;
location / {
proxy_pass http://test01;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
}
}
3.2 修改nginx01,nginx02容器里nginx的欢迎页面,测试是否实现了负载均衡
index.html的路径在: /usr/share/nginx/html/index.html
把在外面改好的内容传人到容器内
docker cp index.html nginx02:/usr/share/nginx/html
至此,一个简单的负载均衡已经实现