docker私有仓库registry(v2)的搭建

人盡茶涼 提交于 2020-01-18 00:15:48

1、上传rpm文件,解压

yum文件:

链接:https://pan.baidu.com/s/1_tSwm2jqXqrV2mbMk0C80g 
提取码:5gd7

[root@k8smaster01 zhaiky]# unzip yum.zip 
Archive:  yum.zip
   creating: yum/
  inflating: yum/docker-1.13.1-103.git7f2769b.el7.centos.x86_64.rpm  
  inflating: yum/docker-client-1.13.1-103.git7f2769b.el7.centos.x86_64.rpm  
  inflating: yum/docker-common-1.13.1-103.git7f2769b.el7.centos.x86_64.rpm  
  inflating: yum/elinks-0.12-0.37.pre6.el7.0.1.x86_64.rpm  
  inflating: yum/etcd-3.3.11-2.el7.centos.x86_64.rpm  
  inflating: yum/flannel-0.7.1-4.el7.x86_64.rpm  
  inflating: yum/gpm-libs-1.20.7-6.el7.x86_64.rpm  
  inflating: yum/httpd-2.4.6-90.el7.centos.x86_64.rpm  
  inflating: yum/httpd-tools-2.4.6-90.el7.centos.x86_64.rpm  
  inflating: yum/js-1.8.5-20.el7.x86_64.rpm  
  inflating: yum/kernel-lt-4.4.207-1.el7.elrepo.x86_64.rpm  
  inflating: yum/kubernetes-client-1.5.2-0.7.git269f928.el7.x86_64.rpm  
  inflating: yum/kubernetes-master-1.5.2-0.7.git269f928.el7.x86_64.rpm  
  inflating: yum/lrzsz-0.12.20-36.el7.x86_64.rpm  
  inflating: yum/nss_compat_ossl-0.9.6-8.el7.x86_64.rpm  
  inflating: yum/ntp-4.2.6p5-29.el7.centos.x86_64.rpm  
  inflating: yum/ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm  
  inflating: yum/unzip-6.0-20.el7.x86_64.rpm  
  inflating: yum/vsftpd-3.0.2-25.el7.x86_64.rpm  
  inflating: yum/createrepo-0.9.9-28.el7.noarch.rpm  
   creating: yum/repodata/
  inflating: yum/repodata/f3ce581fcc0df911a991bb4de0ec69734d34e709163755cc73f2b33294ae3fc8-other.sqlite.bz2  
  inflating: yum/repodata/e7c0b54bf7034761afe8273df137b5fd02cb1ccd12c82a13d98b8f3dfb9ec097-other.xml.gz  
  inflating: yum/repodata/6ea25bac3dbbf238dbe019e0d8194d14e263b47bd569257552600e14b0703946-filelists.sqlite.bz2  
  inflating: yum/repodata/580edaa5e2bcf2ea8112c50fcb418be547462664f6a0f9d6ffcc9b5b032a2994-filelists.xml.gz  
  inflating: yum/repodata/1ce6e205b25bbcc793f0a114f2adab54498d1b1a7b480228845fc712b5dfec38-primary.sqlite.bz2  
  inflating: yum/repodata/654b6a5366bfd98b0669eb1ffe6faf41afb6ac26a8634da0a7d7963649e03b8c-primary.xml.gz  
  inflating: yum/repodata/repomd.xml  
[root@k8smaster01 zhaiky]#

2、配置本地yum源
2.1、安装http服务,将yum文件夹拷贝到,并设置权限
[root@k8smaster01 ~]# cp -r /home/zhaiky/yum /var/www/html/
[root@k8smaster01 html]# chmod -R 777 yum/
2.2、配置yum.repo文件
[root@k8snode01 etc]# more yum.repo 
[yum]
name=base
baseurl=http://192.168.23.134/yum
gpgcheck=0

