Harbor私有仓库搭建并配置https对接docker与kubernetes

这篇具有很好参考价值的文章主要介绍了Harbor私有仓库搭建并配置https对接docker与kubernetes。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、环境介绍

默认情况下,Harbor 不附带证书。可以在没有安全保护的情况下部署 Harbor,以便您可以通过 HTTP 连接到它。在生产环境中,推荐始终使用 HTTPS。要配置 HTTPS,必须创建 SSL 证书。可以使用由受信任的第三方 CA 签名的证书,也可以使用自签名证书。本文以自签名证书为例。

使用到的各个软件版本

操作系统版本:ubuntu 20.04
harbor版本:v2.5.3-797c3536
docker版本:20.10.8
kubernetets版本:1.22.2
harbor地址:192.168.10.112   域名:harbor.snow.com
master01地址:192.168.10.100 

二、仓库部署

配置主机名与hosts文件

root@harbor:~# cat /etc/hosts
127.0.0.1 localhost
192.168.10.112 harbor.snow.com
修改主机名
root@barbor:~# hostnamectl set-hostname harbor
root@harbor:~# bash
root@harbor:~# hostname
harbor

下载harbor安装包

root@harbor:~#wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz
解压harbor安装包
root@harbor:~# tar xf harbor-offline-installer-v2.5.3.tgz -C /usr/local/src/
安装docker-compose
root@harbor:~# curl -SL https://github.com/docker/compose/releases/download/v2.7.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

生成证书颁发机构证书及私钥

root@harbor:/usr/local/src/harbor/certs# openssl genrsa -out ca.key 4096
Generating RSA private key, 4096 bit long modulus (2 primes)
............................++++
............................................++++
e is 65537 (0x010001)
root@harbor:/usr/local/src/harbor/certs# openssl req -x509 -new -nodes -sha512 -days 3650 \
>  -subj "/C=CN/ST=Shanghai/L=Shanghai/O=SmartX/OU=Lab/CN=harbor.snow.com" \
>  -key ca.key \
>  -out ca.crt
root@harbor:/usr/local/src/harbor/certs# ls
ca.crt  ca.key

生成服务器私钥及证书签名请求(CSR)

root@harbor:/usr/local/src/harbor/certs# openssl genrsa -out harbor.snow.com.key 4096
Generating RSA private key, 4096 bit long modulus (2 primes)
...........++++
.....................................................................++++
e is 65537 (0x010001)

生成证书签名请求
root@harbor:/usr/local/src/harbor/certs# openssl req -sha512 -new \
> -subj "/C=CN/ST=Shanghai/L=Shanghai/O=SmartX/OU=Lab/CN=harbor.snow.com" \
> -key harbor.snow.com.key \
> -out harbor.snow.com.csr
root@harbor:/usr/local/src/harbor/certs# ls
ca.crt  ca.key  harbor.snow.com.csr  harbor.snow.com.key

生成 x509 v3 扩展文件。
无论使用 FQDN 还是 IP 地址连接到Harbor服务器,都必须创建此文件,以便keyi 为的 Harbor 主机生成符合主题备用名称 (SAN) 和 x509 v3 的证书扩展要求。

root@harbor:/usr/local/src/harbor/certs# cat v3.ext
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=harbor.snow.top
DNS.2=snow.top
DNS.3=harbor

使用该v3.ext文件为 Harbor 服务器生成证书。

root@harbor:/usr/local/src/harbor/certs# openssl x509 -req -sha512 -days 3650 \
> -extfile v3.ext \
> -CA ca.crt -CAkey ca.key -CAcreateserial \
> -in harbor.snow.com.csr \
> -out harbor.snow.com.crt
Signature ok
subject=C = CN, ST = Shanghai, L = Shanghai, O = SmartX, OU = Lab, CN = harbor.snow.com
Getting CA Private Key

将 harbor.snow.com.crt 转换为 harbor.snow.com.cert , 供 Docker 使用。Docker 守护进程将.crt文件解释为 CA 证书,.cert将文件解释为客户端证书。

