kubernetes基础之dokcer数据持久化
  zZHnZavbRDNq 2023年11月02日 35 0


dokcer的数据持久化

  • docker提供三种方式将数据从宿主机挂载到容器中

kubernetes基础之dokcer数据持久化_docker


开搞

volumes
[root@localhost ~]# docker ps -a    #确认下当前有几个容器
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS                    PORTS     NAMES
be9a8ab117d1   nginx     "/docker-entrypoint.…"   12 hours ago   Exited (0) 11 hours ago             web2
afb553ce70c8   nginx     "/docker-entrypoint.…"   12 hours ago   Exited (0) 11 hours ago             web1

[root@localhost ~]# docker rm $(docker ps -aq)  ##全部移除掉
be9a8ab117d1
afb553ce70c8

[root@localhost ~]# docker volume --help   #查看volume命令帮助
Usage:  docker volume COMMAND
Manage volumes
Commands:
  create      Create a volume   #创建一个volume
  inspect     Display detailed information on one or more volumes   #展现volume得详细信息
  ls          List volumes  #列出volume
  prune       Remove all unused local volumes   #移除不在使用得volume
  rm          Remove one or more volumes  #移除一个或者多个volume
Run 'docker volume COMMAND --help' for more information on a command.

[root@localhost ~]# docker volume ls  #查看当前有几个volume,很显然是没有得。
DRIVER    VOLUME NAME

[root@localhost ~]# docker volume create web-vol   #创建一个叫web-vol得volume
web-vol
[root@localhost ~]# docker volume ls   #查看volume,发现有了刚刚创建的饿那个一个
DRIVER    VOLUME NAME
local     web-vol
[root@localhost ~]# docker run -d -v web-vol:/usr/share/nginx/html --name=web1 nginx   #-v 将其挂载到新创建得容器,位置是nginx得网页目录位置
WARNING: IPv4 forwarding is disabled. Networking will not work.
fa7a8670d01e01e772abe3e256d0e3d2d309fceecc0e31763d1f33fb83da310b
[root@localhost ~]# docker ps   #查看新创建得容器运行状况
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
fa7a8670d01e   nginx     "/docker-entrypoint.…"   34 seconds ago   Up 31 seconds   80/tcp    web1
[root@localhost ~]# docker volume inspect web-vol   #查看volume得详细信息
[
    {
        "CreatedAt": "2021-03-05T11:48:27+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/web-vol/_data",   #可以看到这个就是新创建得volume目录
        "Name": "web-vol",
        "Options": {},
        "Scope": "local"
    }
]
[root@localhost ~]# ls /var/lib/docker/volumes/web-vol/_data   #可以很清楚得看见里面有nginx得网页文件
50x.html  index.html


[root@localhost ~]# docker inspect fa7a8670d01e | grep -A 20 -i ipaddres   #查看容器得ip地址
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "60f8343e40730166aa477b145474418597823acf100004b9e160adcf231c3306",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2cbcdbf497dd696ef1ea69839b941ddedc6aa6ee851d963d16282926d69f6da0",
                    "EndpointID": "60f8343e40730166aa477b145474418597823acf100004b9e160adcf231c3306",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",   #发现是172.17.0.1
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]



[root@localhost ~]# curl 172.17.0.2   #访问一下,成功访问
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]# echo "hell0 word " > /var/lib/docker/volumes/web-vol/_data/index.html    #在web-vol里面更改首页文件得内容
[root@localhost ~]# curl 172.17.0.2   #发现网站首页也是跟着变了
hell0 word 
###上面得内容,是我们在创建了volume之后操作得,那么如果我们不创建volume,直接挂载呢,docker会自动创建volume嘛,答案是会的


[root@localhost ~]# docker run -d -v web-vol2:/usr/share/nginx/html --name=web2 nginx   ##未创建volume,直接挂载
WARNING: IPv4 forwarding is disabled. Networking will not work.
7c81459329f0428c29bcbcdfc2ffc41eac7954af125b78b62116187091e485ac
[root@localhost ~]# docker volume ls   #docker会自动创建的
DRIVER    VOLUME NAME
local     web-vol
local     web-vol2


