Docker swarm 使用服务编排部署lnmp

一、简介

目的:在Docker Swarm集群中,使用stack服务编排搭建lnmp来部署WordPress

  1. 使用私有仓库的nginx和php镜像
  2. mysql使用dockerhup最新镜像
  3. 使用nfs共享存储做数据持久化,包括nginx配置文件、wordpress网站文件、mysql配置文件和数据

  使用nfs volume做数据持久化,可以让所有节点共享数据,这样数据只需要保留一份,方便部署和更新

二、准备

  (1)新建nfs存储的目录 

# 配置文件目录
mkdir -p /data/conf/{stack_nginx,stack_mysql}

# 数据存放目录
mkdir -p /data/storage/{mysql_data,web_data}

  (2)配置nfs

vim /etc/exports

/data/conf/stack_nginx 172.16.60.0/24(rw,sync,no_root_squash)
/data/conf/stack_mysql 172.16.60.0/24(rw,sync,no_root_squash)
/data/storage/web_data 172.16.60.0/24(rw,sync,no_root_squash)
/data/storage/mysql_data 172.16.60.0/24(rw,sync,no_root_squash)

  (3)重新nfs

systemctl restart nfs

  

三、部署服务

  (1)编写ymal文件

version: '3.3'
services:
  nginx:
    image: 172.16.60.95:5000/nginx:v1.0.1
    ports:
      - "8000:80"
    networks:
      - stack_net
    volumes:
      - type: volume
        source: webdata
        target: /usr/local/nginx/html
        volume:
          nocopy: true
      - type: volume
        source: nginx_conf
        target: /usr/local/nginx/conf/vhosts
        volume:
          nocopy: true
    deploy:
      mode: replicated
      replicas: 3
      resources:
        limits:
          cpus: '0.2'
          memory: 200M
        reservations:
          cpus: '0.1'
          memory: 50M
    depends_on:
      - php
      - mysql  
  php:
    image: 172.16.60.95:5000/php:v1.0.1
    networks:
      - stack_net
    volumes:
      - type: volume
        source: webdata
        target: /usr/local/nginx/html
        volume:
          nocopy: true
    deploy:
      mode: replicated
      replicas: 3
      resources:
        limits:
          cpus: '0.2'
          memory: 200M
        reservations:
          cpus: '0.1'
          memory: 50M

  mysql:
    image: mysql:latest
    networks:
      - stack_net
    ports:
      - "3307:3306"
    volumes:
      - type: volume
        source: mysql_cnf
        target: /etc/mysql/conf.d
        volume:
          nocopy: true
      - type: volume
        source: dbdata
        target: /var/lib/mysql
        volume:
          nocopy: true
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 200M
        reservations:
          cpus: '0.25'
          memory: 50M
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wp
      MYSQL_PASSWORD: wp123456
     
networks:
  stack_net:
    driver: overlay

volumes:
  webdata:
    driver_opts:
      type: "nfs"
      o: "addr=172.16.60.95,vers=4,soft,timeo=180,bg,tcp,rw"
      device: "172.16.60.95:/data/storage/web_data"
  dbdata:
    driver_opts:
      type: "nfs"
      o: "addr=172.16.60.95,vers=4,soft,timeo=180,bg,tcp,rw"
      device: "172.16.60.95:/data/storage/mysql_data"
  nginx_conf:
    driver_opts:
      type: "nfs"
      o: "addr=172.16.60.95,vers=4,soft,timeo=180,bg,tcp,rw"
      device: "172.16.60.95:/data/conf/stack_nginx"

  mysql_cnf:
    driver_opts:
      type: "nfs"
      o: "addr=172.16.60.95,vers=4,soft,timeo=180,bg,tcp,rw"
      device: "172.16.60.95:/data/conf/stack_mysql"

  (2)部署

# docker stack deploy -c service_stack.yml wp
Creating service wp_php
Creating service wp_mysql
Creating service wp_nginx

  (3)查看服务

  虽然服务名称中加了wp_*,但是在swarm的dns解析中,一样可以识别ymal文件中定义的service名称

# docker service ls
pmd1si576gcf        wp_mysql            replicated          1/3                 mysql:latest                     *:3307->3306/tcp
3f6y5uct3qpc        wp_nginx            replicated          3/3                 172.16.60.95:5000/nginx:v1.0.1   *:8000->80/tcp
ut8kphc9pu86        wp_php              replicated          3/3                 172.16.60.95:5000/php:v1.0.1
[root@manager conf]# docker stack ls
NAME                SERVICES
wp                  3

  (4)查看数据卷

# 工作节点
[root@node-01 ~]# docker volume ls
DRIVER              VOLUME NAME

local               wp_dbdata
local               wp_mysql_cnf
local               wp_nginx_conf
local               wp_webdata

  

四、部署WordPress

  nginx和mysql配置文件和上一篇部署lnmp是一样的

  (1)下载wordpress 

# 下载WordPress压缩包
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz

# 解压到web文件共享存储的目录或者 数据卷目录下
tar -zxvf wordpress-4.9.4-zh_CN.tar.gz -C /var/lib/docker/volumes/wp_webdata/_data/

  (2)WordPress网站设置

# 访问任意节点
http://172.16.60.95:8000/wordpress

  ①

  

  ②

  

  ③第一次手动写入

  

  ④ 填写信息,设置用户(admin/123456)

  

  ⑤完成登入

  

 

  

 

posted @ 2018-04-18 14:46  Bigberg  阅读(2431)  评论(7编辑  收藏  举报