十一、docker-compose编排工具
一、安装docker-compose
安装docker-compose
# 下载pip软件
yum install -y python2-pip
# 下载 docker-compose
pip install docker-compose
国内开启pip 下载加速:http://mirrors.aliyun.com/help/pypi
mkdir ~/.pip/
cat > ~/.pip/pip.conf <<'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
二、编排启动镜像
1. 创建文件目录
[root@docker01 ~]# mkdir /opt/my_wordpress/
[root@docker01 ~]# cd /opt/my_wordpress/
2、编写编排文件
[root@docker01 my_wordpress]# vim docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
3、启动
[root@docker01 my_wordpress]# docker-compose up
#启动方法:docker-compose up
#后台启动方法:docker-compose up -d
4、浏览器上访问http://10.0.0.100:8000
进行wordpress的安装即可
三、haproxy代理后端docker容器
1、修改编排脚本
[root@docker01 my_wordpress]# cat docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
2、同时启动两台wordpress
[root@docker01 my_wordpress]# docker-compose scale wordpress=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... done
3、安装haproxy
[root@docker01 ~]# yum install haproxy -y
4、修改haproxy配置文件
关于配置文件的详细说明,参考:https://www.cnblogs.com/MacoLee/p/5853413.html
[root@docker01 ~]#cp /etc/haproxy/haproxy.cfg{,.bak}
[root@docker01 ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats level admin #支持命令行控制
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats
mode http
bind 0.0.0.0:8888
stats enable
stats uri /haproxy-status
stats auth admin:123456
frontend frontend_www_example_com
bind 10.0.0.100:8000
mode http
option httplog
log global
default_backend backend_www_example_com
backend backend_www_example_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
balance roundrobin
server web-node1 10.0.0.100:32768 check inter 2000 rise 30 fall 15
server web-node2 10.0.0.100:32769 check inter 2000 rise 30 fall 15
5、启动haproxy
systemctl start haproxy
systemctl enable haproxy
6、使用浏览器访问hapeoxy监听的8000端口可以看到负载的情况
7、使用浏览器访问 http://10.0.0.100:8888/haproxy-status
可以看到后端节点的监控状况,
四、安装socat 直接操作socket控制haproxy
1、安装软件
yum install socat.x86_64 -y
2、查看帮助
[root@docker01 web_data]# echo "help"|socat stdio /var/lib/haproxy/stats
3、下线后端节点
echo "disable server backend_www_example_com/web-node2"|socat stdio /var/lib/haproxy/stats
4、上线后端节点
echo "enable server backend_www_example_com/web-node3"|socat stdio /var/lib/haproxy/stats
5、编写php测试页,放到/data/web_data下,在浏览器中访问可以查看当前的节点
[root@docker01 web_data]# vim 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>