七、Harbor构建企业级私有docker镜像的仓库的开源解决方案

目录

 

# Harbor 介绍

# Harbor特性

# Harbor组件

## 1、设置harbor服务器主机名为harbor

## 为 Harbor 自签发证书

## 生成CA证书

## 生成域名证书

## 签发证书:

##安装 Harbor

2、初始化操作:关闭harbor服务器防火墙、禁用SELinux


# Harbor 介绍

Docker 容器应用的开发和运行离不开可靠的镜像管理,虽然 Docker 官方也提供了公共的镜像仓库,
但是从安全和效率等方面考虑,部署我们私有环境内的 Registry 也是非常必要的。Harbor 是由 VMware
公司开源的企业级的 Docker Registry 管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、
自我注册、镜像复制和中文支持等功能。
官网地址:https://github.com/goharbor/harb

# Harbor特性

1、基于角色的访问控制 :用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。

2、镜像复制 : 镜像可以在多个Registry实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。

3、图形化用户界面 : 用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。

4、AD/LDAP 支持 : Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。

5、审计管理 : 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。

6、国际化 : 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。

7、RESTful API : RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。

8、部署简单 : 提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。

# Harbor组件

Harbor在架构上主要由6个组件构成:
1、harbor-adminserver:harbor系统管理接口,可以修改系统配置以及获取系统信息
2、harbor-db:存储项目的元数据、用户、规则、复制策略等信息
3、harbor-jobservice:harbor里面主要是为了镜像仓库之前同步使用的
4、harbor-log:收集其他harbor的日志信息。rsyslogd
5、harbor-ui:一个用户界面模块,用来管理registry。主要是前端的页面和后端CURD的接口
6、nginx:harbor的一个反向代理组件,代理registry、ui、token等服务。这个代理会转发harbor web和docker client的各种请求到后端服务上。是个nginx。nginx负责流量转发和安全验证,对外提供的流量都是从nginx中转,它将流量分发到后端的ui和正在docker镜像存储的docker registry
7、registry:存储docker images的服务,并且提供pull/push服务。harbor需要对image的访问进行访问控制,当client每次进行pull、push的时候,registry都需要client去token服务获取一个可用的token。
8、redis:存储缓存信息
9、webhook:当registry中的image状态发生变化的时候去记录更新日志、复制等操作。
   Harbor是VMware公司开源的企业级Registry项目,可以帮助用户快速搭建一个企业级的镜像仓库。harbor由python语言开发,需要使用docker-compose工具进行启动。
   安装 harbor 的机器,主机名设置成 harbor
机器需要的内存至少要 2G,我分配的是 4G
机器 ip:192.168.200.101
4vCPU/4G 内存/100G 硬盘

## 1、设置harbor服务器主机名为harbor

 [root@node1 ssl]# hostnamectl set-hostname harbor && bash
[root@harbor ssl]#

## 

## 为 Harbor 自签发证书

[root@node1 ~]# mkdir /data/ssl -p
[root@node1 ~]# cd /data/ssl/

## 生成CA证书

[root@node1 ssl]# openssl genrsa -out ca.key 3072
Generating RSA private key, 3072 bit long modulus
.........................................++
.....++
e is 65537 (0x10001)
#生成一个 3072 位的 key,也就是私钥
[root@harbor ssl]# openssl req -new -x509 -days 3650 -key ca.key -out ca.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CH
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:
您在 /var/spool/mail/root 中有新邮件
#生成一个数字证书 ca.pem,3650 表示证书的有效时间是 3 年,按箭头提示填写即可,没有箭头标注的为
空:

## 生成域名证书

[root@harbor ssl]# openssl genrsa -out harbor.key 3072
Generating RSA private key, 3072 bit long modulus
.................................................................++
.................................................................++
e is 65537 (0x10001)
#生成一个 3072 位的 key,也就是私钥
#生成一个证书请求,一会签发证书时需要的,标箭头的按提示填写,没有箭头标注的为空:
[root@harbor ssl]# openssl req -new -key harbor.key -out harbor.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CH
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:harbor
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

## 签发证书:

[root@harbor ssl]# openssl x509 -req -in harbor.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out harbor.pem -days 3650
显示如下,说明证书签发好了:
Signature ok
subject=/C=CH/ST=BJ/L=BJ/O=Default Company Ltd/CN=harbor
Getting CA Private Key

##安装 Harbor


18.2.1 安装 Docker
关闭iptables防火墙

[root@harbor ~]# yum install -y iptables-services.x86_64            #安装iptables
禁用iptables
[root@harbor ~]# service iptables stop && systemctl disable iptables
清空防火墙规则
[root@harbor ~]# iptables -F

2、初始化操作:关闭harbor服务器防火墙、禁用SELinux

