在Gitlab中使用Harbor

提示,如果是直接在docker中运行的gitlab-runner需要在docker配置中将域名添加信任。如果是在k8s中运行的gitlab-runner,需要在ci文件中添加信任配置

在.gitlab-ci.yml文件中使用

构建镜像过程中添加如下代码可推送镜像至仓库

    - docker build -t $PROJECT .
    - docker login -u 'robot$gitlab' -p 'SeQh123vpuJo3iHz' harbor.thefunc.com
    - docker tag chatgptwebapi:latest harbor.thefunc.com/chatgpt/chatgptwebapi:latest
    - docker push harbor.thefunc.com/chatgpt/chatgptwebapi:latest
    - docker logout harbor.thefunc.com

Hits: 4740

Harbor简化版使用

机器人账户

登录

docker login -u  'robot$gitlab'  -p  'SeQh123tBCP7ubmMvpuJo3iHz' harbor.thefunc.com

拉取

docker pull harbor.thefunc.com/agilebpm/bpmtest@sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4

推送

docker tag SOURCE_IMAGE[:TAG] harbor.thefunc.com/agilebpm/REPOSITORY[:TAG]

docker push harbor.thefunc.com/agilebpm/REPOSITORY[:TAG]

样例

docker tag hello-world:latest harbor.thefunc.com/agilebpm/bpmtest:v1.0
docker push harbor.thefunc.com/agilebpm/bpmtest:v1.0

登出

docker logout harbor.thefunc.com

Hits: 4750

Harbor跟随系统启动

创建启动文件

cd /etc/systemd/system
vi harbor.service

下列中的 /usr/local/harbor 为harbor所在目录
/usr/bin/docker-compose 为 docker-compose 程序文件,路径可通过命令查找 find / -name docker-compose

[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/bin/docker-compose -f  /srv/harbor/docker-compose.yml up
ExecStop=/usr/bin/docker-compose -f  /srv/harbor/docker-compose.yml down

[Install]
WantedBy=multi-user.target

systemctl enable harbor
systemctl restart harbor

Hits: 4425

Harbor配置维护

例如修改Hostname

从主机名变更为域名以便于外部使用

先停止服务

docker-compose down -v
cd /srv/harbor

修改harbor.yml文件

hostname: harbor.thefunc.com

重新启动部署(如果不生效可能需要先执行./prepare)

./install.sh

再次启动服务

docker-compose up -d

Hits: 4837

Harbor安装

首先关闭SELINUX

其次开放http/https防火墙协议

yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose
systemctl enable docker && systemctl start docker
ll -t

自行官网下载

wget http://server.thefunc.com/download/harbor-offline-installer-v2.7.1.tgz
tar -xvzf harbor-offline-installer-v2.7.1.tgz -C /srv
cd /srv/harbor
ls
docker load -i harbor.v2.7.1.tar.gz
cp harbor.yml.tmpl harbor.yml

harbor.yml 修改hostname为指定服务名(服务名为外部客户端访问所能访问的地址)

hostname

修改hostname为当前主机名

vi harbor.yml
hostname: harbor.thefunc.com
harbor_admin_password: 666666
# Harbor DB configuration
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: 666666

SSL配置

https://goharbor.io/docs/1.10/install-config/configure-https/

mkdir -p /srv/harbor/cert && cd /srv/harbor/cert
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=GD/L=GZ/O=GGEC/OU=IT/CN=harbor.thefunc.com" \
 -key ca.key \
 -out ca.crt
openssl genrsa -out harbor.thefunc.com.key 4096
openssl req -sha512 -new \
    -subj "/C=CN/ST=GD/L=GZ/O=GGEC/OU=IT/CN=harbor.thefunc.com" \
    -key harbor.thefunc.com.key \
    -out harbor.thefunc.com.csr

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=harbor.thefunc.com
DNS.2=192.168.199.179
DNS.3=REDHAT7-Docker-Harbor-ImageService
EOF
openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in harbor.thefunc.com.csr \
    -out harbor.thefunc.com.crt
openssl x509 -inform PEM -in harbor.thefunc.com.crt -out harbor.thefunc.com.cert
mkdir -p /etc/docker/certs.d/harbor.thefunc.com
cp harbor.thefunc.com.cert /etc/docker/certs.d/harbor.thefunc.com/
cp harbor.thefunc.com.key /etc/docker/certs.d/harbor.thefunc.com/
cp ca.crt /etc/docker/certs.d/harbor.thefunc.com/
systemctl restart docker

修改harbor配置

# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /srv/harbor/cert/harbor.thefunc.com.crt
  private_key: /srv/harbor/cert/harbor.thefunc.com.key

安装

./prepare
./install.sh

打开网址访问

https://harbor.thefunc.com

超级密码 666666

Hits: 4758