root@harbor:/usr/local/src/harbor/certs# openssl x509 -inform PEM -in harbor.snow.com.crt -out harbor.snow.com.cert

配置 harbor yml文件,注意需要修改hostname与指定证书位置。

root@harbor:/usr/local/src/harbor# cat harbor.yml |more
# Configuration file of Harbor

# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: harbor.snow.com

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

# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /usr/local/src/harbor/certs/harbor.snow.com.cert
  private_key: /usr/local/src/harbor/certs/harbor.snow.com.key

安装harbor

root@harbor:/usr/local/src/harbor# ./install.sh 

[Step 0]: checking if docker is installed ...

Note: docker version: 20.10.12

[Step 1]: checking docker-compose is installed ...

Note: docker-compose version: 1.25.0

[Step 2]: loading Harbor images ...
Loaded image: goharbor/harbor-portal:v2.5.3
Loaded image: goharbor/harbor-core:v2.5.3
Loaded image: goharbor/redis-photon:v2.5.3
Loaded image: goharbor/prepare:v2.5.3
Loaded image: goharbor/harbor-db:v2.5.3
Loaded image: goharbor/chartmuseum-photon:v2.5.3
Loaded image: goharbor/harbor-jobservice:v2.5.3
Loaded image: goharbor/harbor-registryctl:v2.5.3
Loaded image: goharbor/nginx-photon:v2.5.3
Loaded image: goharbor/notary-signer-photon:v2.5.3
Loaded image: goharbor/harbor-log:v2.5.3
Loaded image: goharbor/harbor-exporter:v2.5.3
Loaded image: goharbor/registry-photon:v2.5.3
Loaded image: goharbor/notary-server-photon:v2.5.3
Loaded image: goharbor/trivy-adapter-photon:v2.5.3


[Step 3]: preparing environment ...

[Step 4]: preparing harbor configs ...
prepare base dir is set to /usr/local/src/harbor
Clearing the configuration file: /config/registryctl/env
Clearing the configuration file: /config/registryctl/config.yml
Clearing the configuration file: /config/jobservice/env
Clearing the configuration file: /config/jobservice/config.yml
Clearing the configuration file: /config/portal/nginx.conf
Clearing the configuration file: /config/nginx/nginx.conf
Clearing the configuration file: /config/log/rsyslog_docker.conf
Clearing the configuration file: /config/log/logrotate.conf
Clearing the configuration file: /config/registry/passwd
Clearing the configuration file: /config/registry/config.yml
Clearing the configuration file: /config/core/env
Clearing the configuration file: /config/core/app.conf
Clearing the configuration file: /config/db/env
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
loaded secret from file: /data/secret/keys/secretkey
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir



