书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》
一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:CKA备考实验 | 汇总_热爱编程的通信人的博客-CSDN博客
前面我们创建容器所需要的镜像都是从网络下载的,但是生产服务器很多是不能连接到外网的,那么此时该如何使用镜像呢?
这个时候,我们可以在自己的内网搭建一个私有仓库,把需要的镜像上传到私有仓库,这样其他主机需要镜像的话,就可以从私有仓库自行下载了。
要想配置私有仓库的话,有两种方法。
(1)使用registry镜像。
(2)利用harbor。
本章暂且先讲registry配置私有仓库,后面会单独讲解如何使用harbor配置私有仓库。
先查看下面的拓扑图,如图2-3所示。
这里把vms101作为私有仓库,在vms101上用镜像registry创建一个容器出来,这个容器就作为私有仓库。容器的端口是5000,为了让外部其他主机能访问到此容器,所以创建这个registry容器的时候,需要映射到物理机的5000端口。后面通过192.168.26.101:5000即可访问到此容器。
在vms101上安装并启动docker-ce,自行下载镜像registry。
##########实操验证##########
[root@vms101 ~]# docker pull hub.c.163.com/library/registry:latest
latest: Pulling from library/registry
25728a036091: Pull complete
0da5d1919042: Pull complete
e27a85fd6357: Pull complete
d9253dc430fe: Pull complete
916886b856db: Pull complete
Digest: sha256:fce8e7e1569d2f9193f75e9b42efb07a7557fc1e9d2c7154b23da591e324f3d1
Status: Downloaded newer image for hub.c.163.com/library/registry:latest
hub.c.163.com/library/registry:latest
[root@vms101 ~]#
在vms101上配置。
步骤1:设置docker可以通过http的方式访问,有两种方法,二选一即可。
(1)修改
/usr/lib/systemd/system/docker.service,在ExecStart后面添加:
##########实操验证##########
ExecStart=/usr/bin/dockerd --insecure-registry=192.168.26.101:5000 -H fd:// --containerd=/run/containerd/containerd.sock
然后重启docker。
##########实操验证##########
[root@vms101 ~]# systemctl daemon-reload ; systemctl restart docker
[root@vms101 ~]#
(2)或者修改/etc/docker/daemon.json,内容如下。
[root@vms101 ~]# cat/etc/docker/daemon.json
{
"insecure-registries": ["192.168.26.101:5000"]
}
[root@vms101 ~]#
注意:如果配置了加速器的话,或者说{}里有多行值的话,除了最后一行之外,每行都要逗号结束,比如这样:
{
"registry-mirrors": ["https://frz7i079.mirror.aliyuncs.com"], -->这里有逗号
"insecure-registries": ["192.168.26.101:5000"]
}
步骤2:重启docker。
[root@vms101 ~]# systemctl restart docker
[root@vms101 ~]#
步骤3:利用registry镜像创建一个容器。
##########实操验证##########
[root@vms101 ~]# docker run -d --name myreg -p 5000:5000 --restart=always -v /myreg:/var/lib/registry hub.c.163.com/library/registry
c8ac11dbf60bb13a752684efd67a9bc869cb5a257b3fb01b282318b1eec4c998
[root@vms101 ~]#
此时私有仓库就配置好了。这里把容器的端口映射到物理机的5000端口,上传的镜像都是在容器的/var/lib/registry目录里,为了能够保证删除容器之后上传的镜像也是存在的,这里做了一个数据卷,把容器的/var/lib/registry映射到物理机的/myreg目录。
下面在vms100上配置。
步骤4:修改相关配置,使得docker能以http方式访问,这里修改的是/etc/docker/daemon.json,内容如下。
##########实操验证##########
[root@vms100 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://frz7i079.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.26.101:5000"]
}
[root@vms100 ~]#
步骤5:重启docker。
##########实操验证##########
[root@vms100 ~]# systemctl restart docker
[root@vms100 ~]#
步骤6:在vms100上对要推送的镜像进行tag操作。
##########实操验证##########
[root@vms100 ~]# docker tag centos:v1 192.168.26.101:5000/cka/centos:v1
[root@vms100 ~]#
这里新的tag的服务器要指向docker仓库的地址,192.168.26.101:5000,后面的分类cka及镜像名都可以随意定义。
步骤7:把此镜像推送到docker仓库。
##########实操验证##########
[root@vms100 ~]# docker push 192.168.26.101:5000/cka/centos:v1
The push refers to repository [192.168.26.101:5000/cka/centos]
0b744a01646c: Pushed
b362758f4793: Pushed
v1: digest: sha256:0276a952e12c2cd822b2f8879ccc8d6a175d66e8fc04fa27c25eb207a7895e01 size: 741
[root@vms100 ~]#
以此类推,可以推送多个镜像,分类可以根据需要自己定义,我这里把wordpress和mysql都推送到私有仓库里了。
##########实操验证##########
[root@vms100 ~]# docker tag hub.c.163.com/library/mysql 192.168.26.101:5000/rhce8/mysql:v1
[root@vms100 ~]# docker push 192.168.26.101:5000/rhce8/mysql:v1
The push refers to repository [192.168.26.101:5000/rhce8/mysql]
8129a85b4056: Pushed
3c376267ac82: Pushed
fa9efdcb088a: Pushed
9e615ff77b4f: Pushed
e5de8ba20fae: Pushed
2bee3420217b: Pushed
904af8e2b2d5: Pushed
daf31ec3573d: Pushed
da4155a7d640: Pushed
3b7c5f5acc82: Pushed
295d6a056bfd: Pushed
v1: digest: sha256:c0806ac73235043de2a6cb4738bb2f6a74f71d9c7aa0f19c8e7530fd6c299e75 size: 2617
[root@vms100 ~]# docker tag hub.c.163.com/library/wordpress 192.168.26.101:5000/rhce8/wordpress:v2
[root@vms100 ~]# docker push 192.168.26.101:5000/rhce8/wordpress:v2
The push refers to repository [192.168.26.101:5000/rhce8/wordpress]
53e16fa1f104: Pushed
562dd11ed871: Pushed
6671cb9eb6b3: Pushed
4214911d5945: Pushed
fed26d7fe7b9: Pushed
c8c6d352c96e: Pushed
5a151fa4136e: Pushed
8747705a74d9: Pushed
adfd7c98cdf6: Pushed
354b4ef9664b: Pushed
2da371244fc1: Pushed
315db89f9acb: Pushed
3d2582fdede2: Pushed
53da01089a82: Pushed
0fd7a67d49b3: Pushed
f0c367fa8636: Pushed
ddd6dcab19ff: Pushed
2c40c66f7667: Pushed
v2: digest: sha256:ca4cf4692b7bebd81f229942c996b1c4e6907d6733e977e93d671a54b8053a22 size: 4078
[root@vms100 ~]#
步骤8:利用之前写过的rm_all_image.sh清空vms100上所有的镜像。
##########实操验证##########
[root@vms100 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@vms100 ~]#
本节练习如何从私有仓库拉取镜像,首先要查看私有仓库里有多少镜像。
步骤1:安装工具jq。
##########实操验证##########
[root@vms100 ~]# yum install jq -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package jq.x86_64 0:1.6-2.el7 will be installed
--> Processing Dependency: libonig.so.5()(64bit) for package: jq-1.6-2.el7.x86_64
--> Running transaction check
---> Package oniguruma.x86_64 0:6.8.2-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================
Installing:
jq x86_64 1.6-2.el7 epel 167 k
Installing for dependencies:
oniguruma x86_64 6.8.2-2.el7 epel 181 k
Transaction Summary
==================================================================================================================================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 348 k
Installed size: 1.0 M
Downloading packages:
(1/2): oniguruma-6.8.2-2.el7.x86_64.rpm | 181 kB 00:00:00
(2/2): jq-1.6-2.el7.x86_64.rpm | 167 kB 00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.5 MB/s | 348 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : oniguruma-6.8.2-2.el7.x86_64 1/2
warning: Failed to open SELinux handle.
Installing : jq-1.6-2.el7.x86_64 2/2
Verifying : oniguruma-6.8.2-2.el7.x86_64 1/2
Verifying : jq-1.6-2.el7.x86_64 2/2
Installed:
jq.x86_64 0:1.6-2.el7
Dependency Installed:
oniguruma.x86_64 0:6.8.2-2.el7
Complete!
[root@vms100 ~]#
步骤2:查看私有仓库所具有的仓库。
##########实操验证##########
[root@vms100 ~]# curl -s http://192.168.26.101:5000/v2/_catalog | jq
{
"repositories": [
"cka/centos",
"rhce8/mysql",
"rhce8/wordpress"
]
}
[root@vms100 ~]#
此时只看到了分类及镜像,并没有看到这些镜像的tag,如果想有某镜像具体的tag,使用如下命令。
##########实操验证##########
[root@vms100 ~]# curl http://192.168.26.101:5000/v2/cka/centos/tags/list
{"name":"cka/centos","tags":["v1"]}
[root@vms100 ~]#
可以看到cka/centos对应的tag为v1。这样完整的镜像就是
192.168.26.101:5000/cka/centos:v1。
步骤3:也可以通过写一个脚本实现列出仓库里一共有多少镜像。
##########实操验证##########
[root@vms100 ~]# cat list_img
#!/bin/bash
file=$(mktemp)
curl -s $1:5000/v2/_catalog | jq | egrep -v '\{|\}|\[|]' | awk -F\" '{print $2}' > $file
while read aa ; do
tag=($(curl -s $1:5000/v2/$aa/tags/list | jq | egrep -v '\{|\}|\[|]|name' | awk -F\" '{print $2}'))
for i in ${tag[*]} ; do
echo $1:5000/${aa}:$i
done
done < $file
rm -rf $file
[root@vms100 ~]#
步骤4:给此脚本加上一个可执行权限。
##########实操验证##########
[root@vms100 ~]# chmod +x list_img
[root@vms100 ~]#
步骤5:执行脚本列出192.168.26.101上有多少镜像。
##########实操验证##########
[root@vms100 ~]# ./list_img 192.168.26.101
192.168.26.101:5000/cka/centos:v1
192.168.26.101:5000/rhce8/mysql:v1
192.168.26.101:5000/rhce8/wordpress:v2
[root@vms100 ~]#
步骤6:下载镜像。
##########实操验证##########
[root@vms100 ~]# docker pull 192.168.26.101:5000/cka/centos:v1
v1: Pulling from cka/centos
364f9b7c969a: Already exists
a80ee3336562: Already exists
Digest: sha256:0276a952e12c2cd822b2f8879ccc8d6a175d66e8fc04fa27c25eb207a7895e01
Status: Downloaded newer image for 192.168.26.101:5000/cka/centos:v1
192.168.26.101:5000/cka/centos:v1
[root@vms100 ~]#
步骤7:删除此镜像。
##########实操验证##########
[root@vms100 ~]# docker rmi 192.168.26.101:5000/cka/centos:v1
Untagged: 192.168.26.101:5000/cka/centos:v1
Untagged: 192.168.26.101:5000/cka/centos@sha256:0276a952e12c2cd822b2f8879ccc8d6a175d66e8fc04fa27c25eb207a7895e01
Deleted: sha256:09a13e02d80a1f9082c04d8672b10251762d7c565a8d3922ff40714ef8e6c014
[root@vms100 ~]#
本地已经拉取下来的镜像可以通过docker rmi来删除,那么存在仓库里的镜像该如何删除呢?下面开始练习删除仓库里的镜像。
因为镜像仓库是放在vms101上的,所以下面的步骤是在vms101上操作的。
用命令wget
ftp://ftp.rhce.cc/cka-tool/delete_docker_registry_image下载脚本。
步骤1:设置变量REGISTRY_DATA_DIR,值为/path/docker/registry/v2,此处的/path是在创建容器时物理机对应的目录,这里是/myreg。
##########实操验证##########
[root@vms101 ~]# export REGISTRY_DATA_DIR=/myreg/docker/registry/v2
[root@vms101 ~]# chmod +x delete_docker_registry_image
[root@vms101 ~]#
步骤2:现在删除rhce8/wordpress:v2这个镜像。
##########实操验证##########
[root@vms101 ~]# ./delete_docker_registry_image -i rhce8/wordpress:v2
INFO [2023-05-03 13:10:57,967] Deleting /myreg/docker/registry/v2/blobs/sha256/46/4676067592f277e1723e4ab4c588603df0b3dea762e22c354f7ada29b391cf10
INFO [2023-05-03 13:10:57,970] Deleting /myreg/docker/registry/v2/blobs/sha256/50/500c148f4d2bc727705f60726244dbe81ea49c82d24b4b75bbc95785f8f59c28
INFO [2023-05-03 13:10:57,970] Deleting /myreg/docker/registry/v2/blobs/sha256/a1/a1f92323ce632857be9799acd2f47d6037862312a78cafeb0def5bc1cd4a4bd9
INFO [2023-05-03 13:10:57,970] Deleting /myreg/docker/registry/v2/blobs/sha256/21/21ca94ab129e63d21156d1185897ee9f47b751d4acf08e5d357c0a7e01c0b8c3
INFO [2023-05-03 13:10:57,970] Deleting /myreg/docker/registry/v2/blobs/sha256/7b/7bf6285c3cb3496d442c9da238840311ad5300de9529848c78d50a5fa31b95e7
INFO [2023-05-03 13:10:57,970] Deleting /myreg/docker/registry/v2/blobs/sha256/4c/4cba0c8f2fb9ebdf1bf77a5f3e362ac11301efe6ea4c72b48ec475704811f192
INFO [2023-05-03 13:10:57,970] Deleting /myreg/docker/registry/v2/blobs/sha256/3a/3a64563707752ce8e348b2ec97e545c9eec87150028c4435b5d054215bf9416d
INFO [2023-05-03 13:10:57,971] Deleting /myreg/docker/registry/v2/blobs/sha256/47/47aadd06aa861e7fe2e51a47d18609b878e4b6e7a6eb7503726290dde541789d
INFO [2023-05-03 13:10:57,971] Deleting /myreg/docker/registry/v2/blobs/sha256/d7/d78a85ab33d971061da05887be7a089a43c3d2ca6c79f6146fc46320af045fde
INFO [2023-05-03 13:10:57,972] Deleting /myreg/docker/registry/v2/blobs/sha256/13/13ce341a48bcfddbd7f3a37fbd416ba7afbba8e19473bd40ed562b4dfb7db274
INFO [2023-05-03 13:10:57,985] Deleting /myreg/docker/registry/v2/blobs/sha256/2e/2e9c78a839ea6f24d8ed9b6268c5f7dac359953b86fa28f1fc5e84a22537157f
INFO [2023-05-03 13:10:57,986] Deleting /myreg/docker/registry/v2/blobs/sha256/16/16cb800870057c22e7ae56cc4fd1c6ba25fbe8037c518702454c0f52a9d6a0f8
INFO [2023-05-03 13:10:57,987] Deleting /myreg/docker/registry/v2/blobs/sha256/43/43cdadc01967acd376acd72fe5c2b449822b2b58eb1285fc77328b90f8e03418
INFO [2023-05-03 13:10:57,987] Deleting /myreg/docker/registry/v2/blobs/sha256/4c/4cf805694c593ac99f6363c0e0ad468c54e6d9ecc55108db4070d1858b779ace
INFO [2023-05-03 13:10:57,988] Deleting /myreg/docker/registry/v2/blobs/sha256/8f/8f8908a0bafbb9663be870461595439404d5ce2be4810e19cc9288daf65b2512
INFO [2023-05-03 13:10:57,988] Deleting /myreg/docker/registry/v2/blobs/sha256/21/21f90b3df721af13a7b797ff5bb5d447a48984c13a9eb065654534493691ab6d
INFO [2023-05-03 13:10:57,994] Deleting /myreg/docker/registry/v2/blobs/sha256/dc/dccaeccfba3693896ee17c63257e0159b0c909ac33cc7c846f5598de0d7d19fb
INFO [2023-05-03 13:10:57,994] Deleting /myreg/docker/registry/v2/blobs/sha256/ca/ca4cf4692b7bebd81f229942c996b1c4e6907d6733e977e93d671a54b8053a22
INFO [2023-05-03 13:10:57,995] Deleting /myreg/docker/registry/v2/blobs/sha256/04/045041220168d92a2f46190dffb984ae57a3a429d5fe43d8dddea9894565def3
INFO [2023-05-03 13:10:57,995] Deleting /myreg/docker/registry/v2/blobs/sha256/8e/8eac15b405f6f21a74fa6e710acaa23ecfdb9d25ec6e365d8541510bc9b58a2b
INFO [2023-05-03 13:10:57,995] Deleting /myreg/docker/registry/v2/repositories/rhce8/wordpress
[root@vms101 ~]#
步骤3:到vms100上验证。
##########实操验证##########
[root@vms100 ~]# ./list_img 192.168.26.101
192.168.26.101:5000/cka/centos:v1
192.168.26.101:5000/rhce8/mysql:v1
[root@vms100 ~]#
可以看到已经成功地删除了。
步骤4:自行把vms101上的registry容器删除。
##########实操验证##########
[root@vms101 ~]# docker rm -f myreg
myreg
[root@vms101 ~]#
步骤:为了以后方便,在vms100上为镜像
192.168.26.101:5000/cka/centos:v1重新做一个tag,名字为centos:v1。
##########实操验证##########
#之前已经删除了下载的cetnos:v1镜像,此处无法再重新做个tag了,验证失败