Docker中Web集群迁移及共享数据

简介: Docker中Web集群迁移及共享数据

实验题

在这里插入图片描述

实验环境

ip 服务
192.168.2.66 dockerA
192.178.2.67 dockerB
192.168.2.11 NFS&Docker Registry

实验步骤

DockerA中搭建web集群
创建热数据持久化目录

[root@localhost ~]# mkdir hy
[root@localhost ~]# echo "hy.com" > hy/index.html
[root@localhost ~]# 

运行集群容器

[root@localhost ~]# docker run -d -p 80 --name hy --volume /root/hy/:/usr/local/apache2/htdocs httpd:latest
0e5c87513337406d79c7afe8d549ada86f5c9f46bf5922ed6daa77a4d2a73093
[root@localhost ~]# docker run -d -p 80 --name hy1 --volume /root/hy/:/usr/local/apache2/htdocs httpd:latest
73128e4462630b4507ba0349b401baf5524c1721729c5f0bb51b5ae51cdc6a56
[root@localhost ~]# docker run -d -p 80 --name hy2 --volume /root/hy/:/usr/local/apache2/htdocs httpd:latest
80229b37611a7c7bda6be90ce0a0c200f5bcee9427e2d53075ed900f8377d374

查看三台apache容器的80端口在物理机的映射端口

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS              PORTS                   NAMES
80229b37611a        httpd:latest        "httpd-foreground"   About a minute ago   Up About a minute   0.0.0.0:32781->80/tcp   hy2
73128e446263        httpd:latest        "httpd-foreground"   About a minute ago   Up About a minute   0.0.0.0:32780->80/tcp   hy1
0e5c87513337        httpd:latest        "httpd-foreground"   About a minute ago   Up About a minute   0.0.0.0:32779->80/tcp   hy

用物理机ip访问这些端口,来验证物理机的cyj目录是否被每台容器的htdocs挂载使用

[root@localhost ~]# curl 192.168.2.66:32781
hy.com
[root@localhost ~]# curl 192.168.2.66:32780
hy.com
[root@localhost ~]# curl 192.168.2.66:32779
hy.com

迁移DockerA中的集群以及热数据到DockerB
设置私库
迁移时需要用到Registry私有仓库,所以需要现在NFS服务器中pull私库镜像

[root@localhost yum.repos.d]# docker pull registry:2
2: Pulling from library/registry
486039affc0a: Pull complete 
ba51a3b098e6: Pull complete 
8bb4c43d6c8e: Pull complete 
6f5f453e5f2d: Pull complete 
42bc10b72f42: Pull complete 
Digest: sha256:7d081088e4bfd632a88e3f3bcd9e007ef44a796fddfe3261407a3f9f04abe1e7
Status: Downloaded newer image for registry:2
docker.io/library/registry:2

将私库在后台运行,并将5000端口映射到物理机1.13的5000端口

[root@localhost yum.repos.d]# docker run -itd -p 5000:5000 --restart always --volume /opt/data/registry/:/var/lib/registry registry:2
2d790d31bc2132b1ef38af5926d9bb81b139b54fd97bdf36010b1cf2be2af708

在三台服务器中指定私库地址

NFS&Registry(192.168.2.11)

[root@localhost yum.repos.d]# vim /usr/lib/systemd/system/docker.service
[root@localhost yum.repos.d]# systemctl daemon-reload 
[root@localhost yum.repos.d]# systemctl restart docker
#修改内容
14 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.2.11:5000

