华为云之云原生基础设施之容器存储管理【玩转华为云】
一、本次实践介绍
- 本次实践目的
1.掌握容器bind mount存储方案
2.掌握容器managed volume存储方案
3.掌握容器volume container存储方案
二、购买弹性云服务器ECS
1.进ECS管理页面
登录华为云,服务列表——弹性云服务器ECS,进入ECS管理页面。
二、购买ECS弹性云服务器
选择购买弹性云服务器:
基础配置:
区域:华为——北京四;
计费模式:按需计费;
可用区:可用区1;
cpu架构:x86
规格:通用计算型,s6.large.2 ;
镜像:公共镜像,centos7.6版本;
其余配置,默认即可。
网络配置:
VPC:选择默认即可;
安全组:选择默认即可;
弹性公网IP:现在购买;
线路:全动态BGP;
公网宽带:按带宽计费;
带宽大小: 5 ;
高级配置:
主要配置:设置账号密码和ECS服务器名称,其余默认即可。
3.查看ECS状态
查看刚购买ECS状态。
三、安装docker环境
1.连接ECS云服务器
双击桌面的“Xfce终端”打开Terminal,输入以下命令登录弹性云服务器。
ssh root@EIP
2.安装docker
安装相关系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
配置docker的yum仓库
[root@ecs-3380 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@ecs-3380 ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
[root@ecs-3380 ~]#
检查docker仓库状态
[root@ecs-3380 ~]# yum repolist all |grep enable
base/7/x86_64 CentOS-7 - Base enabled: 10,072
docker-ce-stable/7/x86_64 Docker CE Stable - x86_64 enabled: 210
epel/x86_64 Extra Packages for Enterpri enabled: 13,768
extras/7/x86_64 CentOS-7 - Extras enabled: 515
updates/7/x86_64 CentOS-7 - Updates enabled: 4,839
安装docker
yum -y install docker-ce
配置镜像加速
[root@ecs-3380 ~]# cat /etc/docker/daemon.josn
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn"
]
}
启动docker服务
[root@ecs-3380 ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
检查docker状态
[root@ecs-3380 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-03-15 13:41:10 CST; 28s ago
Docs: https://docs.docker.com
Main PID: 8545 (dockerd)
Tasks: 8
Memory: 29.2M
CGroup: /system.slice/docker.se
四、运行web01测试容器
1.查看docker信息
检查docker配置详细信息
[root@ecs-3380 ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.10.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.16.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
scan: Docker Scan (Docker Inc.)
Version: v0.23.0
Path: /usr/libexec/docker/cli-plugins/docker-scan
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 23.0.1
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-1160.53.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.7GiB
Name: ecs-3380
ID: 415094c4-e869-4ce5-af6f-ef2634b8df95
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
2.检查docker版本
检查docker版本
[root@ecs-3380 ~]# docker version
Client: Docker Engine - Community
Version: 23.0.1
API version: 1.42
Go version: go1.19.5
Git commit: a5ee5b1
Built: Thu Feb 9 19:51:00 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 23.0.1
API version: 1.42 (minimum version 1.12)
Go version: go1.19.5
Git commit: bc3805a
Built: Thu Feb 9 19:48:42 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.18
GitCommit: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
3.创建一个测试容器web01
创建一个挂载目录
[root@ecs-3380 ~]# mkdir -p /data/test/mydata
[root@ecs-3380 ~]# cd /data/test/mydata/
[root@ecs-3380 mydata]#
运行容器,并将宿主机上的目录挂载到容器
[root@ecs-3380 mydata]# docker run -d --name web01 --restart always -p 80:80 -v /data/test/mydata/:/usr/local/apache2/htdocs/ httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
3f9582a2cbe7: Pull complete
9423d69c3be7: Pull complete
d1f584c02b5d: Pull complete
758a20a64707: Pull complete
08507f82f391: Pull complete
Digest: sha256:76618ddd53f315a1436a56dc84ad57032e1b2123f2f6489ce9c575c4b280c4f4
Status: Downloaded newer image for httpd:latest
8ad9cbc43fa7c051e6a866401fd6c4c9ddf7d1351d58c97c9b9bcdd2fd560925
4.查询容器的挂载信息
使用docker inspect命令查询容器元数据可以获取mount具体信息。
[root@ecs-3380 mydata]# docker inspect web01 |grep -i -A10 mount
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/data/test/mydata/:/usr/local/apache2/htdocs/"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
--
"Mounts": [
{
"Type": "bind",
"Source": "/data/test/mydata",
"Destination": "/usr/local/apache2/htdocs",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
[root@ecs-3380 mydata]#
5.宿主机目录内更新文件,验证容器内读取。
宿主机目录内更新文件,验证容器内读取。
[root@ecs-3380 mydata]# echo "test page" > index.html
[root@ecs-3380 mydata]# ls
index.html
进入容器内,查看挂载目录的文件。
[root@ecs-3380 mydata]# docker exec -it web01 /bin/bash
root@8ad9cbc43fa7:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@8ad9cbc43fa7:/usr/local/apache2# ls htdocs/index.html
htdocs/index.html
root@8ad9cbc43fa7:/usr/local/apache2# cat htdocs/index.html
test page
五、测试容器的数据持久化
1.停止并删除测试容器web01
停止并删除测试容器web01
[root@ecs-3380 mydata]# docker stop web01
web01
[root@ecs-3380 mydata]# docker rm web01
web01
2.检查宿主机挂载目录文件
检查宿主机挂载的目录文件,发现容器删除后,容器内的文件数据还保留在挂载目录中。
[root@ecs-3380 mydata]# ls
index.html
[root@ecs-3380 mydata]# cat index.html
test page
[root@ecs-3380 mydata]#
六、容器的数据卷管理
1.创建测试容器web02
重新创建一个测试容器web02
[root@ecs-3380 ~]# docker run -d --name web02 -p 82:80 -v /usr/local/apache2/htdocs/ httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
3f9582a2cbe7: Pull complete
9423d69c3be7: Pull complete
d1f584c02b5d: Pull complete
758a20a64707: Pull complete
08507f82f391: Pull complete
Digest: sha256:76618ddd53f315a1436a56dc84ad57032e1b2123f2f6489ce9c575c4b280c4f4
Status: Downloaded newer image for httpd:latest
bc9a95026a445f61fa3b8d8b2f9f806eaedaf1939e0a14be84bf412efce1ddb5
2.查看容器默认挂载的数据卷
查看容器默认挂载的数据卷
[root@ecs-3380 ~]# docker inspect web02 |grep Mounts -A5
"Mounts": [
{
"Type": "volume",
"Name": "41f7f401c5455628a2fa5285a23baff3d317513a3baa16bdae3e31ad58651c61",
"Source": "/var/lib/docker/volumes/41f7f401c5455628a2fa5285a23baff3d317513a3baa16bdae3e31ad58651c61/_data",
"Destination": "/usr/local/apache2/htdocs",
3.进入容器内编辑index.html文件
进入容器内编辑index.html文件
root@bc9a95026a44:/usr/local/apache2# cd htdocs/
root@bc9a95026a44:/usr/local/apache2/htdocs# echo "test for web02" > index.html
root@bc9a95026a44:/usr/local/apache2/htdocs#
4.宿主机访问容器服务
宿主机访问容器服务
[root@ecs-3380 ~]# curl 127.0.0.1:82
test for web02
5.检查宿主机数据卷的文件
[root@ecs-3380 ~]# docker inspect web02 |grep Mounts -A5
"Mounts": [
{
"Type": "volume",
"Name": "41f7f401c5455628a2fa5285a23baff3d317513a3baa16bdae3e31ad58651c61",
"Source": "/var/lib/docker/volumes/41f7f401c5455628a2fa5285a23baff3d317513a3baa16bdae3e31ad58651c61/_data",
"Destination": "/usr/local/apache2/htdocs",
[root@ecs-3380 ~]# cd /var/lib/docker/volumes/41f7f401c5455628a2fa5285a23baff3d317513a3baa16bdae3e31ad58651c61/_data
[root@ecs-3380 _data]# ls
index.html
[root@ecs-3380 _data]# cat index.html
test for web02
6.删除容器,验证数据持久化
删除容器,验证数据持久化,删除容器后,挂载数据卷中文件保存完整。
[root@ecs-3380 _data]# docker stop web02
web02
[root@ecs-3380 _data]# docker rm web02
web02
[root@ecs-3380 _data]# pwd
/var/lib/docker/volumes/41f7f401c5455628a2fa5285a23baff3d317513a3baa16bdae3e31ad58651c61/_data
[root@ecs-3380 _data]# ls
index.html
[root@ecs-3380 _data]# cat index.html
test for web02
七、测试容器共享数据卷
1.创建一个数据卷容器
创建一个test03的数据卷容器
[root@ecs-3380 ~]# docker create --name test03 -v /data/test03/htdocs:/usr/local/apache2/htdocs -v /other/tools/ busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
1487bff95222: Pull complete
Digest: sha256:c118f538365369207c12e5794c3cbfb7b042d950af590ae6c287ede74f29b7d4
Status: Downloaded newer image for busybox:latest
2dd786ac6b319e2b334a77d62ebab117effc5f33c5aeb2476ac2955ca746ffaa
2.创建一个test04测试容器
创建一个test04测试容器,将test03容器的数据卷关联挂载到test04上。
[root@ecs-3380 ~]# docker run --name test04 -td -p 86:80 --volumes-from test03 busybox
7917a4bbaa8b6b0610810ec67c6556fbd56e02026b24fc7684997a9f4f5adbff
3.分别查看test03和test04的数据挂载信息
test03容器的挂载详情
[root@ecs-3380 ~]# docker inspect test03 |grep Mounts -A16
"Mounts": [
{
"Type": "volume",
"Name": "399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3",
"Source": "/var/lib/docker/volumes/399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3/_data",
"Destination": "/other/tools",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "bind",
"Source": "/data/test03/htdocs",
"Destination": "/usr/local/apache2/htdocs",
"Mode": "",
"RW": true,
test04容器的挂载详情
[root@ecs-3380 ~]# docker inspect test04 |grep Mounts -A16
"Mounts": [
{
"Type": "bind",
"Source": "/data/test03/htdocs",
"Destination": "/usr/local/apache2/htdocs",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "volume",
"Name": "399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3",
"Source": "/var/lib/docker/volumes/399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3/_data",
"Destination": "/other/tools",
"Driver": "local",
"Mode": "",
"RW": true,
4.验证容器数据共享
在宿主机test03的挂载目录上编辑测试文件
[root@ecs-3380 ~]# cd /data/test03/htdocs/
[root@ecs-3380 htdocs]# echo "web3 for docker" >index.html
[root@ecs-3380 htdocs]# cat index.html
web3 for docker
在测试容器test04上查看文件
[root@ecs-3380 htdocs]# docker exec -it test04 /bin/sh
/ #
/ # cat /usr/local/apache2/htdocs/index.html
web3 for docker
/ #
在测试容器test04上编辑测试文件,然后到宿主机的挂载源路径检查对应文件。
/ # cd /other/tools/
/other/tools # echo "test04 for docker" > index.html
/other/tools #
[root@ecs-3380 htdocs]# docker inspect test04 |grep Mounts -A16
"Mounts": [
{
"Type": "bind",
"Source": "/data/test03/htdocs",
"Destination": "/usr/local/apache2/htdocs",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "volume",
"Name": "399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3",
"Source": "/var/lib/docker/volumes/399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3/_data",
"Destination": "/other/tools",
"Driver": "local",
"Mode": "",
"RW": true,
[root@ecs-3380 htdocs]# cd /var/lib/docker/volumes/399146af9183dd028e14f9cab602eb1087f5ed6c40c19b96d198426b27f6b1c3/_data
[root@ecs-3380 _data]# ls
index.html
[root@ecs-3380 _data]# cat index.html
test04 for docker
- 点赞
- 收藏
- 关注作者
评论(0)