CentOS 7.x 安装Docker-ce社区版本
https://www.osyunwei.com/archives/11592.html
搭建docker本地私有镜像仓库
https://www.osyunwei.com/archives/11928.html
CentOS 7.x安装部署NFS文件共享服务器
https://www.osyunwei.com/archives/11942.html
使用Docker-Compose实现Docker容器编排
https://www.osyunwei.com/archives/11903.html
一、通过在Docker Swarm集群内每台服务器挂载nfs目录实现共享存储
1、在nfs服务器配置好挂载目录
/nfs 192.168.21.0/24(insecure,rw,sync,no_subtree_check,no_root_squash)
2、在每台服务器上挂载nfs服务器目录,并设置开机自动挂载
mount -t nfs -o nolock,nfsvers=3,vers=3,soft,intr,bg,rw,rsize=32768,wsize=32768 192.168.21.8:/nfs /nfs
3、集群内所有服务器登录到私有仓库,下载好所需要的容器镜像
docker login hub.osyunwei.com
[root@node02 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.osyunwei.com/nginx 1.20.1 01dbf760f0dc 2 weeks ago 1.22GB
hub.osyunwei.com/php-fpm 8.0.11 597d18ebb61d 2 weeks ago 2.66GB
hub.osyunwei.com/mysql v8.0.26 b4d1dc08fa3f 2 weeks ago 548MB
4、管理master节点使用docker stack deploy创建容器
mkdir -p /nfs/data/docker-compose/conf
vi /nfs/data/docker-compose/conf/wordpress.yaml
version: '3'
services:
#容器名称
mysql:
image: hub.osyunwei.com/mysql:v8.0.26
networks:
- mycust-overlay
volumes:
- /nfs/data/mysql/database:/var/lib/mysql
- /nfs/data/mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8 --collation-server=utf8_general_ci
environment:
- MYSQL_ROOT_PASSWORD=123456
restart: always
deploy:
replicas: 1
#设置优先启动mysql
depends_on:
- mysql
#容器名称
php:
image: hub.osyunwei.com/php-fpm:8.0.11
networks:
- mycust-overlay
volumes:
- /nfs/data/web:/data/web
- /nfs/data/php/etc:/usr/local/php/etc
restart: always
deploy:
replicas: 3
#容器名称
nginx:
#容器所使用的镜像名称
image: hub.osyunwei.com/nginx:1.20.1
networks:
- mycust-overlay
#设置端口映射(宿主机端口:容器内端口)
ports:
- 80:80
volumes:
- /nfs/data/web:/data/web
- /nfs/data/nginx/conf/ssl:/usr/local/nginx/conf/ssl
- /nfs/data/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf
- /nfs/data/nginx/conf/vhost:/usr/local/nginx/conf/vhost.conf
- /nfs/data/nginx/conf/vhost:/usr/local/nginx/conf/vhost
restart: always
deploy:
replicas: 3
networks:
mycust-overlay:
external: true
:wq! #保存退出
#yaml文件格式有前后缩进和空格,一定要注意
#需要认证的私有仓库必须加 --with-registry-auth 才能下载镜像,部署成功
cd /nfs/data/docker-compose/conf
#执行部署命令
docker stack deploy -c wordpress.yaml --with-registry-auth wordpress
#查看服务列表
docker stack ls
#查看服务信息
docker stack ps wordpress
[root@master01 conf]# docker stack ps wordpress
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
6rph3as6avgh wordpress_nginx.1 hub.osyunwei.com/nginx:1.20.1 node02.k8s Running Running 11 minutes ago
boivdsuyo0rq wordpress_php.1 hub.osyunwei.com/php-fpm:8.0.11 node02.k8s Running Running 11 minutes ago
mad4psqwgcmf wordpress_mysql.1 hub.osyunwei.com/mysql:v8.0.26 node02.k8s Running Running 11 minutes ago
c75r6t1kle9q wordpress_nginx.2 hub.osyunwei.com/nginx:1.20.1 node01.k8s Running Running 12 minutes ago
9b4isjmf43z5 wordpress_php.2 hub.osyunwei.com/php-fpm:8.0.11 node01.k8s Running Running 12 minutes ago
txxamc3yqfs0 wordpress_nginx.3 hub.osyunwei.com/nginx:1.20.1 node01.k8s Running Running 12 minutes ago
s1szv6110ngc wordpress_php.3 hub.osyunwei.com/php-fpm:8.0.11 node02.k8s Running Running 11 minutes ago
#移除服务命令
docker stack down wordpress
二、通过在Docker Swarm集群内使用数据卷volume实现NFS共享存储
1、在所有工作节点上手动创建docker volume数据卷
#创建volume数据卷
docker volume create --driver local \
--opt type=nfs4 \
--opt o=addr=192.168.21.8,rw \
--opt device=:/nfs \
volume-nfs
--opt type=nfs4 #类型
--opt o=addr=192.168.21.8,rw #nfs服务器ip,rw具有读写权限
--opt device=:/nfs #共享目录,也可以是共享目录下的子目录
volume-nfs #volume数据卷名称
docker volume ls #查看数据卷列表
docker volume inspect volume-nfs #查看数据卷信息
[root@node01 _data]# docker volume inspect volume-nfs
[
{
"CreatedAt": "2021-12-03T00:19:21+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/volume-nfs/_data",
"Name": "volume-nfs",
"Options": {
"device": ":/nfs",
"o": "addr=192.168.21.8,rw",
"type": "nfs4"
},
"Scope": "local"
}
]
cd /var/lib/docker/volumes/volume-nfs/_data #进入到数据卷目录
ll #查看
[root@node01 _data]# ll
total 0
#特别注意:
这个时候数据卷volume-nfs目录是空的,此刻它仅仅相当于我们在宿主机上创建了一个挂载目录,但是没有执行任何挂载命令。
即使nfs服务器共享目录/nfs里面有数据,也不会显示出来,因为还没有执行任何挂载命令。
2、在Docker Swarm集群管理节点执行,创建服务,进行数据卷挂载测试(使用alpine镜像,默认会从docker官方仓库下载)
docker service create --replicas 2 --name nfs --mount 'type=volume,src=volume-nfs,dst=/nfs' alpine sleep 3600
[root@master01 nfs]# docker service create --replicas 2 --name nfs --mount 'type=volume,src=volume-nfs,dst=/nfs' alpine sleep 3600
uibwefgqab9t30caxmg77h45f
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
#在工作节点执行
cd /var/lib/docker/volumes/volume-nfs/_data #进入到数据卷目录
mkdir new-test #创建测试目录
[root@node01 _data]# cd /var/lib/docker/volumes/volume-nfs/_data
[root@node01 _data]# mkdir new-test
[root@node01 _data]# ls
new-test
[root@node01 _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a011b2aac989 alpine:latest "sleep 3600" 2 minutes ago Up 2 minutes nfs.1.lxxww3osklacwkluz45ni8h56
[root@node01 _data]# docker exec -it a011b2aac989 /bin/sh #进入容器
/ # ls
bin dev etc home lib media mnt nfs opt proc root run sbin srv sys tmp usr var
/ # cd /nfs
/nfs # ls
new-test
/nfs # exit
#可以看到刚才创建的目录已经同步到容器目录里面了
docker service ls #查看
docker service rm nfs #删除服务
#或者直接执行下面的命令(挂载数据卷的同时创建volume数据卷)
docker service create --replicas 2 \
--name nfs \
--mount 'type=volume,src=volume-nfs,dst=/nfs,volume-driver=local,volume-opt=type=nfs4,volume-opt=device=192.168.21.8:/nfs,"volume-opt=o=addr=192.168.21.8,vers=4,soft,timeo=180,bg,tcp,rw"' \
alpine sleep 3600
[root@master01 nfs]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
1ny0644yl9wr nfs replicated 2/2 alpine:latest
[root@master01 nfs]#
[root@node01 _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4ef435a0d94 alpine:latest "sleep 3600" About a minute ago Up About a minute nfs.2.ke9pm6j4dclu1noh8f2jj7ntm
[root@node01 _data]#
docker service rm nfs #删除服务
3、使用docker compose方式创建数据卷
使用docker compose方式不需要在每个节点上手动创建docker volume
#编辑配置文件
vi /nfs/data/docker-compose/conf/volume-nfs-wordpress.yaml
version: '3'
services:
#容器名称
mysql:
image: hub.osyunwei.com/mysql:v8.0.26
networks:
- mycust-overlay
volumes:
- volume-nfs-mysql:/var/lib/mysql
- /nfs/data/mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8 --collation-server=utf8_general_ci
environment:
- MYSQL_ROOT_PASSWORD=123456
restart: always
deploy:
replicas: 1
#设置优先启动mysql
depends_on:
- mysql
#容器名称
php:
image: hub.osyunwei.com/php-fpm:8.0.11
networks:
- mycust-overlay
volumes:
- volume-nfs-nginx:/data/web
- /nfs/data/php/etc:/usr/local/php/etc
restart: always
deploy:
replicas: 1
#容器名称
nginx:
#容器所使用的镜像名称
image: hub.osyunwei.com/nginx:1.20.1
networks:
- mycust-overlay
#设置端口映射(宿主机端口:容器内端口)
ports:
- 80:80
volumes:
- volume-nfs-nginx:/data/web
- /docker/data/nginx/conf/ssl:/usr/local/nginx/conf/ssl
- /nfs/data/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf
- /nfs/data/nginx/conf/vhost:/usr/local/nginx/conf/vhost.conf
- /nfs/data/nginx/conf/vhost:/usr/local/nginx/conf/vhost
restart: always
deploy:
replicas: 1
volumes:
volume-nfs-mysql:
driver: local
driver_opts:
type: "nfs4"
o: "addr=192.168.21.8,rw"
device: ":/nfs/data/mysql/database"
volume-nfs-nginx:
driver: local
driver_opts:
type: "nfs4"
o: "addr=192.168.21.8,rw"
device: ":/nfs/data/web"
networks:
mycust-overlay:
external: true
:wq! #保存退出
#yaml文件格式有前后缩进和空格,一定要注意
#需要认证的私有仓库必须加 --with-registry-auth 才能下载镜像,部署成功
cd /nfs/data/docker-compose/conf
#执行部署命令
docker stack deploy -c volume-nfs-wordpress.yaml --with-registry-auth wordpress
#注意:
多个数据卷必须配置在一个volumes标签下
docker stack ls
#移除服务命令
docker stack down wordpress
至此,Docker Swarm集群使用NFS共享存储教程完成。