一、简介
目的:使用Docker Swarm 搭建lnmp来部署WordPress
- 使用Dockerfile构建nginx、php镜像
- 将构建的镜像上传docker私有仓库
- 使用volume做workpress网站文件持久化(每个工作节点都要保存一份数据)
- 使用nfs共享存储做ngixn配置文件持久化(一份数据多个工作节点共享)
- mysql镜像直接从dockerhub中获取
- mysql的配置文件使用docker config创建(当然也可以用挂载的方式)
- mysql 数据使用volume数据卷持久化
- 启动是mysql--php--nginx
二、准备
(1)如何创建私有仓库: http://www.cnblogs.com/bigberg/p/8821872.html
# 已经创建好的私有库中的镜像 [root@manager ~]# curl http://172.16.60.95:5000/v2/_catalog {"repositories":["busyboxx","nginx","php"]} # 其中nginx和php是准备要用的
(2)Dockerfiel文件
1 FROM centos:latest 2 MAINTAINER bigberg 3 RUN yum -y install pcre-devel openssl-devel net-tools gcc gcc-c++ zlib zlib-devel 4 make openssl 5 ADD nginx-1.12.1.tar.gz /tmp/ 6 RUN cd /tmp/nginx-1.12.1 7 && ./configure --prefix=/usr/local/nginx 8 --with-http_ssl_module 9 --with-http_gzip_static_module 10 --with-http_realip_module 11 && make && make install 12 13 ADD nginx.conf /usr/local/nginx/conf/nginx.conf 14 15 RUN mkdir -p /usr/local/nginx/logs 16 && mkdir -p /usr/local/nginx/conf/vhosts 17 && groupadd -g 1001 nginx 18 && useradd -g 1001 -u 1001 -s /sbin/nologin -M nginx 19 RUN cat /usr/share/zoneinfo/Asia/Shanghai > /etc/localtime 20 21 EXPOSE 80 22 EXPOSE 443 23 CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
1 FROM centos:latest 2 MAINTAINER bigberg 3 4 RUN yum -y install gcc gcc-c++ gd-devel libxml2 libxml2-devel libcurl-devel 5 openssl openssl-devel curl curl-devel libjpeg libjpeg-devel libpng 6 freestyle freestyle-devel pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-devel 7 ADD php-7.2.3.tar.gz /tmp/ 8 RUN cd /tmp/php-7.2.3 9 && ./configure --prefix=/usr/local/php 10 --with-curl --with-freetype-dir --with-gd 11 --with-gettext --with-iconv-dir --with-kerberos 12 --with-libdir=lib64 --with-libxml-dir --with-mysqli 13 --with-openssl --with-pcre-regex --with-pdo-mysql 14 --with-pdo-sqlite --with-pear --with-png-dir 15 --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib 16 --with-bz2 --with-mhash --enable-fpm --enable-bcmath 17 --enable-libxml --enable-inline-optimization --enable-gd-native-ttf 18 --enable-mbregex --enable-mbstring --enable-opcache 19 --enable-pcntl --enable-shmop --enable-soap --enable-sockets 20 --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip 21 && make && make install 22 && cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 23 && chmod a+x /etc/init.d/php-fpm 24 && groupadd -g 1001 www 25 && useradd -g 1001 -u 1001 www 26 27 RUN cat /usr/share/zoneinfo/Asia/Shanghai > /etc/localtime 28 ADD php-fpm.conf /usr/local/php/etc/php-fpm.conf 29 ADD php.ini /usr/local/php/etc/php.ini 30 ADD www.conf /usr/local/php/etc/php-fpm.d/www.conf 31 32 EXPOSE 9000 33 CMD ["/usr/local/php/sbin/php-fpm", "--nodaemonize"]
安装包并未提供,其余配置文件可以在下面下载:
https://github.com/Bigberg/docker/tree/master/compose-lnmp
(3)系统环境
节点名称 | IP | 操作系统 | 内核版本 |
manager | 172.16.60.95 | CentOs7 | 4.16.1-1.el7.elrepo.x86_64 |
node-01 | 172.16.60.96 | CentOs7 | 4.16.1-1.el7.elrepo.x86_64 |
node-02 | 172.16.60.97 | CentOs7 | 4.16.1-1.el7.elrepo.x86_64 |
node-03 | 172.16.60.98 | CentOs7 | 4.16.1-1.el7.elrepo.x86_64 |
#docker -v Docker version 18.03.0-ce, build 0520e24
(4)创建一个overlay网络
# manager节点 docker network create -d overlay lnmp
三、部署服务
3.1 创建mysql服务
1)创建mysql配置文件
[root@manager ~]# docker config create my.cnf /data/conf/my.cnf tw5h0s7cby10x5kqhy26vn7hz [root@manager ~]# docker config ls ID NAME CREATED UPDATED tw5h0s7cby10x5kqhy26vn7hz my.cnf 11 seconds ago 11 seconds ago
简单的mysql配置文件
[client] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set = utf8 [mysql] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set = utf8 [mysqld] port = 3306 basedir = /var/lib/mysql datadir = /var/lib/mysql socket = /var/lib/mysql/mysql.sock character-set-server = utf8 skip-external-locking slow-query-log = on long_query_time = 1 slow_query_log_file = /var/lib/mysql/slow.log sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER log-error=/var/lib/mysql/mysql.log pid-file=/var/lib/mysql/mysql.pid
2)创建mysql服务
[root@manager ~]# docker service create --name mysql --replicas 1 --network lnmp --config src=my.cnf,target=/etc/mysql/conf.d/my.cnf --mount type=volume,src=dbdata,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wp123456 -e MYSQL_DATABASE=wordpress --publish 3306:3306 mysql 参数: --name: 服务名称 --replicas: 副本集个数 --network:使用的网络 --config:配置文件 --mount:挂载数据卷 MYSQL_ROOT_PASSWORD:设置mysql的root密码 MYSQL_USER:创建mysql的一个用户 MYSQL_PASSWORD:创建的用户密码 MYSQL_DATABASE:新建一个数据库实例 --publish:暴露端口
3)查看mysql服务信息
[root@manager ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS k2nkk4dkpycb mysql replicated 1/1 mysql:latest *:3306->3306/tcp [root@manager ~]# docker service ps mysql ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS uzzp4mpmbg59 mysql.1 mysql:latest node-01 Running Running 2 minutes ago
4)登入mysql查看配置信息
登入ip在4个节点中都可以,配置信息确实是docker my.cnf中设置的
5)在node-01上查看持久化数据
[root@node-01 ~]# docker volume ls DRIVER VOLUME NAME local dbdata
mysql的数据确实已经保存在数据卷中了
3.2 创建php服务
1)创建服务
[root@manager ~]# docker service create > --name php > --replicas 3 > --network lnmp > --mount type=volume,src=wwwroot,dst=/usr/local/nginx/html > 172.16.60.95:5000/php:v1.0.1 l0hb9efv4qckxk6ibh36vax9f overall progress: 3 out of 3 tasks 1/3: running 2/3: running 3/3: running verify: Service converged 参数: --name php:服务名称 --replicas 3:副本数为3 --network lnmp:使用lnmp这个overlay网络 --mount :挂载数据卷,这个和nginx要使用的一样,方便php的fastcgi通过nginx找到文件 172.16.60.95:5000/php:v1.0.1:使用私有镜像
2)查看服务
[root@manager ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS k2nkk4dkpycb mysql replicated 1/1 mysql:latest *:3306->3306/tcp l0hb9efv4qck php replicated 3/3 172.16.60.95:5000/php:v1.0.1 [root@manager ~]# docker service ps php ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS p9x3eggj1up8 php.1 172.16.60.95:5000/php:v1.0.1 node-01 Running Running about a minute ago kko40sac6twy php.2 172.16.60.95:5000/php:v1.0.1 node-02 Running Running about a minute ago sz8q663q4gdt php.3 172.16.60.95:5000/php:v1.0.1 node-03 Running Running about a minute ago # 现在已经有两个服务mysql和 php
3.3 创建nginx服务
1)创建一个nfs共享存储
如何创建docker nfs存储:http://www.cnblogs.com/bigberg/p/8795265.html
# 该nfs数据卷是用来存储nginx配置文件的,省得每个节点都要做配置 # 创建目录 # mkdir -p /data/conf/lnmp_nginx # 修改nfs配置 vim /etc/exports /data/conf/lnmp_nginx 172.16.60.0/24(rw,sync,no_root_squash) # 重启nfs # systemctl restart nfs # 挂载测试(工作节点) [root@node-03 ~]# mount -t nfs 172.16.60.95:/data/conf/lnmp_nginx /tmp You have new mail in /var/spool/mail/root [root@node-03 ~]# # 取消挂载 [root@node-03 ~]# umount /tmp/
2)创建nginx服务
[root@manager ~]# docker service create --name nginx --replicas 3 --network lnmp --publish 8888:80 --mount type=volume,src=wwwroot,dst=/usr/local/nginx/html --mount 'type=volume,src=nginx-conf,dst=/usr/local/nginx/conf/vhosts,volume-driver=local,volume-nocopy=true,volume-opt=type=nfs,volume-opt=device=172.16.60.95:/data/conf/lnmp_nginx,"volume-opt=o=addr=172.16.60.95,vers=4,soft,timeo=180,bg,tcp,rw"' 172.16.60.95:5000/nginx:v1.0.1 参数: --name nginx:服务名称 --replicas 3:副本数3 --network lnmp:使用lnmp这个overlay网络 --publish 8888:80:映射的端口 --mount:第一个mount为网站文件持久化使用,第二个mount持久化nginx配置文件 172.16.60.95:5000/nginx:v1.0.1:使用私有镜像
3)查看服务
[root@manager ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS k2nkk4dkpycb mysql replicated 1/1 mysql:latest *:3306->3306/tcp q4ddyozuqguo nginx replicated 3/3 172.16.60.95:5000/nginx:v1.0.1 *:8888->80/tcp l0hb9efv4qck php replicated 3/3 172.16.60.95:5000/php:v1.0.1 [root@manager ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS tr98r5yfo82r nginx.1 172.16.60.95:5000/nginx:v1.0.1 node-03 Running Running 29 seconds ago mrlpj5kiixfs nginx.2 172.16.60.95:5000/nginx:v1.0.1 node-02 Running Running 30 seconds ago pxrhvhinrhpe nginx.3 172.16.60.95:5000/nginx:v1.0.1 node-01 Running Running 30 seconds ago # 现在有3个服务,而且都已经起来了
4)在nfs存储中添加一个nginx配置文件
[root@manager ~]# cd /data/conf/lnmp_nginx/ [root@manager lnmp_nginx]# vim wordpress.con server { listen 80; server_name localhost; root html; index index.htm index.hmtl index.php; location ~ .php$ { root html; fastcgi_pass php-cgi:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } #因为是nfs共享存储,所以每个工作节点的nginx中vhosts中都有一个wordpress.conf # 每个工作节点的nginx 需要reload一下
访问测试:
四、部署wordpress
1)放置网站文件(所有节点操作)
# wwwroot数据卷是放置网站文件的,但是是volume本地驱动,所以不是共享数据卷,需要在每个节点上都放置WordPress网站文件 #查看wwwroot数据卷 [root@node-03 _data]# docker volume inspect wwwroot [ { "CreatedAt": "2018-04-16T17:24:01+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/wwwroot/_data", "Name": "wwwroot", "Options": {}, "Scope": "local" } ] # 存放位置在/var/lib/docker/volumes/wwwroot/_data # 下载WordPress压缩包 wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz # 将文件解压到wwwroot对应的文件夹中 tar -zxvf wordpress-4.9.4-zh_CN.tar.gz -C /var/lib/docker/volumes/wwwroot/_data/
2)WordPress部署
# 访问任意节点 http://172.16.60.96:8888/wordpress
①
②用户名/密码(wordpress/wp123456)
③所有节点
vim /var/lib/docker/volumes/wwwroot/_data/wordpress/wp-config.php
④填写信息,设置用户(admin/123456)
⑤完成登入
到此部署完成