因爲Docker Hub公共倉庫不少時候使用這並非很方便,大分部由於網絡的問題可能拉取的時候會很慢或者拉取不到,因此搭建一個本地的私有倉庫。html
因爲此篇文章是在Kubernetes集羣安裝部署這篇文章的基礎上補充docker私有倉庫的搭建,因此安裝機器配置過hosts的mysql
[root@k8s-master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.44.60 etcd
192.168.44.60 registry
192.168.44.60 k8s-master
192.168.44.61 k8s-slave01
192.168.44.62 k8s-slave02
從Docker官方倉庫裏下載registry鏡像linux
[root@k8s-master ~]# docker pull registry
經過該鏡像啓動容器
nginx
[root@k8s-master ~]# docker run -d --name=my_registry -p 5000:5000 -v /opt/data/docker_registry:/var/lib/registry --restart=always --privileged=true registry
8eaed99a084999678795b9a6cedb8e57446f555dde6798756718f1b4decfee66
參數解釋:
-v /opt/data/registry:/var/lib/registry:默認狀況下,會將倉庫存放於容器內的/tmp/registry目錄下,指定本地目錄掛載到容器,不過也不必定是這個目錄,具體仍是進入鏡像內肯定一下最好,也有多是/var/lib/registry或者其餘目錄,若是不是的話從新啓動一下容器。
-p 5000:5000 :端口映射。即本地5000端口,映射到registry中的5000端口。
–restart=always:在容器退出時老是重啓容器,主要應用在生產環境。
–privileged=true:在CentOS7中的安全模塊selinux把權限禁掉了,參數給容器加特權,不加上傳鏡像會報相似權限錯誤。OSError: [Errno 13] Permission denied: '/tmp/registry/repositories/liibrary')或者(Received unexpected HTTP status: 500 Internal Server Error)
–-name=my_registry:指定容器的名稱,也能夠-name my_registry。
查看容器git
[root@k8s-master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8eaed99a0849 docker.io/registry "/entrypoint.sh /e..." 50 seconds ago Up 49 seconds 0.0.0.0:5000->5000/tcp my_registry
由上能夠看到,已經啓動了一個容器,地址爲:registry:5000也就是192.168.44.60:5000。github
修改daemon.jsonredis
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"insecure-registries":["registry:5000"]
}
重啓dockersql
systemctl restart docker
首先看一下如今機器上的鏡像docker
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/registry latest 33fbbf4a24e5 12 days ago 24.2 MB
docker.io/tomcat latest 1a51cb5e3006 2 weeks ago 462 MB
docker.io/nginx latest 7042885a156a 2 weeks ago 109 MB
docker.io/mysql latest 102816b1ee7d 2 weeks ago 486 MB
docker.io/redis latest 5d2989ac9711 2 weeks ago 95 MB
docker.io/centos latest 1e1148e4cc2c 5 weeks ago 202 MB
修改一下tomcat鏡像的tag標識,而後查看鏡像信息json
[root@k8s-master ~]# docker tag tomcat registry:5000/tomcat
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/registry latest 33fbbf4a24e5 12 days ago 24.2 MB
docker.io/tomcat latest 1a51cb5e3006 2 weeks ago 462 MB
registry:5000/tomcat latest 1a51cb5e3006 2 weeks ago 462 MB
docker.io/nginx latest 7042885a156a 2 weeks ago 109 MB
docker.io/mysql latest 102816b1ee7d 2 weeks ago 486 MB
docker.io/redis latest 5d2989ac9711 2 weeks ago 95 MB
docker.io/centos latest 1e1148e4cc2c 5 weeks ago 202 MB
發現多了一個registry:5000/tomcat鏡像,另外須要注意的是:修改了tag後的鏡像若要刪除,docker rmi後面不能用鏡像ID了,須要用docker rmi registry:5000/tomcat。
把上面修改tag後的鏡像上傳到私有倉庫
[root@k8s-master ~]# docker push registry:5000/tomcat
The push refers to a repository [registry:5000/tomcat]
10acf9008214: Pushed
f725cace5e8c: Pushed
70333c64c1a2: Pushed
a8586e8d29fb: Pushed
2d63501da7e5: Pushed
e6ae888dd260: Pushed
80917357f055: Pushed
bfc5dbc4d7a7: Pushed
b53b57a50746: Pushed
d2518892581f: Pushed
c581f4ede92d: Pushed
latest: digest: sha256:af0dc8139f8353ff2f10f8fe9fdf54a9d844f4c337b35dd40b6c0336d919e3f6 size: 2626
驗證是否將鏡像上傳到私有倉庫
[root@k8s-master ~]# curl -XGET http://registry:5000/v2/_catalog
{"repositories":["tomcat"]}
[root@k8s-master ~]# curl -XGET http://registry:5000/v2/tomcat/tags/list
{"name":"tomcat","tags":["latest"]}
查看映射的本地文件夾
[root@k8s-master ~]# ls /opt/data/docker_registry/
docker
此時即使將docker images中列出來的 registry:5000/tomcat刪掉,本地倉庫中的registry:5000/tomcat也仍是能夠正常使用的。
docker rmi registry:5000/tomcat
如此,Kubernetes集羣安裝部署這篇文章中的master的配置才起到了做用(能夠從本地倉庫拉取所需鏡像)。
這樣,也就能夠在同一局域網內的其餘機器上,從該私有倉庫中pull下來該鏡像:
docker pull registry:5000/tomcat
#若是沒有配置hosts信息,可經過ip來拉取
docker pull 192.168.44.60:5000/tomcat
下載delete_docker_registry_image工具
curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/delete_docker_registry_image.py | sudo tee /usr/local/bin/delete_docker_registry_image >/dev/null
更改目錄權限,設置目錄變量
chmod 755 /usr/local/bin/delete_docker_registry_image
export REGISTRY_DATA_DIR=/opt/data/docker_registry/docker/registry/v2
刪除私有倉庫鏡像
delete_docker_registry_image -i tomcat