[root@harbor ~]# systemctl stop firewalld && systemctl disable firewalld
[root@harbor ~]# setenforce 0
setenforce: SELinux is disabled
[root@harbor~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
注意:修改 selinux 配置文件之后,重启机器,selinux 才能永久生效
#配置时间任务
[root@harbor ~]# crontab -e
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
重启 crond 服务使配置生效:
[root@harbor ~]# systemctl restart crond
配置hosts文件

[root@harbor ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.101         harbor
192.168.200.100         master
安装基础软件包
[root@harbor ~]# yum install -y wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack

安装docker-ce

配置docker-ce 国内阿里云yum源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装docker依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

安装docker-ce

yum install -y docker-ce 

#启动docker服务

[root@harbor ~]# systemctl start docker && systemctl enable docker
[root@harbor ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2022-05-31 11:48:09 CST; 21h ago
看到running表示docker正常运行

#查看docker版本信息


[root@harbor ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.14
#开启包转发功能修改内核参数

[root@harbor ~]# modprobe br_netfilter
[root@harbor ~]# cat /etc/sysctl.d/docker.conf <<EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.btidge.bridge-nf-call-iptables = 1
> net.ipv4.ip_forward = 1
> EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
重新加载内核参数

[root@harbor ~]# sysctl -p /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
#重启docker 

root@harbor ~]# systemctl restart docker
安装harbor

#创建安装目录

[root@harbor ~]# mkdir /data/install -p
您在 /var/spool/mail/root 中有新邮件
[root@harbor ~]# cd /data/install
安装 harbor/data/ssl 目录下有如下文件

[root@harbor ssl]# ls
ca.key  ca.pem  ca.srl  harbor.csr  harbor.key  harbor.pem
把harbor的离线包harbor-offline-installer-v2.3.0-rc3.tgz上传到这个目录

[root@harbor install]# ls
harbor-offline-installer-v2.3.0-rc3.tgz
解压:

[root@harbor install]# tar zxvf harbor-offline-installer-v2.3.0-rc3.tgz
harbor/harbor.v2.3.0.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl
[root@harbor install]# cd harbor/
[root@harbor harbor]# ls
common.sh  harbor.v2.3.0.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare

[root@harbor harbor]# cp harbor.yml.tmpl harbor.yml
修改配置文件

[root@harbor harbor]# vim harbor.yml

#修改主机名hostname和上面颁发的证书保持一致

hostname: harbor

协议用https

  certificate: /data/ssl/harbor.pem
  private_key: /data/ssl/harbor.key

#修改之后保存退出

注:harbor默认账号密码:admin /Harbor12345

安装docker-compose

将docker-compose-Linux-x86_64文件上传到harbor机器上

[root@harbor harbor]# ls
common.sh                       harbor.v2.3.0.tar.gz  harbor.yml.tmpl  LICENSE
docker-compose-Linux-x86_64.64  harbor.yml            install.sh       prepare
[root@harbor harbor]# mv docker-compose-Linux-x86_64.64 /usr/bin/docker-compose
mv:是否覆盖"/usr/bin/docker-compose"? y
[root@harbor harbor]# chmod +x /usr/bin/docker-compose
注: docker-compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。
Docker-Compose 的工程配置文件默认为 docker-compose.yml,Docker-Compose 运行目录下的必要有一个docker-compose.yml。docker-compose 可以管理多个 docker 实例。

安装 harbor 需要的离线镜像包 docker-harbor-2-3-0.tar.gz,可上传到 harbor 机器,通过
docker load -i 解压

[root@harbor install]# ls
docker-harbor-2-3-0.tar.gz  harbor  harbor-offline-installer-v2.3.0-rc3.tgz
[root@harbor install]# docker load -i docker-harbor-2-3-0.tar.gz
[root@harbor install]# cd harbor/
[root@harbor harbor]# ./install.sh
看到下面内容,说明安装成功:

[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-db     ... done
Creating redis         ... done
Creating registry      ... done
Creating harbor-portal ... done
Creating registryctl   ... done
Creating harbor-core   ... done
Creating nginx             ... done
Creating harbor-jobservice ... done
✔ ----Harbor has been installed and started successfully.----
在自己电脑修改 hosts 文件
如何停掉 harbor:

[root@harbor harbor]# cd /data/install/harbor/
[root@harbor harbor]# docker-compose stop
Stopping harbor-jobservice ... done
Stopping nginx             ... done
Stopping harbor-core       ... done
Stopping harbor-portal     ... done
Stopping registryctl       ... done
Stopping registry          ... done
Stopping harbor-db         ... done                                                                                     
Stopping redis             ... done
Stopping harbor-log        ... done
启动harbor

[root@harbor harbor]# docker-compose start
如果 docker-compose start 启动 harbor 之后,还是访问不了,那就需要重启虚拟机
# Harbor 图像化界面使用说明
在浏览器输入:https://harbor

接收风险并继续,出现如下界面,说明访问正常
账号:admin
密码:Harbor12345

输入账号密码出现如下:

所有基础镜像都会放在 library 里面,这是一个公开的镜像仓库
新建项目->起个项目名字 test(把访问级别公开那个选中,让项目才可以被公开使用)测试使用 harbor 私有镜像仓库

#修改 docker 配置

 [root@harbor harbor]# cat /etc/docker/daemon.json
{"registry-mirrors": ["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com"],
"insecure-registries": ["192.168.200.101","harbor"]

 修改配置后使配置生效

[root@harbor harbor]# systemctl daemon-reload && systemctl restart docker
#查看dockers是否启动成功

[root@harbor harbor]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2022-06-01 12:05:32 CST; 12s ago
#显示如下,说明启动成功

 注意:
配置新增加了一行内容如下:
"insecure-registries":["192.168.200.101"],
上面增加的内容表示我们内网访问 harbor 的时候走的是 http,192.168.40.181 是安装 harbor 机器
的 ip

登录 harbor:

[root@harbor harbor]# docker login 192.168.200.101
Username: harbor
Password:
注意报错:Error response from daemon: Get "http://192.168.200.101/v2/": dial tcp 192.168.200.101:80: connect: connection refused

在此记录下这个坑。

/etc/docker/daemon.json
在这个文件中添加:{ "insecure-registries":["192.168.2.101"] },写上自己的ip。若没有这个文件则新建一个。

然后回到Harbor的文件夹中。

drwxr-xr-x. 4 root root        37 Dec 11 15:38 common
-rw-r--r--. 1 root root      1185 Sep 12  2018 docker-compose.clair.yml
-rw-r--r--. 1 root root      1725 Sep 12  2018 docker-compose.notary.yml
-rw-r--r--. 1 root root      3596 Sep 12  2018 docker-compose.yml
drwxr-xr-x. 3 root root       156 Sep 12  2018 ha
-rw-r--r--. 1 root root      6953 Dec 11 15:38 harbor.cfg
-rw-r--r--. 1 root root 915878468 Sep 12  2018 harbor.v1.5.3.tar.gz
-rwxr-xr-x. 1 root root      5773 Sep 12  2018 install.sh
-rw-r--r--. 1 root root     10764 Sep 12  2018 LICENSE
-rw-r--r--. 1 root root       482 Sep 12  2018 NOTICE
-rw-r--r--. 1 root root   1247461 Sep 12  2018 open_source_license
-rwxr-xr-x. 1 root root     27840 Sep 12  2018 prepare
再次执行./install.sh即可。

再重新登陆就能登陆成功!

[root@harbor harbor]# ./install.sh

[root@harbor harbor]# docker login 192.168.200.101
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
输入账号密码之后看到如下,说明登录成功了:
Login Succeeded
#导入 tomcat 镜像

 [root@harbor harbor]# ls
common  common.sh  docker-compose.yml  harbor.v2.3.0.tar.gz  harbor.yml  harbor.yml.tmpl  install.sh  LICENSE  prepare  tomcat.tar.gz

把tomcat镜像打标签

[root@harbor harbor]# docker tag tomcat:latest 192.168.200.100/test/tomact:v1
执行上面命令就会把 192.168.200.100/test/tomcat:v1 上传到 harbor 里的 test 项目下

执行如下命令将192.168.40.181/test/tomcat:v1 上传到 harbor 里的test项目下

[root@harbor harbor]# docker push 192.168.200.101/test/tomcat:v1
The push refers to repository [192.168.200.101/test/tomcat]
a9502f3f1738: Pushed
26cdef4ed0c4: Pushed
e48093759a19: Pushed
c47f8e016290: Pushed
c0848348e2f7: Pushed
79c550eb7bd2: Pushed
7095af798ace: Pushed
fe6a4fdbedc0: Pushed
e4d0e810d54a: Pushed
4e006334a6fd: Pushed
v1: digest: sha256:a3e9f6c942098d3b32c7810d3ec00079719198c9af41c3a32f6fc5d66124155f size: 2421

 从harbor仓库下载镜像

1.从harbor机器上删除镜像

[root@harbor harbor]# docker rmi -f 192.168.200.101/test/tomcat:v1
Untagged: 192.168.200.101/test/tomcat:v1
Untagged: 192.168.200.101/test/tomcat@sha256:a3e9f6c942098d3b32c7810d3ec00079719198c9af41c3a32f6fc5d66124155f
2.拉去镜像

[root@harbor harbor]# docker pull 192.168.200.101/test/tomcat:v1
v1: Pulling from test/tomcat
Digest: sha256:a3e9f6c942098d3b32c7810d3ec00079719198c9af41c3a32f6fc5d66124155f
Status: Downloaded newer image for 192.168.200.101/test/tomcat:v1
192.168.200.101/test/tomcat:v1
[root@harbor harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
192.168.200.101/test/tomcat     v1        921ef208ab56   10 months ago   668MB
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

k8s小王

你的鼓励是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值