Menggunakan pengurusan rangkaian docker dan tetapan ip kontena sebagai pengetahuan asas untuk melaksanakan pengimbangan beban nginx
Lihat semua rangkaian docker
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(桥接)类型
Nyatakan alamat ip kontena
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
Gunakan docker-compose untuk memulakan bekas
docker-compose up -d
Periksa sama ada bekas dimulakan dan sahkan sama ada rangkaian nginx-lsb dicipta
// 可以查看当前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网络,命名是容器组项目的 文件名开头_网络名
Lihat butiran rangkaian 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": "" } }, ... */
Gunakan fail persekitaran env_file:
boleh difahami secara ringkas sebagai: tentukan pembolehubah dalam docker-compose.yml dan pembolehubah rujukan dalam fail .env luaran
Alamat dokumen rasmi:
// 还是在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
Mulakan semula projek composetest dan semak butiran rangkaian untuk mengesahkan sama ada IP kontena berjaya ditetapkan
// 重新启动composetest项目 docker-compose up -d // 查看网络详情 docker network inspect composetest_nginx-lsb
Tambah pelayan nginx sebagai pelayan pengimbangan beban dalam projek composetest
// 在.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
Ubah suai fail konfigurasi nginx.conf, konfigurasikan pengimbangan beban
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; } }
Mulakan semula nginx-lsb, muatkan fail konfigurasi
docker-composer restart nginx-lsb
Lawati http://alamat ip pelayan: 8000, uji pengimbangan beban pelayan !
Atas ialah kandungan terperinci Cara menggunakan Docker's Compose untuk melaksanakan pengimbangan beban nginx. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!