docker实现haproxy代理后端wordpress

筅森魡賤 提交于 2020-08-15 17:29:36

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、使用浏览器访问状态页面

  • http://192.168.219.101:8080/status

  • 可以看到后端节点的监控状况,

  • hapeoxy 监听的8080端口可以看到负载的情况

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!