Harbor仓库开启SSL

本文详细介绍了如何在内网环境中离线部署Docker和安装Docker-compose,特别提醒不要从特定源下载docker-compose。接着,通过下载Harbor的离线安装包,创建并配置CA证书,修改harbor.yaml文件,将证书添加到信任列表,然后启动安装。最后,文章提到了其他服务器连接Harbor的步骤,包括推送和拉取镜像,并记录了可能遇到的问题及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、部署docker

自行百度

二、安装docker-compose

自行百度,从github上下载即可。

需要提醒的是:不要从daocloud上面下载docker-compose,下载下来的完全没法用,会不停地给你安装docker,即使你已经安装了docker了!

三、安装harbor

因为是在内网,所以我们采用离线安装包的方式。

下载离线安装包:

[root@harbor ~]# wget https://github.com/goharbor/harbor/releases/download/v1.10.17/harbor-offline-installer-v1.10.17.tgz

解压

[root@harbor ~]# tar -zxf harbor-offline-installer-v1.10.17.tgz -C /app

创建CA证书(把11.0.1.5改成你自己的harbor的服务器IP)

[root@harbor ~]# mkdir /app/harbor/certs.d
[root@harbor ~]# cd /app/harbor/certs.d
[root@harbor certs.d]# openssl req  -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 365 -out ca.crt
Generating a 4096 bit RSA private key
................................................................++
............................................................................++
writing new private key to 'ca.key'
-----
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]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:DevOps
Organizational Unit Name (eg, section) []:Tec
Common Name (eg, your name or your server's hostname) []:11.0.1.5
Email Address []:

创建证书请求文件csr

[root@harbor certs.d]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout 11.0.1.5.key  -out 11.0.1.5.csr
Generating a 4096 bit RSA private key
...........................++
............................................................................................................................................................................................................................................................................................++
writing new private key to '11.0.1.5.key'
-----
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]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:DevOps
Organizational Unit Name (eg, section) []:Tec
Common Name (eg, your name or your server's hostname) []:11.0.1.5
Email Address []:
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

创建证书

[root@harbor certs.d]# echo subjectAltName = IP:11.0.1.5 > extfile.cnf
[root@harbor certs.d]# openssl x509 -req -days 365 -in 11.0.1.5.csr -CA ca.crt -CAkey ca.key -CAcreateserial -extfile extfile.cnf -out 11.0.1.5.crt
Signature ok
subject=/C=CN/ST=beijing/L=beijing/O=DevOps/OU=Tec/CN=192.168.11.11/emailAddress=352972405@qq.com
Getting CA Private Key

查看已生成的证书:

[root@harbor certs.d]# ll
总用量 28
-rw-r--r-- 1 root root 1915 3月  21 20:14 11.0.1.5.crt
-rw-r--r-- 1 root root 1687 3月  21 20:13 11.0.1.5.csr
-rw-r--r-- 1 root root 3272 3月  21 20:13 11.0.1.5.key
-rw-r--r-- 1 root root 1996 3月  21 20:13 ca.crt
-rw-r--r-- 1 root root 3272 3月  21 20:13 ca.key
-rw-r--r-- 1 root root   17 3月  21 20:14 ca.srl
-rw-r--r-- 1 root root   29 3月  21 20:14 extfile.cnf

修改harbor.yaml文件

# hostname这里修改为你的IP,跟上面创建证书使用的IP一致
hostname: 11.0.1.5

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 18080

# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  # 证书路径
  certificate: /app/harbor/certs.d/11.0.1.5.crt
  private_key: /app/harbor/certs.d/11.0.1.5.key

将证书加为受信任的证书:

[root@harbor certs.d]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
[root@harbor certs.d]# cat /app/harbor/certs.d/11.0.1.5.crt >> /etc/pki/tls/certs/ca-bundle.crt
[root@harbor certs.d]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

重启docker

[root@harbor certs.d]# systemctl restart docker

开始安装harbor

[root@harbor certs.d]# cd /app/harbor
[root@harbor harbor]# ./prepare
[root@harbor harbor]# ./install.sh --with-notary --with-clair

docker登录私有仓库测试:

[root@harbor harbor]# docker login -u admin -p Harbor12345 11.0.1.5:443
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

登录成功后,认证信息会保存在/root/.docker里面:

[root@harbor ~]# cat .docker/config.json 
{
	"auths": {
		"11.0.1.5:443": {
			"auth": "YWRtaW46SGFyYm9yMTIzNDU="
		}
	}
}
[root@harbor ~]# 

再次登录时,就不需要加用户名和密码了:

[root@harbor harbor]# docker login 11.0.1.5:443
Authenticating with existing credentials...
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

四、其他服务器连接harbor

在新的服务器上/etc/docker/上创建目录:

mkdir /etc/docker/certs.d/11.0.1.5

将harbor的证书文件发送过去

scp /app/harbor/certs.d/* 11.0.1.4:/etc/docker/certs.d/11.0.1.5

同样将证书添加为受信任的证书:

[root@jenkins 11.0.1.5]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
[root@jenkins 11.0.1.5]# cat /etc/docker/certs.d/11.0.1.5/11.0.1.5.crt >> /etc/pki/tls/certs/ca-bundle.crt
[root@jenkins 11.0.1.5]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

重启docker

systemctl restart docker 

登录测试:

[root@jenkins 11.0.1.5]# docker login -u admin -p Harbor12345 11.0.1.5:443
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

推送镜像:

nerdctl -n=k8s.io pull 11.0.1.5:443/library/nginx:latest

拉取镜像

nerdctl -n=k8s.io pull myharbor.com/library/nginx:latest

五、containerd连接harbor

containerd无法直接登录harbor,需要借助nerdctl工具,具体安装方式自行百度。
将证书从harbor服务器发送到containerd所在服务器上,并将证书添加到可信:

[root@node02 certs.d]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
[root@node02 certs.d]# cat /app/harbor/certs.d/11.0.1.5.crt >> /etc/pki/tls/certs/ca-bundle.crt
[root@node02 certs.d]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

登录测试:

nerdctl login -u admin -p Harbor12345 11.0.1.5:443
WARN[0000] WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded

六、Kubernetes连接Harbor仓库

在Kubernetes中,通过配置ImagePullSecrets来存储私有仓库的认证信息并在Pod的spec中引用这些Secrets来拉取镜像是一个常见的做法。以下是一步一步的指南,以您的Harbor私有仓库(地址为11.0.1.5:443,用户名为admin,密码为Harbor12345)为例:

第一步:创建Kubernetes Secret
首先,您需要创建一个Kubernetes Secret对象来存储私有仓库的认证信息。这可以通过kubectl create secret命令完成,并指定docker-registry类型。

kubectl create secret docker-registry harbor-secret \
--docker-server=11.0.1.5:443 \
--docker-username=admin \ 
--docker-password=Harbor12345 \
--docker-email=your-email@example.com

请注意,这里–docker-email是一个可选参数,但某些仓库可能要求提供。将其替换为您的电子邮件地址或任何有效的电子邮件地址。

第二步:在Pod的spec中引用Secret
接下来,您需要在Pod的YAML配置文件中引用这个Secret,以便Pod能够从私有仓库拉取镜像。这通过在Pod的spec.imagePullSecrets字段中添加Secret的名称来实现。

以下是一个Pod配置的示例,展示了如何引用Secret:

apiVersion: v1  
kind: Pod  
metadata:  
  name: my-pod  
spec:  
  containers:  
  - name: my-container  
    image: harbor.local.com:8443/zhaoll/nginx:1.14.1-8.6
    imagePullPolicy: IfNotPresent  
  imagePullSecrets:  
  - name: harbor-secret

在这个示例中,Pod名为my-pod,它有一个容器my-container,该容器使用私有仓库中的my-image:latest镜像。imagePullSecrets字段包含了Secret的名称harbor-secret,该Secret包含了访问私有仓库所需的认证信息。

第三步:部署Pod
最后,使用kubectl apply命令部署Pod:

kubectl apply -f pod.yaml

其中pod.yaml是包含上述Pod配置的YAML文件。

验证
部署Pod后,您可以检查Pod的状态以确认它是否成功从私有仓库拉取了镜像。使用kubectl get pods查看Pod列表,并使用kubectl describe pod 查看Pod的详细信息,包括其事件和状态。

[root@master1 ~]# kubectl get pod
NAME                                      READY   STATUS        RESTARTS       AGE
busybox                                   1/1     Running       49 (92s ago)   20d
my-pod                                    1/1     Running       0              5m59s

问题记录:

1、通过浏览器访问Harbor页面时,报500内部错误的话,可能是科学上网工具Ss的问题,将Ss退出或改为其他的模式。
2、docker login的时候报404,将harbor.yml的hostname改成跟证书使用的一致,如果创建证书的过程中使用的是11.0.1.5,hostname也使用这个IP。反之如果使用的是主机名,这里也改成主机名。
3、docker login的时候提示“wanted to harbor.com”类似的内容,将/etc/hosts的主机名解析注释掉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dustin.Hoffman

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

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

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

打赏作者

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

抵扣说明:

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

余额充值