DockerA

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service 
[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker

#修改内容
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.2.11:5000

DockerB

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service 
[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker

#修改内容
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.2.11:5000

构建带有热数据的镜像
在DockerA中编写Dockerfile

[root@localhost ~]# vim Dockerfile 

FROM busybox:latest
ADD hy /usr/local/apache2/htdocs
VOLUME /usr/local/apache2/htdocs

构建镜像

[root@localhost ~]# docker build -t httpd_cluster .
Sending build context to Docker daemon  84.11MBB
Step 1/3 : FROM busybox:latest
 ---> be5888e67be6
Step 2/3 : ADD hy /usr/local/apache2/htdocs
 ---> 17931e0e9e6f
Step 3/3 : VOLUME /usr/local/apache2/htdocs
 ---> Running in 6b278ed07c5c
Removing intermediate container 6b278ed07c5c
 ---> 8b0d6a73ca37
Successfully built 8b0d6a73ca37
Successfully tagged httpd_cluster:latest

上传镜像到私库中

[root@localhost ~]# docker tag httpd_cluster 192.168.2.11:5000/httpd_cluster
[root@localhost ~]# docker push 192.168.2.11:5000/httpd_cluster
The push refers to repository [192.168.2.11:5000/httpd_cluster]
9e9bf87f3138: Pushed 
5b0d2d635df8: Pushed 
latest: digest: sha256:b22a53ad7941ebfa6ef39a77cd29fa49a23d0e150caa774d21c6da6215a2c811 size: 734
[root@localhost ~]# 

验证是否上传成功

[root@localhost ~]# curl 192.168.2.11:5000/v2/_catalog
{"repositories":["httpd_cluster"]}

DockerB下载镜像

[root@localhost ~]# docker pull 192.168.2.11:5000/httpd_cluster
Using default tag: latest
latest: Pulling from httpd_cluster
e2334dd9fee4: Already exists 
e4588d532789: Pull complete 
Digest: sha256:b22a53ad7941ebfa6ef39a77cd29fa49a23d0e150caa774d21c6da6215a2c811
Status: Downloaded newer image for 192.168.2.11:5000/httpd_cluster:latest
192.168.2.11:5000/httpd_cluster:latest

DockerB构建与DockerA访问数据相同的集群
更改标签方便使用

[root@localhost ~]# docker tag 192.168.2.11:5000/httpd_cluster:latest  httpd:cluster
[root@localhost ~]# docker rmi 192.168.2.11:5000/httpd_cluster:latest 
Untagged: 192.168.2.11:5000/httpd_cluster:latest
Untagged: 192.168.2.11:5000/httpd_cluster@sha256:b22a53ad7941ebfa6ef39a77cd29fa49a23d0e150caa774d21c6da6215a2c811
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
httpd                     cluster             8b0d6a73ca37        3 minutes ago       1.22MB

使用httpd:cluster创建容器,但不运行,作为volume使用

[root@localhost ~]# docker create --name dockerweb httpd:latest
eac6c6f2ba6f99fee15d5cb34ca8a06b01689cf31a01b7eb55dd5e66b3f86dd5

使用dockerbweb运行容器,搭建集群

[root@localhost ~]# docker run -d -p 80 --name hy3 --volumes-from dockerweb httpd:cluster
3c5ae82cbabd82606e1eca3eb1117cb76a4c7d7f4cf4d890261eefbe64a8a872
[root@localhost ~]# docker run -d -p 80 --name hy4 --volumes-from dockerweb httpd:cluster
b94793a48fe57af7303fb5872ccdff9538b7ea2641cd95ed4356949f2fe4a9ae
[root@localhost ~]# docker run -d -p 80 --name hy5 --volumes-from dockerweb httpd:cluster
f258c7effef67e87888be7cac611c045dadca804e4ee76ea87e4b70951f11363

查看三个web容器在主机物理机2.67映射的端口

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
793f18ba350b        httpd               "httpd-foreground"       4 seconds ago       Up 3 seconds        0.0.0.0:32771->80/tcp    hy5
c447ef63e115        httpd               "httpd-foreground"       10 seconds ago      Up 9 seconds        0.0.0.0:32770->80/tcp    hy4
38b7ade3308c        httpd               "httpd-foreground"       19 seconds ago      Up 17 seconds       0.0.0.0:32769->80/tcp    hy3

访问验证与DockerA主机集群中的页面是否一致

[root@localhost ~]# curl 192.168.2.67:32769
hy.com
[root@localhost ~]# curl 192.168.2.67:32770
hy.com
[root@localhost ~]# curl 192.168.2.67:32771
hy.com

DockerA新建集群,使用NFS共享数据
NFS&Registry

安装NFS

[root@localhost ~]# yum -y install nfs-utils rpcbind

创建共享目录

[root@localhost ~]# mkdir /dockera

将目录共享到所有网段

[root@localhost ~]# vim /etc/exports
/dockera *(rw,no_root_squash,sync)
[root@localhost ~]# exportfs -r

启动nfs服务

[root@localhost ~]# systemctl start rpcbind nfs-server
[root@localhost ~]# systemctl enable rpcbind nfs-server

清空防火墙策略

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables-save

DockerA

查看是否可以获取到NFS的共享目录

[root@localhost ~]# showmount -e 192.168.2.11
Export list for 192.168.2.11:
/dockera *

使用docker创建驱动类型为nfs的磁盘,并指定nfs的ip和目录

[root@localhost ~]# docker volume create --driver local --opt type=nfs --opt o=addr=192.168.2.11,rw --opt device=:/dockera --name dockera-nfs
dockera-nfs

查看创建好的volume

[root@localhost ~]# docker volume ls
DRIVER              VOLUME NAME

local               volume-nfs

使用dockera-nfs卷来启动httpd镜像的web集群

[root@localhost ~]# docker run -d -p 80 --name hy6 --volume dockera-nfs:/usr/local/apache2/htdocs httpd
1ee478d8464ba8eb631d8fc3b205fca2c2d70b8f2f6f428130004b3b6a44674f
[root@localhost ~]# docker run -d -p 80 --name hy7 --volume dockera-nfs:/usr/local/apache2/htdocs httpd
eb0025df59b3e2e77018ffea130f19d25ea680af6929d5236f292587a682d855
[root@localhost ~]# docker run -d -p 80 --name hy8 --volume dockera-nfs:/usr/local/apache2/htdocs httpd
b93d70e8819dc8f4964d76e0278d25cf29c13073e293aec83be32f4ff739fe35

查看三个集群容器在主机的映射端口

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
b93d70e8819d        httpd               "httpd-foreground"   26 seconds ago      Up 24 seconds       0.0.0.0:1027->80/tcp   hy8
eb0025df59b3        httpd               "httpd-foreground"   32 seconds ago      Up 31 seconds       0.0.0.0:1026->80/tcp   hy7
1ee478d8464b        httpd               "httpd-foreground"   40 seconds ago      Up 38 seconds       0.0.0.0:1025->80/tcp   hy6

访问这几个映射端口,查看得到的页面内容

[root@localhost ~]# curl 192.168.2.66:1025
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 192.168.2.66:1026
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 192.168.2.66:1027
<html><body><h1>It works!</h1></body></html>

发现都是It works!这是因为我们的NFS共享目录中的内容是空的
NFS&Registry

[root@localhost yum.repos.d]# echo "2020ComeOnWH" > /dockera/index.html 

再次访问验证

[root@localhost yum.repos.d]# curl 192.168.2.66:1025
2020ComeOnWH
[root@localhost yum.repos.d]# curl 192.168.2.66:1026
2020ComeOnWH
[root@localhost yum.repos.d]# curl 192.168.2.66:1027
2020ComeOnWH

成功!

相关文章
|
3月前
|
存储 监控 测试技术
如何将现有的应用程序迁移到Docker容器中?
如何将现有的应用程序迁移到Docker容器中?
304 57
|
9天前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
129 5
|
3月前
|
存储 监控 Java
如何对迁移到Docker容器中的应用进行性能优化?
如何对迁移到Docker容器中的应用进行性能优化?
273 59
|
5月前
|
Prometheus 监控 Cloud Native
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
433 79
|
8月前
|
消息中间件 监控 RocketMQ
Docker部署RocketMQ5.2.0集群
本文详细介绍了如何使用Docker和Docker Compose部署RocketMQ 5.2.0集群。通过创建配置文件、启动集群和验证容器状态,您可以快速搭建起一个RocketMQ集群环境。希望本文能够帮助您更好地理解和应用RocketMQ,提高消息中间件的部署和管理效率。
1081 91
|
8月前
|
存储 缓存 Docker
docker: No space left on device处理与迁移目录
通过以上方法,可以有效地管理 Docker 的磁盘使用情况,并确保 Docker 运行环境的稳定性和效率。
720 34
|
8月前
|
中间件 关系型数据库 数据库
docker快速部署OS web中间件 数据库 编程应用
通过Docker,可以轻松地部署操作系统、Web中间件、数据库和编程应用。本文详细介绍了使用Docker部署这些组件的基本步骤和命令,展示了如何通过Docker Compose编排多容器应用。希望本文能帮助开发者更高效地使用Docker进行应用部署和管理。
224 19
|
7月前
|
运维 网络安全 文件存储
找不到类似 Docker Desktop 的 Web 管理界面?试试这些开源方案
Docker Desktop 是本地容器化开发的利器,但存在无法通过 Web 远程管理、跨平台体验不一致等问题。为此,推荐几款轻量级、可 Web 化管理的 Docker 工具:Portainer 功能全面,适合企业级运维;CasaOS 集成应用商店和 NAS 功能,适合家庭/个人开发环境;Websoft9 提供预集成环境,新手友好。这些工具能有效提升容器管理效率,满足不同场景需求。
366 3
|
8月前
|
监控 Linux PHP
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
216 20
下一篇
oss教程