以Docker的網絡管理,容器的IP設置爲基礎知識實現Nginx負載均衡
查看全部docker網絡html
docker network ls /* NETWORK ID NAME DRIVER SCOPE b832b168ca9a bridge bridge local 373be82d3a6a composetest_default bridge local a360425082c4 host host local 154f600f0e90 none null local */ // composetest_default 是上一篇介紹Compose時,docker-compose.yml文件所在的目錄名, // 因此,用docker-compose建立的容器會默認建立一個以目錄名爲網絡名的網絡,而且是dridge(橋接)類型
指定容器IP地址nginx
官網文檔地址:https://docs.docker.com/compo...web
繼續編寫上一篇《12.使用Docker Compose容器編排工具》文章中的docker-compose.ymldocker
version: "3" services: web1: container_name: web1 image: "centos:httpd" ports: - "8080:80" privileged: true volumes: - "/app/www/web1/:/var/www/html/" command: ['/usr/sbin/init'] networks: nginx-lsb: ipv4_address: 192.169.0.3 web2: container_name: web2 image: "centos:httpd" ports: - "8081:80" privileged: true volumes: - "/app/www/web2/:/var/www/html/" command: ['/usr/sbin/init'] networks: nginx-lsb: ipv4_address: 192.169.0.2 networks: nginx-lsb: driver: bridge ipam: config: - subnet: 192.169.0.0/16
使用docker-compose啓動容器centos
docker-compose up -d
查看容器是否啓動,並確認是否建立了網絡 nginx-lsb服務器
// 能夠查看當前docker-compose.yml配置的容器組裏的容器狀態 docker-compose ps docker network ls /* NETWORK ID NAME DRIVER SCOPE b832b168ca9a bridge bridge local 373be82d3a6a composetest_default bridge local de6f5b8df1c8 composetest_nginx-lsb bridge local a360425082c4 host host local 154f600f0e90 none null local */ // 建立了nginx-lsb網絡,命名是容器組項目的 文件名開頭_網絡名
查看網絡 nginx-lsb的詳情網絡
docker network inspect composetest_nginx-lsb // 詳情裏面能夠看到使用這個網絡的每一個容器的ip 如: /* ... "Containers": { "039aa860ef04f20a7566fdc943fb4398a61d2ad6dd3e373b17c86ac778af89e3": { "Name": "web2", "EndpointID": "1bc206661179e65999015f132c2889d3d4365b8d42b8a89cf9c260016fedd5ee", "MacAddress": "02:42:c0:a9:00:02", "IPv4Address": "192.169.0.2/16", "IPv6Address": "" }, "437ad7a07da8e46c0abaf845c4b08425338009fbe972bde69478cf47c75c315b": { "Name": "web1", "EndpointID": "5a36e602a5364ee9ad06e9816d09e3966d56ebf06b9394ebc25b8bcee9546607", "MacAddress": "02:42:c0:a9:00:03", "IPv4Address": "192.169.0.3/16", "IPv6Address": "" } }, ... */
使用 env_file環境文件:app
簡單能夠理解爲:在docker-compose.yml中定義變量,引用在外部.env文件中進行變量定義composer
官方文檔地址:https://docs.docker.com/compo...負載均衡
// 仍是在composetest目錄中定義個 .env文件,用來存放變量 web1_addr=192.169.0.2 web2_addr=192.169.0.3 // 修改docker-compose.yml文件,加入變量定義 version: "3" services: web1: container_name: web1 image: "centos:httpd" ports: - "8080:80" privileged: true volumes: - "/app/www/web1/:/var/www/html/" command: ['/usr/sbin/init'] networks: nginx-lsb: ipv4_address: ${web1_addr} web2: container_name: web2 image: "centos:httpd" ports: - "8081:80" privileged: true volumes: - "/app/www/web2/:/var/www/html/" command: ['/usr/sbin/init'] networks: nginx-lsb: ipv4_address: ${web2_addr} networks: nginx-lsb: driver: bridge ipam: config: - subnet: 192.169.0.0/16
從新啓動composetest項目,並查看網絡詳情,確認容器ip是否設置成功
// 從新啓動composetest項目 docker-compose up -d // 查看網絡詳情 docker network inspect composetest_nginx-lsb
在composetest項目中添加一臺nginx服務器做爲負載均衡服務器
// 在.env文件裏添加一個變量 nginx_lsb web1_addr=192.169.0.2 web2_addr=192.169.0.3 nginx_lsb=192.169.0.100 // 修改docker-compose.yml文件,加入變量定義 version: "3" services: nginx-lsb: container_name: nginx-lsb image: "centos:nginx" ports: - "8000:80" privileged: true volumes: - "/app/nginx/nginx.conf:/etc/nginx/nginx.conf" networks: nginx-lsb: ipv4_address: ${nginx_lsb} web1: container_name: web1 image: "centos:httpd" ports: - "8080:80" privileged: true volumes: - "/app/www/web1/:/var/www/html/" command: ['/usr/sbin/init'] networks: nginx-lsb: ipv4_address: ${web1_addr} web2: container_name: web2 image: "centos:httpd" ports: - "8081:80" privileged: true volumes: - "/app/www/web2/:/var/www/html/" command: ['/usr/sbin/init'] networks: nginx-lsb: ipv4_address: ${web2_addr} networks: nginx-lsb: driver: bridge ipam: config: - subnet: 192.169.0.0/16 // 從新啓動composetest項目 docker-compose up -d
修改nginx.conf配置文件,配置負載均衡
upstream mydocker { server 192.169.0.2; server 192.169.0.3; } server { listen 80; server_name mydocker; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; proxy_pass http://mydocker; } }
從新啓動nginx-lsb,加載配置文件
docker-composer restart nginx-lsb
訪問 http://服務器IP地址:8000,測試服務器負載均衡!
注意:上一篇已經在兩臺httpd服務器上放置了不一樣的web文件