[root@k8smaster01 yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base elrepo extras updates yum
[root@k8smaster01 yum.repos.d]# 

3、docker私有仓库的搭建,安装docker
[root@k8smaster01 ~]# yum install docker -y
Loaded plugins: fastestmirror
Determining fastest mirrors
yum                                                                                                                                                  | 2.9 kB  00:00:00     
yum/primary_db                                                                                                                                       | 454 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-103.git7f2769b.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                            Arch                               Version                                                        Repository                       Size
============================================================================================================================================================================
Installing:
 docker                             x86_64                             2:1.13.1-103.git7f2769b.el7.centos                             yum                              18 M

Transaction Summary
============================================================================================================================================================================
Install  1 Package

Total download size: 18 M
Installed size: 65 M
Downloading packages:
docker-1.13.1-103.git7f2769b.el7.centos.x86_64.rpm                                                                                                   |  18 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:docker-1.13.1-103.git7f2769b.el7.centos.x86_64                                                                                                         1/1 
  Verifying  : 2:docker-1.13.1-103.git7f2769b.el7.centos.x86_64                                                                                                         1/1 

Installed:
  docker.x86_64 2:1.13.1-103.git7f2769b.el7.centos                                                                                                                          

Complete!
[root@k8smaster01 ~]#

4、上传registry_v2.tar和nginx.tar导入镜像

镜像包:

链接:https://pan.baidu.com/s/1T4snL6ntNqCtxkF4eiUPcA 
提取码:jpfe 

[root@k8smaster01 zhaiky]# docker load -i registry_v2.tar
Loaded image: docker.io/registry:latest
[root@k8smaster01 zhaiky]# docker load -i nginx.tar 
Loaded image: 192.168.23.134:5000/nginx:latest
[root@k8smaster01 zhaiky]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
192.168.23.134:5000/nginx   latest              c7460dfcab50        5 days ago          126 MB
docker.io/registry          latest              f32a97de94e1        10 months ago       25.8 MB
[root@k8smaster01 zhaiky]# 

5、启动仓库
[root@k8smaster01 registry]# docker run -d -p 5000:5000 --restart=always --name private-docker-registry --privileged=true -v /data/registry:/var/lib/registry registry
76d008081e427438a2efed0b1f579711025b8c27265472296f3b89d1037d0c60
[root@k8smaster01 registry]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
76d008081e42        registry            "/entrypoint.sh /e..."   10 seconds ago      Up 9 seconds        0.0.0.0:5000->5000/tcp   private-docker-registry
[root@k8smaster01 registry]#
说明:-d表示容器在后台运行。-p 5000:5000表示将容器的5000端口映射到虚拟机的5000端口。–restart=always表示容器随着docker启动而启动,同时若容器异常终止,会自动启动;实测表明,使用docker stop命令停止容器后,
此参数不会让容器自动重启。–name private-docker-registry设置容器名。–privileged=true使容器真正具有容器内的root权限。-v /data/registry:/var/lib/registry将容器中的/var/lib/registry目录映射
到虚拟机的/data/registry;/var/lib/registry是私有仓库容器存放镜像的目录。

6、nginx推送到私有仓库
[root@k8smaster01 registry]# docker push 192.168.23.134:5000/nginx:latest
The push refers to a repository [192.168.23.134:5000/nginx]
Get https://192.168.23.134:5000/v1/_ping: http: server gave HTTP response to HTTPS client
[root@k8smaster01 registry]#
报错解决:
修改/usr/lib/systemd/system/docker.service配置
[root@k8smaster01 registry]# more /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=main
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current \
          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          --default-runtime=docker-runc \
          --insecure-registry 0.0.0.0/0 -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375 \
          --exec-opt native.cgroupdriver=systemd \
          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          --init-path=/usr/libexec/docker/docker-init-current \
          --seccomp-profile=/etc/docker/seccomp.json \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
      $REGISTRIES
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
KillMode=process

[Install]
WantedBy=multi-user.target
[root@k8smaster01 registry]# systemctl daemon-reload && systemctl restart docker
[root@k8smaster01 registry]# docker push 192.168.23.134:5000/nginx:latest
The push refers to a repository [192.168.23.134:5000/nginx]
c26e88311e71: Pushed 
17fde96446df: Pushed 
556c5fb0d91b: Pushed 
latest: digest: sha256:89a42c3ba15f09a3fbe39856bddacdf9e94cd03df7403cad4fc105088e268fc9 size: 948
[root@k8smaster01 registry]# 

7、查看仓库镜像
[root@k8smaster01 registry]# curl "http://192.168.23.134:5000/v2/_catalog"
{"repositories":["nginx"]}

8、从私有仓库pull镜像
[root@k8smaster01 registry]# docker rmi -f 192.168.23.134:5000/nginx
Untagged: 192.168.23.134:5000/nginx:latest
Untagged: 192.168.23.134:5000/nginx@sha256:89a42c3ba15f09a3fbe39856bddacdf9e94cd03df7403cad4fc105088e268fc9
Deleted: sha256:c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7
Deleted: sha256:3e51598e49c550f8b212a07c6ff2ed47a09eeb637f67d1b3c5468e9a8ee646e3
Deleted: sha256:a8b9a5643b3cc8082997d3d2fbaf4b53213ff80aa4169226be8b3768ae6e3605
Deleted: sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10
[root@k8smaster01 registry]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
docker.io/registry   latest              f32a97de94e1        10 months ago       25.8 MB
[root@k8smaster01 registry]# docker pull 192.168.23.134:5000/nginx
Using default tag: latest
Trying to pull repository 192.168.23.134:5000/nginx ... 
latest: Pulling from 192.168.23.134:5000/nginx
8ec398bc0356: Pull complete 
dfb2a46f8c2c: Pull complete 
b65031b6a2a5: Pull complete 
Digest: sha256:89a42c3ba15f09a3fbe39856bddacdf9e94cd03df7403cad4fc105088e268fc9
Status: Downloaded newer image for 192.168.23.134:5000/nginx:latest
[root@k8smaster01 registry]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
192.168.23.134:5000/nginx   latest              c7460dfcab50        5 days ago          126 MB
docker.io/registry          latest              f32a97de94e1        10 months ago       25.8 MB
[root@k8smaster01 registry]#
 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!