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

成功!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101958.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号