[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-portal ... done
Creating registryctl   ... done
Creating registry      ... done
Creating redis         ... done
Creating harbor-db     ... done
Creating harbor-core   ... done
Creating nginx             ... done
Creating harbor-jobservice ... done
✔ ----Harbor has been installed and started successfully.----

安装完harbor后验证

root@harbor:/usr/local/src/harbor# docker ps 
CONTAINER ID   IMAGE                                COMMAND                  CREATED          STATUS                    PORTS                                                                            NAMES
0304ebf0cddc   goharbor/nginx-photon:v2.5.3         "nginx -g 'daemon of…"   38 seconds ago   Up 35 seconds (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp, 0.0.0.0:443->8443/tcp, :::443->8443/tcp   nginx
10d1ff19120b   goharbor/harbor-jobservice:v2.5.3    "/harbor/entrypoint.…"   38 seconds ago   Up 36 seconds (healthy)                                                                                    harbor-jobservice
2d5e3d8877f8   goharbor/harbor-core:v2.5.3          "/harbor/entrypoint.…"   39 seconds ago   Up 38 seconds (healthy)                                                                                    harbor-core
ee6c12da89d9   goharbor/harbor-db:v2.5.3            "/docker-entrypoint.…"   43 seconds ago   Up 39 seconds (healthy)                                                                                    harbor-db
5b7455e56733   goharbor/redis-photon:v2.5.3         "redis-server /etc/r…"   43 seconds ago   Up 39 seconds (healthy)                                                                                    redis
a0c279ca0206   goharbor/harbor-registryctl:v2.5.3   "/home/harbor/start.…"   43 seconds ago   Up 41 seconds (healthy)                                                                                    registryctl
478f35433f69   goharbor/registry-photon:v2.5.3      "/home/harbor/entryp…"   43 seconds ago   Up 39 seconds (healthy)                                                                                    registry
eb33121f4126   goharbor/harbor-portal:v2.5.3        "nginx -g 'daemon of…"   43 seconds ago   Up 39 seconds (healthy)                                                                                    harbor-portal
b0c7ab3a7133   goharbor/harbor-log:v2.5.3           "/bin/sh -c /usr/loc…"   44 seconds ago   Up 43 seconds (healthy)   127.0.0.1:1514->10514/tcp                                                        harbor-log

修改宿主机hosts文件,做好harbor主机的域名解析,在浏览器使用域名访问harbor服务器发现提示(忽略域名,域名是第一次测试的时候用的)

harbor配置https,kubernetes,docker,docker,kubernetes,https
在当前页面用键盘输入thisisunsafe,不是在地址栏输入,就直接敲键盘就行了,页面即会自动刷新进入网页
harbor配置https,kubernetes,docker,docker,kubernetes,https
使用默认用户名admin,密码Harbor12345即可登录。

harbor配置https,kubernetes,docker,docker,kubernetes,https

三、配置docker客户端

将证书发送到master01服务器,注意在操作之前要先在master01创建好存放证书的文件夹/etc/docker/certs.d/harbor.snow.com

root@master01:~# mkdir -p /etc/docker/certs.d/harbor.snow.com
root@harbor:/usr/local/src/harbor/certs# scp harbor.snow.com.cert 192.168.10.100:/etc/docker/certs.d/harbor.snow.com/
root@192.168.10.100's password: 
harbor.snow.com.cert                                                                                        100% 2102     2.3MB/s   00:00      

root@harbor:/usr/local/src/harbor/certs# scp harbor.snow.com.key 192.168.10.100:/etc/docker/certs.d/harbor.snow.com/
root@192.168.10.100's password: 
harbor.snow.com.key                                                                                         100% 2045     2.0MB/s   00:00
  
root@harbor:/usr/local/src/harbor/certs# scp ca.crt  192.168.10.100:/etc/docker/certs.d/harbor.snow.com
root@192.168.10.100's password: 
ca.crt                                                                                                      100% 2045     2.0MB/s   00:00    

在master01节点看到证书已经成功发送过来

root@master01:/etc/docker/certs.d/harbor.snow.com# ls
ca.crt  harbor.snow.com.cert  harbor.snow.com.key

修改docker的daemon.json文件,使其信任harbor.snow.com仓库

root@master01:/etc/docker# cat daemon.json 
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "insecure-registries":["harbor.snow.com"],
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "http://hub-mirror.c.163.com"
  ],
  "max-concurrent-downloads": 10,
  "log-driver": "json-file",
  "log-level": "warn",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
    },
  "data-root": "/var/lib/docker"
}

重启docker服务并修改其hosts文件使master01节点能够通过域名找到对应的harbor仓库

root@master01:~# systemctl restart docker
root@master01:~# cat /etc/hosts
127.0.0.1 localhost
192.168.10.112 harbor.snow.com

登录仓库

root@master01:~# docker login harbor.snow.com
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

上传镜像进行测试,注意要提前在web页面创建好新的项目。

root@master01:~# docker images
REPOSITORY                                TAG             IMAGE ID       CREATED         SIZE
calico/cni                                v3.19.2         05bf027c9836   17 months ago   146MB

为镜像改名字
root@master01:~# docker tag calico/cni:v3.19.2 harbor.snow.com/calico/cni:v3.19.2

推送到镜像仓库
root@master01:~# docker push harbor.snow.com/calico/cni:v3.19.2
The push refers to repository [harbor.snow.com/calico/cni]
758850cabe72: Pushed 
e88fd392bb0c: Pushed 
fb44224b4823: Pushed 
v3.19.2: digest: sha256:00a619424a20d1b31c0f65d1194484aebe9f90dfc1d4d6396ce706941004860b size: 946

