跳至主要內容

搭建Harbor

知识库运维技巧开发搭建DockerHarbor大约 2 分钟

harbor 是一个 docker 私有镜像仓库。
Harbor 的优势:

  • 图形管理界面、
  • 按项目管理镜像、
  • 独立的用户管理,不同用户可以操作不同镜像,细粒度的权限控制,包含 create、push 、pull、delete
  • 镜像管理
  • 标签管理、
  • 操作日志管理。

安装

########################
## 官网:https://goharbor.io/
## 官方开源:https://github.com/goharbor/harbor
## 安装包下载地址:https://github.com/goharbor/harbor/releases
########################

# 确保已经安装docker和docker-compose
docker -v
docker-compose -v
# 下载安装包
wget https://github.com/goharbor/harbor/releases/download/v1.10.10/harbor-offline-installer-v1.10.10.tgz
tar -zxvf harbor-offline-installer-v1.10.10.tgz -C /usr/local/lib/
ls /usr/local/lib/harbor
# 备份一下默认的配置文件
cp /usr/local/lib/harbor/harbor.yml /usr/local/lib/harbor/harbor.yml.default
# 修改配置文件 hostname
sed -i 's/hostname:.*/hostname: localhost/g' /usr/local/lib/harbor/harbor.yml
# hostname: localhost
# http:
#   port: 8080
# ## 如果使用HTTPS需要添加证书,证书生成的方法在下面
# #https:
# #  port: 8081
#   # 证书的位置
# #  certificate: /data/certs/server.crt
# #  private_key: /data/certs/server.key
# # 默认账号 admin 的初始密码
# harbor_admin_password: Harbor12345
# 配置生效且安装harbor
cd /usr/local/lib/harbor && ./prepare && ./install.sh
# 安装完毕访问地址:http://192.168.13.103:8080 默认:admin/Harbor12345

# 停止harbor
cd /usr/local/lib/harbor && docker-compose down
# 配置service
cat > /lib/systemd/system/harbor.service << EOF
[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor

[Service]
Type=simple
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/bin/docker-compose -f  /usr/local/lib/harbor/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /usr/local/lib/harbor/docker-compose.yml down

[Install]
WantedBy=multi-user.target
EOF
# 加载配置并设置开机自启
systemctl daemon-reload && systemctl enable harbor
# 启动harbor
systemctl restart harbor && systemctl status harbor

docker 信任配置

# 配置 { "insecure-registries": ["192.168.13.103:8080"] }
vi  /etc/docker/daemon.json
# 重启docker
systemctl daemon-reload && systemctl restart docker.service
# 用docker登录harbor
docker login 192.168.13.103:8080 -u 用户名 -p 密码
# 下载image
docker pull nginx:1.21.5
# 在项目中标记镜像
docker tag nginx:1.21.5 192.168.13.103:8080/library/nginx:1.21.5
# 推送镜像到当前项目
docker push 192.168.13.103:8080/library/nginx:1.21.5
# 下载镜像
docker pull 192.168.13.103:8080/library/nginx:1.21.5

https 生成证书(http 可忽略)

# 进入创建的存储证书的目录
mkdir -p /data/certs/ && cd /data/certs/
# 首先生成证书私钥 数据密码,回车
openssl genrsa -des3 -out server.key 2048
# 证书的服务 数据上面的密码,回车
openssl req -new -key server.key -out server.csr
# 输出内容为:
# Enter pass phrase for root.key: ← 输入前面创建的密码
# Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
# State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
# Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
# Organizational Unit Name (eg, section) []: ← 可以不输入
# Common Name (eg, YOUR name) []: ← 此时不输入
# Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
# Please enter the following ‘extra’ attributes
# to be sent with your certificate request
# A challenge password []: ← 可以不输入
# An optional company name []: ← 可以不输入
# 备份私钥
cp server.key server.key.org
# 转换为证书
openssl rsa -in server.key.org -out server.key
# 给证书签名
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
# 给所有的证书授权
chmod 755 *