##接下来我们尝试一下,如果删除了容器,那么volume是否还会存在
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
7c81459329f0   nginx     "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp    web2
fa7a8670d01e   nginx     "/docker-entrypoint.…"   9 minutes ago        Up 9 minutes        80/tcp    web1
[root@localhost ~]# docker stop $(docker ps -aq)    ##停止所有的容器
7c81459329f0
fa7a8670d01e
[root@localhost ~]# docker rm $(docker ps -aq)  ##移除所有的容器
7c81459329f0
fa7a8670d01e
[root@localhost ~]# docker ps -a   #当前没有容器了哈
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker volume ls   #可以看到volume依然还在
DRIVER    VOLUME NAME
local     web-vol
local     web-vol2
[root@localhost ~]# cat  /var/lib/docker/volumes/web-vol/_data/index.html   ##里面的内容依旧还有,是不是就达到了持久化了
hell0 word 

###接下来,我们新建一个容器,直接挂载之前的那个volume
[root@localhost ~]# docker run -d -v web-vol:/usr/share/nginx/html --name=web3 nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
de1405111150972e226c6af3a2ba7fcecfef16c1efffcbfc8b35a0f83cd30216
[root@localhost ~]# docker inspect web3 | grep -A 20 -i ipaddres
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "6ebee48b42ec57de3ebf2a2e7f0ec635676f777ac182ff74338d03b34f68e762",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2cbcdbf497dd696ef1ea69839b941ddedc6aa6ee851d963d16282926d69f6da0",
                    "EndpointID": "6ebee48b42ec57de3ebf2a2e7f0ec635676f777ac182ff74338d03b34f68e762",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@localhost ~]# curl 172.17.0.2   #发现,新的容器挂载之前的那个volume也是可以的
hell0 word 
[root@localhost ~]#
bind mounts
[root@localhost ~]# mkdir vol   #创建一个待挂载目录

[root@localhost ~]# docker run -d -v $PWD/vol:/usr/share/nginx/html --name=web4 nginx   #创建一个新的容器,挂载我们刚刚的那个目录
WARNING: IPv4 forwarding is disabled. Networking will not work.
ddde61f54bfcdd442e6703a5378675be69d53c4b3919f64da5f2b45da087559d
[root@localhost ~]# docker inspect web4 | grep -i ipad -A 20
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f2b7274f7c22261d0cce8b30d515da2627e3cdb4098f25f4bc68a47dc41e8802",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2cbcdbf497dd696ef1ea69839b941ddedc6aa6ee851d963d16282926d69f6da0",
                    "EndpointID": "f2b7274f7c22261d0cce8b30d515da2627e3cdb4098f25f4bc68a47dc41e8802",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@localhost ~]# curl 172.17.0.2   #访问403,应为我们的目录里面是空的
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.6</center>
</body>
</html>

[root@localhost ~]# echo 123 > vol/index.html   #往目录里面写个index.html文件
[root@localhost ~]# curl 172.17.0.2   #发现可以正常的访问,说明这个挂载也是有效的
123

###接下来,我们尝试一下不创建目录,直接挂载一个不存在的目录,看看docker是否会帮我们创建
[root@localhost ~]# ls
nginx.tar  vol  web1.txt  web2.txt
[root@localhost ~]# docker run -d -v $PWD/web:/usr/share/nginx/html --name=web5 nginx   #挂载一个不存在的目录
WARNING: IPv4 forwarding is disabled. Networking will not work.
8fda22d309a755e53de2baa9dc0fd7d1e0f7cc5cd3a7940b3176e9e9b3befa84
[root@localhost ~]# ls
nginx.tar  vol  web  web1.txt  web2.txt   #发现docker会帮我们创建所需要的目录
[root@localhost ~]# ls web  #里面是空的
[root@localhost ~]# docker inspect web5 | grep -i ipad -A 20
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2bcfeb6e07789c543b9c89aa8edf8aa092e0469b66f78b71593303921b3eaa5d",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2cbcdbf497dd696ef1ea69839b941ddedc6aa6ee851d963d16282926d69f6da0",
                    "EndpointID": "2bcfeb6e07789c543b9c89aa8edf8aa092e0469b66f78b71593303921b3eaa5d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",   #得到容器的ip地址
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@localhost ~]# curl 172.17.0.3  #访问403
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.6</center>
</body>
</html>
[root@localhost ~]# echo 456 > web/index.html   #创建首页文件
[root@localhost ~]# curl 172.17.0.3   #就可以发现能正常访问了
456

结束

大家可以总结一下volume和bind mounts有什么区别,谢谢大家


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
zZHnZavbRDNq