docker-stack一键编排lnmp

环境要求

1, 配置nfs存储卷

1,在docker swarm集群中所有节点都确认安装nfs客户端软件

<code>/<code>

2, 在192.168.122.1 上搭建nfs,共享目录给docker swarm集群中所有节点挂载

<code>[root@nfs ~] [root@nfs ~] /opt/dockervolume *(rw,no_root_squash,sync) [root@nfs ~]/<code>

3, 在docker swarm集群中所有节点创建存储卷,并验证

<code> {

"CreatedAt"

:

"2019-06-12T13:24:09+08:00"

,

"Driver"

:

"local"

,

"Labels"

: {},

"Mountpoint"

:

"/var/lib/docker/volumes/nginx_volume/_data"

,

"Name"

:

"nginx_volume"

,

"Options"

: {

"device"

:

":/opt/dockervolume"

,

"o"

:

"addr=192.168.122.1,rw"

, 注意这里的挂载参数要有

"type"

:

"nfs"

},

"Scope"

:

"local"

}

local

nginx_volume nginx_volume/<code>

2, 下载镜像

在任意能上外网的机器上操作

<code>[root@nfs ~]/<code>

3, 准备是相关配置文件

<code>[root@nfs ~] [root@nfs ~]/<code>

准备nginx主配置文件

<code>[root@nfs dockerfile] worker_processes auto; error_log /

var

/log/nginx/error.log warn; pid /

var

/run/nginx.pid; events {

use

epoll

; worker_connections

65535

; } http {

include

/etc/nginx/mime.types; default_type application/octet-stream; log_format main

'$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

; access_log sendfile /

var

/log/nginx/access.log on; on; main; keepalive_timeout

65

;

include

/etc/nginx/conf.d/<code>

准备nginx子配置文件

<code>[root@nfs dockerfile]

listen

80

; server_name

_

;

index

index.php index.html; root /var/www/html; location ~ \.php$ { fastcgi_pass

127.0

.

0

.

1

:

9000

; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }/<code>

准备时区文件

<code>[root@nfs dockerfile]/<code>

准备php-fpm子配置文件

<code>

[root@nfs dockerfile]

user

= nginx group = nginx

listen

=

127.0

.

0.1

:

9000

pm

= dynamic pm.max_children =

64

pm.start_servers

=

20

pm.min_spare_servers

=

5

pm.max_spare_servers

=

35

pm.max_requests

=

500

rlimit_files

=

1024

/<code>

4, 准备镜像

编写Dockerfile

<code>[root@nfs dockerfile] MAINTAINER daniel@itcast.cn RUN echo

'Asia/Shanghai'

>

/etc/timezone

&& rm -f /etc/nginx/nginx.conf && rm -f /usr/

local

/etc/php- fpm.d/www.conf COPY

localtime

/etc/

localtime

COPY nginx.conf /etc/nginx/nginx.conf COPY web.conf /etc/nginx/conf.d/web.conf COPY php-fpm-www.conf /usr/

local

/etc/php-fpm.d/www.conf CMD /usr/sbin/nginx -c /etc/nginx/nginx.conf &&

/usr/local

/sbin/php

-fpm -c /usr/

local

/etc/php-fpm.conf/<code>

构建镜像

<code>

[root@nfs

dockerfile]#

docker

build

-f

dockerfile

-t

192.168

.122

.18

/library/nginx-php-fpm:v1

.

/<code>

上传镜像到harbor

<code>

[root@nfs

dockerfile]#

docker

login

192.168

.122

.18

[root@nfs

dockerfile]#

docker

push

192.168

.122

.18

/library/nginx-php-fpm:v1

/<code>

5, 编排docker stack

编写YMAL文件

在docker swarm集群中的管理节点进行操作

<code>

[root@manager

~]#

cat discuz.yml version:

'3'

services: db:

image: 192.168.122.18/library/mysql:5.7 environment:

MYSQL_ROOT_PASSWORD:

123

MYSQL_DATABASE: discuz MYSQL_USER: discuz_user MYSQL_PASSWORD:

123

deploy:

replicas:

1

nginx-php:

image: 192.168.122.18/library/nginx-php-fpm:v1 depends_on:

-

db ports:

-

"8011:80"

volumes:

-

"nginx_volume:/var/www/html"

deploy:

replicas:

1

volumes:

nginx_volume: driver: local driver_opts:

type:

"nfs"

o:

"addr=192.168.122.1,rw"

device:

":/opt/dockervolume"

/<code>

一键发布stack

<code>

~]# docker stack deploy -c discuz2.yml discuz

Creating

network discuz_default

Creating

service discuz_db

Creating

service discuz_nginx-php

/<code>

6, 拷贝discuz代码到nfs共享

在nfs服务器上操作

<code>[root@nfs dockerfile] [root@nfs dockerfile] -rf cp: overwrite ‘/opt/dockervolume/index.php’? y/<code>

7, 通过浏览器安装


<code>[root@nfs dockerfile]/<code>

上图少了-R参数,请加上