推送到镜像仓库后即可在web页面看到刚刚推送上去镜像
harbor配置https,kubernetes,docker,docker,kubernetes,https

镜像拉取测试

root@master01:~# docker images
REPOSITORY                                  TAG             IMAGE ID       CREATED         SIZE
kubernetesui/dashboard                      v2.3.1          e1482a24335a   18 months ago   220MB

root@master01:~# docker pull harbor.snow.com/calico/cni:v3.19.2
v3.19.2: Pulling from calico/cni
Digest: sha256:00a619424a20d1b31c0f65d1194484aebe9f90dfc1d4d6396ce706941004860b
Status: Downloaded newer image for harbor.snow.com/calico/cni:v3.19.2
harbor.snow.com/calico/cni:v3.19.2

root@master01:~# docker images
REPOSITORY                                  TAG             IMAGE ID       CREATED         SIZE
kubernetesui/dashboard                      v2.3.1          e1482a24335a   18 months ago   220MB
harbor.snow.com/calico/cni                  v3.19.2         05bf027c9836   17 months ago   146MB

四、K8S集群配置使用私有镜像仓库Harbor

在配置k8s使用harbor之前需要先完成本文的第三步,先让docker可以正常使用harbor。
在docker成功登录harbor后会在用户家目录生成一个秘钥文件

root@master01:~# cat /root/.docker/config.json
{
	"auths": {
		"harbor.snow.com": {
			"auth": "YWRtaW46SGFyYm9yMTIzNDU="
		}
	}
}

将秘钥进行base64加密

root@master01:~# cat /root/.docker/config.json | base64 -w 0
ewoJImF1dGhzIjogewoJCSJoYXJib3Iuc25vdy5jb20iOiB7CgkJCSJhdXRoIjogIllXUnRhVzQ2U0dGeVltOXlNVEl6TkRVPSIKCQl9Cgl9Cn0=

创建用户harbor认证的secret的yaml文件

root@master01:~/secret# cat registry-pull-secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: registry-pull-secret
  namespace: kube-system
data:
  .dockerconfigjson: ewoJImF1dGhzIjogewoJCSJoYXJib3Iuc25vdy5jb20iOiB7CgkJCSJhdXRoIjogIllXUnRhVzQ2U0dGeVltOXlNVEl6TkRVPSIKCQl9Cgl9Cn0=
type: kubernetes.io/dockerconfigjson

创建secret,注意secret为名称空间级别的资源,调用这个secret的yaml文件要与此secret在同一个名称空间下。

root@master01:~/secret# kubectl delete -f registry-pull-secret.yaml 
secret "registry-pull-secret" deleted
root@master01:~/secret# kubectl apply -f registry-pull-secret.yaml 
secret/registry-pull-secret created
root@master01:~/secret# kubectl get secret
NAME                   TYPE                                  DATA   AGE
default-token-rxd89    kubernetes.io/service-account-token   3      5h2m
registry-pull-secret   kubernetes.io/dockerconfigjson        1      3h

最后只需要在需要去harbor拉取镜像的pod的yaml文件中添加如下配置即可(只展示出了完成yaml文件的部分内容)注意其是与containers语句是平级的。

template:
    metadata:
      labels:
        k8s-app: metrics-server
    spec:
      imagePullSecrets:
        - name: registry-pull-secret
      containers:
        image: harbor.snow.com/base/metrics-server:v0.5.0
        imagePullPolicy: Always

配置完成后k8s集群即可成功拉取harbor中的镜像。如果在harbor中将要访问的镜像级别设置为公开,则不需要secret认证即可正常拉取。

ps:以上内容在本人实现环境中已试验成功,如发现有问题或表述不清的地方欢迎指正。文章来源地址https://www.toymoban.com/news/detail-771471.html

