Docker仓库Harbor安装配置
Harbor是由 VMWare 公司开源的容器镜像仓库。Harbor功能包括:
- 镜像仓库权限控制
- 镜像远程同步复制
- 大规模镜像发布
- 镜像删除和空间回收
一、依赖环境安装
这里以centos7为例,依赖docker-ce、docker-compose(rust开发)工具,同时Harbor会对两者的版本有一些要求,这里为了便于安装,使用了docker-ce和epel源,方便直接安装好环境 。
1、docker-ce安装
step 1:安装一些必要的系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 Step 2:添加docker镜像源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo Step 3: 安装 Docker-CE yum -y install docker-ce Step 4: 开启Docker服务 systemctl start docker.server systemctl enable docker.server
官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。特点版本的安装方式如下:
Step 1: 查找Docker-CE的版本: yum list docker-ce.x86_64 --showduplicates | sort -r Step2 : 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos) yum -y install docker-ce-[VERSION]
2、docker-compose安装
docker-compose的安装方式有两种,一种是从github上下载已经编译好的二进制包,一种是通过epel源,直接通过yum的方式安装
方法一: curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose #查看版本 docker-compose version 方法二: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum -y install docker-compose
二、Harbor安装
1、修改配置
Harbor的安装分为离线安装和在线安装,这里建议使用离线安装方式,因为默认拉镜外镜像速度太慢了。下载地址https://github.com/goharbor/harbor/releases ,将offline包解压后,放到/opt/harbor 目录,修改harbor.yml.tmpl文件,这里给出部分配置:
hostname: 192.168.159.132 http: port: 80 https: port: 443 certificate: /data/cert/192.168.159.132.crt private_key: /data/cert/192.168.159.132.key harbor_admin_password: Harbor12345 database: password: root123 max_idle_conns: 50 max_open_conns: 1000 data_volume: /data
比较关键的配置是hostname,建议使用域名方式,这里只是测试使用了IP地址,选择IP和域名后面生成key的步骤会有一些小的区别。再有就是数据存方目录和证书文件存方目录。
2、证书生成
证书生成可以使用openssl ,也可以使用cfssl 工具进行生成,这里以官方方法,使用openssl ,如下:
生成CA证书私钥 openssl genrsa -out ca.key 4096 生成CA证书 openssl req -sha512 -new \ -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=192.168.159.132" \ -key 192.168.159.132.key \ -out 192.168.159.132.csr 生成服务器私钥 openssl genrsa -out 192.168.159.132.key 4096 生成服务端CA证书 openssl req -x509 -new -nodes -sha512 -days 3650 \ -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=192.168.159.132" \ -key ca.key \ -out ca.crt 生成x509 v3 扩展文件 cat > v3.ext <<-EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment extendedKeyUsage = serverAuth subjectAltName = IP:192.168.159.132 EOF 生成主机证书 openssl x509 -req -sha512 -days 3650 \ -extfile v3.ext \ -CA ca.crt -CAkey ca.key -CAcreateserial \ -in 192.168.159.132.csr \ -out 192.168.159.132.crt 复制服务端证书文件到认证目录 mkdir -p /data/cert/ cp 192.168.159.132.crt /data/cert/ cp 192.168.159.132.key /data/cert/ 增加docker证书可信 openssl x509 -inform PEM -in 192.168.159.132.crt -out 192.168.159.132.cert cp 192.168.159.132.cert /etc/docker/certs.d/192.168.159.132/ cp 192.168.159.132.key /etc/docker/certs.d/192.168.159.132/ cp ca.crt /etc/docker/certs.d/192.168.159.132/
这里步骤比较繁琐,如果使用域名,将上面的IP地址修改成域名就可以了,不过生成v3.ext这一步需要注意,如果是域名的话,对应的命令会是如下:
openssl x509 -req -sha512 -days 3650 \ -extfile v3.ext \ -CA ca.crt -CAkey ca.key -CAcreateserial \ -in yourdomain.com.csr \ -out yourdomain.com.crt
3、完成harbor的安装
进入解压的/opt/harbor目录,使用prepare进行环境检查(此步也可以跳过),使用install.sh文件运行安装,在离线安装下docker自动load harbor压缩包里的image镜像,如果是在线安装,这里会等待比较长时间会下载相关镜像。安装完成后,可以通过docker ps 或docker-compose ps查看已经启动的镜像信息。
[root@localhost harbor]# docker-compose ps Name Command State Ports -------------------------------------------------------------------------------------------------------- harbor-core /harbor/entrypoint.sh Up harbor-db /docker-entrypoint.sh Up harbor-jobservice /harbor/entrypoint.sh Up harbor-log /bin/sh -c /usr/local/bin/ ... Up 127.0.0.1:1514->10514/tcp harbor-portal nginx -g daemon off; Up nginx nginx -g daemon off; Up 0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp redis redis-server /etc/redis.conf Up registry /home/harbor/entrypoint.sh Up registryctl /home/harbor/start.sh Up
此时在目录下也会生成docker-compose.yml文件,通过docker-compose可以很方便的进行服务启停,指令如下:
docker-compose down -v 或 docker-compose stop //区别,前者会remove掉容器、image、网络,停的更干净,后者只是停服务 docker-compose up -d
4、配置项目仓库
打开 https://192.168.159.132/ 登录,默认用户名密码 admin / Harbor12345 (在配置文件中修改过的,使用修改后的密码),登录后创建test项目仓库。
三、镜像推拉测试
使用docker login 192.168.159.132登录,使用的登录用户名密码是之前配置的用户名密码信息,如果已经登录过的,会在/root/.docker/config.json文件中记录,如下:
[root@localhost ~]# more /root/.docker/config.json { "auths": { "192.168.159.132": { "auth": "YWRtaW46SGFyYm9yMTIzNDU=" } } }
直接base64可以解密出信息。
使用docker pull hello-world可以多外部源拉一个测试镜像到本地,并重新打tag到harbor仓库,后面就可以从该仓库拉文件了。
远端拉镜像 [root@localhost ~]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world Digest: sha256:95ddb6c31407e84e91a986b004aee40975cb0bda14b5949f6faac5d2deadb4b9 Status: Image is up to date for hello-world:latest docker.io/library/hello-world:latest 重新打标签 [root@localhost ~]# docker tag hello-world 192.168.159.132/361way/hello-world:latest [root@localhost ~]# docker image ls|grep hello-world hello-world latest bf756fb1ae65 13 months ago 13.3kB 192.168.159.132/361way/hello-world latest bf756fb1ae65 13 months ago 13.3kB push到仓库 [root@localhost ~]# docker push 192.168.159.132/361way/hello-world:latest The push refers to repository [192.168.159.132/361way/hello-world] 9c27e219663c: Layer already exists latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525
完成后,可以测试从仓库里拉镜像测试
[root@localhost ~]# docker rmi bf756fb1ae65 -f Untagged: 192.168.159.132/361way/hello-world:latest Untagged: 192.168.159.132/361way/hello-world@sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 Untagged: 192.168.159.132/test/hello-world:latest Untagged: 192.168.159.132/test/hello-world@sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 Untagged: hello-world:latest Untagged: hello-world@sha256:95ddb6c31407e84e91a986b004aee40975cb0bda14b5949f6faac5d2deadb4b9 Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b [root@localhost ~]# docker run 192.168.159.132/361way/hello-world:latest [root@localhost ~]# docker image ls |grep hello-world 192.168.159.132/361way/hello-world latest bf756fb1ae65 13 months ago 13.3kB
四、安装中的问题
在key文件配置不当的时候,会遇到如下错误:
File "/usr/lib/python3.6/shutil.py", line 263, in copy2 copyfile(src, dst, follow_symlinks=follow_symlinks) File "/usr/lib/python3.6/shutil.py", line 120, in copyfile with open(src, 'rb') as fsrc: IsADirectoryError: [Errno 21] Is a directory: '/hostfs/data/cert'
解决方法就是把key文件重新配置即可。当然还会遇到其他问题,可以参考:https://goharbor.io/docs/2.1.0/install-config/troubleshoot-installation/
You can donate through PayPal.My paypal id: itybku@139.comPaypal page: https://www.paypal.me/361way
近期评论