在很多情况下,我们需要使用自己的docker仓库。接下来是在本地k8s集群上搭建本地仓库的过程。

假设我们将本地仓库搭建在k8s主节点上。

这里假设主节点是:192.168.14.84

运行registry镜像

1
docker run -d -p 5000:5000 --restart always --name registry registry:2

直接运行该命令即可,本地没有registry:2,会自动pull

修改配置文件

vim /etc/docker/daemon.json

1
2
3
4
5
{
   "insecure-registries": [
         "192.168.14.84:5000"
     ]
 }

重启docker服务

1
systemctl restart docker

验证是否成功

这里的思想是,先下载一个hello-world的镜像,然后修改标签,然后上传到本地服务器上。然后查看服务器是否有该镜像。

下载镜像,上传到本地仓库

1
2
3
docker pull hello-world
docker tag hello-world:latest 192.168.14.84:5000/hello-world:latest
docker push 192.168.14.84:5000/hello-world

删除本地的192.168.14.84:5000/hello-world:latest

1
2
docker rmi 192.168.14.84:5000/hello-world:latest
docker rmi hello-world:latest

然后重新拉取看是否成功

1
docker pull 192.168.14.84:5000/hello-world

查看本地仓库镜像的命令

1
curl -X GET http://192.168.14.84:5000/v2/_catalog

PS:

以上都是在master节点做的操作,要想在其他node节点上也能使用本地仓库,只需要需要在其他节点进行步骤2的操作,修改/etc/docker/daemon.json文件。文件内容和上述一样。