docker实现haproxy代理后端wordpress
1,修改编排脚本
[root@docker-compose ~]# cat docker-compose.yml
[root@docker-compose ~]# cat /etc/docker/wordpress/docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234.com
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress1:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "32768:80"
expose:
- 80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
wordpress2:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "32769:80"
expose:
- 80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
haproxy:
depends_on:
- wordpress1
- wordpress2
image: haproxy
volumes:
- ./haproxy:/usr/local/etc/haproxy
links:
- wordpress1
- wordpress2
ports:
- "8080:80"
expose:
- 80
文件详解
[root@docker-compose ~]# cat /etc/docker/wordpress/docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7 #mysql5.7镜像
volumes:
- /data/db_data:/var/lib/mysql #把宿主机的/data/db_data目录映射到容器中的/var/lib/mysql目录
restart: always
environment: #设置镜像变量并把这些变量保存到容器中
MYSQL_ROOT_PASSWORD: 1234.com
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress1: #wordpress
depends_on: #需要先启动db,才能启动自己
- db
image: wordpress:latest #最新的wordpress镜像
volumes:
- /data/web_data:/var/www/html #把宿主机的/data/web_data目录映射到容器中的/var/www/html目录
ports:
- "32768:80" #端口映射,把容器的80端口映射到宿主机的32768端口
expose:
- 80 #开放容器端口80,但映射到主机
restart: always
environment: #设置镜像变量并把这些变量保存到容器中
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
wordpress2: #wordpress
depends_on: #需要先启动db,才能启动自己
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "32769:80" #端口映射,把容器的80端口映射到宿主机的32769端口
expose:
- 80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
haproxy: #haproxy
depends_on: #需要先启动wordpress1和2,才能启动自己
- wordpress1
- wordpress2
image: haproxy #haproxy镜像
volumes:
- ./haproxy:/usr/local/etc/haproxy #把宿主机的./haproxy目录映射到容器中的/usr/local/etc/haproxy目录
links: #连接到wordpress1和2中的容器
- wordpress1
- wordpress2
ports: #端口映射,容器的80端口映射到宿主机的8080端口
- "8080:80"
expose: #暴露容器端口80,仅暴露端口,不映射
- 80
2,准备haproxy配置文件
[root@docker-compose haproxy]# cat haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats uri /status
frontend balancer
bind 0.0.0.0:80
default_backend web_backends
backend web_backends
balance roundrobin
server web1 wordpress1:80 check #这里的端口是容器暴露的80端口,不是映射的端口
server web2 wordpress2:80 check
启动
[root@docker-compose wordpress]# docker-compose up -d
Creating network "wordpress_default" with the default driver
Creating wordpress_wordpress1_1 ... done
Creating wordpress_wordpress2_1 ... done
Creating wordpress_wordpress1_1 ...
Creating wordpress_haproxy_1 ... done
[root@docker-compose wordpress]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c9218bff183 haproxy "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp wordpress_haproxy_1
3bb16505835e wordpress:latest "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp wordpress_wordpress1_1
2acff996f428 wordpress:latest "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:32769->80/tcp wordpress_wordpress2_1
812a1e1f7600 mysql:5.7 "docker-entrypoint.s…" 7 seconds ago Up 5 seconds 3306/tcp, 33060/tcp wordpress_db_1
3、编写php测试页
- 放到/data/web_data下,在浏览器中访问可以查看当前访问节点信息
[root@docker-compose haproxy]# cat /data/web_data/check.php
<html>
<head>
<title>PHP测试</title>
</head>
<body>
<?php echo '<p>Hello World </p>'; ?>
<?php echo "访问的服务器地址是:"."<fontcolor=red>".$_SERVER['SERVER_ADDR']."</font>"."<br>";
echo"访问的服务器域名是:"."<fontcolor=red>".$_SERVER['SERVER_NAME']."</font>"."<br>";
?>
</body>
</html>
4、使用浏览器访问测试页面
- hapeoxy 监听的8080端口可以看到负载的情况
5、使用浏览器访问状态页面
来源:oschina
链接:https://my.oschina.net/u/4274555/blog/4497035