到了这里,关于Harbor私有仓库搭建并配置https对接docker与kubernetes的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor

    在网页中输入本机的IP地址 在网页中输入本机的IP地址加上:8080,用户名admin密码Harbor12345。登录成功页面如下:

    2024年02月11日
    浏览(31)
  • Docker本地私有仓库、harbor私有仓库部署与管理

    docker本地仓库,存放镜像,本地的机器上传和下载,pull/push。 使用私有仓库有许多优点: ①节省网络带宽,针对于每个镜像不用每个人都去中央仓库上面去下载,只需要从私有仓库中下载即可; ②提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,

    2024年02月11日
    浏览(33)
  • docker容器:本地私有仓库、harbor私有仓库部署与管理

    目录 一、本地私有仓库 1、本地私有仓库简介 2、搭建本地私有仓库 3、容器重启策略介绍 二、harbor私有仓库部署与管理 1、什么是harbor 2、Harbor的特性 3、Harbor的构成 4、harbor部署及配置 ①部署docker-compose ②部署Harbor服务 ③登录创建项目 ④登录仓库并上传镜像 5、客户端测试

    2024年02月04日
    浏览(41)
  • Docker私有仓库harbor

    目录 Docker私有仓库harbor Harbor私有仓库介绍 Harbor部署 harbor页面不显示排错思路 Harbor的使用 Harbor拉镜像 自制镜像推送到Harbor 自动构建wordpress镜像 部署项目 Harbor 是为企业用户设计的容器镜像仓库开源项目,包括了权限管理(RBAC)、LDAP、审计、安全漏洞扫描、镜像验真、管理界

    2024年02月03日
    浏览(25)
  • 基于CentOS搭建私有仓库harbor

    环境:         操作系统:CentOS Linux 7 (Core)         内核: Linux 3.10.0-1160.el7.x86_64 目录 安装搭建harbor (1)安装docker编排工具docker compose (2)下载Harbor 安装包 (3)修改配置文件 (4)添加主机映射 安装启动harbor 安装后验证 使用Harbor ①访问浏览器 ②登录 ③新建一个项目

    2024年02月11日
    浏览(34)
  • docker私有仓库harbor部署

    Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。 作为一个企业级私有Registry服务器,Harbor提供了更好的性能和安全。提升用户使用Registry构建和运行环境传输镜像的效率。

    2024年02月11日
    浏览(25)
  • Docker-harbor私有仓库

    Docker的核心:镜像、容器、仓库 仓库:保存镜像 私有:自定义用户的形式登录仓库,拉取或者上传镜像(内部管理的用户) harbor是由VMware公司开发的,开源的企业级的 docker registry项目。帮助用户快速搭建一个企业级的Docker仓库的服务。 支持中文 1、 基于角色进行控制,用户和

    2024年02月04日
    浏览(28)
  • Docker--harbor私有仓库部署与管理

    ------------------------------------ Harbor 简介 ------------------------------------ (1)什么是Harbor Harbor 是 VMware 公司开源的企业级 Docker Registry 项目,其目标是帮助用户迅速搭建一个企业级的 Docker Registry 服务。 Harbor以 Docker 公司开源的 Registry 为基础,提供了图形管理 UI 、基于角色的访问

    2024年02月08日
    浏览(35)
  • Docker本地私有仓库搭建配置指导

    因内网主机需要拉取镜像进行Docker应用,因此需要一台带外主机作为内网私有仓库来提供内外其他docker业务主机使用。参考架构如下: 相关资源:加密、Distribution registry、Create and Configure Docker Registry、Registry部署、Distribution Registry v2 authentication、docker-registry 注 :不要在已有

    2024年01月18日
    浏览(25)
  • Docker registry镜像仓库,私有仓库及harbor管理详解

    目录 registry镜像仓库概述 Docker 镜像仓库(Docker Registry): registry 容器: 私有仓库概述 搭建本地私有仓库示例 Harbor概述 harbor架构 详解构成 Harbor由容器构成 Harbor部署示例 环境准备 部署Docker-Compose服务 部署 Harbor 服务 启动Harbor 查看 Harbor 启动镜像 创建一个新项目 在其他客

    2024年01月18日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包