基于NFS后端存储搭建Harbor

序言

  • 硬件环境:Virtualbox内建虚拟机
  • OS环境:ubuntu18.04
  • K8s集群环境:单节点的all-in-one环境

1. NFS服务器的搭建

# nfs软件包安装
apt-get update
apt-get install nfs-common nfs-kernel-server -y

# 创建共享目录且设置权限
mkdir -p /data/k8s
chmod -R 777 /data/k8s

# 挂载配置
cat << EOF >> /etc/exports
>/data/k8s *(rw,sync,no_root_squash)
>EOF

# nfs-server设置开机启动并开启服务
systemctl enable nfs-kernel-server
systemctl start nfs-kernel-server

2. 安装K8s NFS后端存储驱动插件

下文的安装的Harbor采用NFS作为后端存储,所以首先需要在K8s集群安装对应的驱动的插件nfs-client-provisioner,这儿采用helm安装。

# 添加NFS helm仓库
helm repo add github https://kubernetes-charts.storage.googleapis.com/

# 安装nfs-client-provisioner
helm install github/nfs-client-provisioner --set nfs.server=10.0.2.15 --set nfs.path=/data/k8s

# 确认安装
helm list
kubectl get pod | grep nfs-client-provisioner

3. Helm安装Harbor

3.1 下载Harbor Helm安装包

# 这里下载的版本是1.0.0
git clone -b 1.0.0 https://github.com/goharbor/harbor-helm.git

3.2 修改values.yaml

首先进入到harbor-helm目录修改values.yaml文件。

修改主要涉及到两块:

  • 服务暴露的类型,这里我选用nodePort类型;
  • 持久化配置,这里采用基于NFS作为后端存储的动态创建PV的方式;
expose:
  # Set the way how to expose the service. Set the type as "ingress",
  # "clusterIP" or "nodePort" and fill the information in the corresponding
  # section
  type: nodePort
  tls:
    # Enable the tls or not. Note: if the type is "ingress" and the tls
    # is disabled, the port must be included in the command when pull/push
    # images. Refer to https://github.com/goharbor/harbor/issues/5291
    # for the detail.
    enabled: false
    # Fill the name of secret if you want to use your own TLS certificate
    # and private key. The secret must contain keys named tls.crt and
    # tls.key that contain the certificate and private key to use for TLS
    # The certificate and private key will be generated automatically if
    # it is not set
    secretName: ""
    # By default, the Notary service will use the same cert and key as
    # described above. Fill the name of secret if you want to use a
    # separated one. Only needed when the type is "ingress".
    notarySecretName: ""
    # The commmon name used to generate the certificate, it's necessary
    # when the type is "clusterIP" or "nodePort" and "secretName" is null
    commonName: ""
...
# The external URL for Harbor core service. It is used to
# 1) populate the docker/helm commands showed on portal
# 2) populate the token service URL returned to docker/notary client
#
# Format: protocol://domain[:port]. Usually:
# 1) if "expose.type" is "ingress", the "domain" should be
# the value of "expose.ingress.hosts.core"
# 2) if "expose.type" is "clusterIP", the "domain" should be
# the value of "expose.clusterIP.name"
# 3) if "expose.type" is "nodePort", the "domain" should be
# the IP address of k8s node
#
# If Harbor is deployed behind the proxy, set it as the URL of proxy
externalURL: http://10.0.2.15:30002
...
# The persistence is enabled by default and a default StorageClass
# is needed in the k8s cluster to provision volumes dynamicly.
# Specify another StorageClass in the "storageClass" or set "existingClaim"
# if you have already existing persistent volumes to use
#
# For storing images and charts, you can also use "azure", "gcs", "s3",
# "swift" or "oss". Set it in the "imageChartStorage" section
persistence:
  enabled: true
  # Setting it to "keep" to avoid removing PVCs during a helm delete
  # operation. Leaving it empty will delete PVCs after the chart deleted
  resourcePolicy: "keep"
  persistentVolumeClaim:
    registry:
      # Use the existing PVC which must be created manually before bound
      existingClaim: ""
      # Specify the "storageClass" used to provision the volume. Or the default
      # StorageClass will be used(the default).
      # Set it to "-" to disable dynamic provisioning
      storageClass: "nfs-client"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 5Gi
    chartmuseum:
      existingClaim: ""
      storageClass: "nfs-client"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 5Gi
    jobservice:
      existingClaim: ""
      storageClass: "nfs-client"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 1Gi
    # If external database is used, the following settings for database will
    # be ignored
    database:
      existingClaim: ""
      storageClass: "nfs-client"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 1Gi
    # If external Redis is used, the following settings for Redis will
    # be ignored
    redis:
      existingClaim: ""
      storageClass: "nfs-client"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 1Gi
...

几点说明

  • 其中externalURL设置时必须标注协议名称比如http、https等,IP地址是节点的IP,端口可设可不设,默认端口号30002。

  • 持久化配置只需写入对应的storageClassName名称,而实际的sc在安装nfs-client-provisioner插件时已经创建好了,可通过如下命令来查看填入即可:
    kebectl get sc

3.3 安装Harbor

安装之前不妨检查下配置及相关模板是否有效:

cd harbor-helm
helm install --dry-run --debug .

检查没问题后开始安装Harbor:

helm install --name my-harbor .

需要等待一会儿,因为要下载docker镜像,或者我们可以通过命令watch "kubectl get pod"来随时监看harbor pod的创建状态,出现如下结果基本上就代表安装成功了。

root@vinefu-dev:~/harbor-helm# kubectl get pod | grep harbor
my-harbor-harbor-adminserver-dd6c47cd4-h7lvg            1/1     Running     0          3h35m
my-harbor-harbor-chartmuseum-78dd845fbd-dmvl6           1/1     Running     0          3h35m
my-harbor-harbor-clair-85cbd7d6bd-8p8kg                 1/1     Running     2          3h35m
my-harbor-harbor-core-566d4655f5-dbntl                  1/1     Running     0          3h35m
my-harbor-harbor-database-0                             1/1     Running     0          3h35m
my-harbor-harbor-jobservice-ffb4b5859-6zh66             1/1     Running     1          3h35m
my-harbor-harbor-nginx-6cf87f6787-pwlnx                 1/1     Running     0          3h35m
my-harbor-harbor-notary-server-65749ddc8d-rsrwz         1/1     Running     0          3h35m
my-harbor-harbor-notary-signer-676b895677-z6ncj         1/1     Running     0          3h35m
my-harbor-harbor-portal-69f6f6cd76-dm6wf                1/1     Running     0          3h35m
my-harbor-harbor-redis-0                                1/1     Running     0          3h35m
my-harbor-harbor-registry-58f4d56b5c-v2xwh              2/2     Running     0          3h35m
root@vinefu-dev:~/harbor-helm#
root@vinefu-dev:~/harbor-helm# kubectl get svc | grep harbor
harbor                           NodePort    10.99.15.249     <none>        80:30002/TCP,4443:30004/TCP   3h39m
my-harbor-harbor-adminserver     ClusterIP   10.103.68.151    <none>        80/TCP                        3h39m
my-harbor-harbor-chartmuseum     ClusterIP   10.102.192.58    <none>        80/TCP                        3h39m
my-harbor-harbor-clair           ClusterIP   10.100.0.197     <none>        6060/TCP                      3h39m
my-harbor-harbor-core            ClusterIP   10.101.122.30    <none>        80/TCP                        3h39m
my-harbor-harbor-database        ClusterIP   10.101.75.97     <none>        5432/TCP                      3h39m
my-harbor-harbor-jobservice      ClusterIP   10.99.153.78     <none>        80/TCP                        3h39m
my-harbor-harbor-notary-server   ClusterIP   10.108.250.157   <none>        4443/TCP                      3h39m
my-harbor-harbor-notary-signer   ClusterIP   10.110.38.62     <none>        7899/TCP                      3h39m
my-harbor-harbor-portal          ClusterIP   10.110.83.54     <none>        80/TCP                        3h39m
my-harbor-harbor-redis           ClusterIP   10.107.51.162    <none>        6379/TCP                      3h39m
my-harbor-harbor-registry        ClusterIP   10.99.48.247     <none>        5000/TCP,8080/TCP             3h39m
root@vinefu-dev:~/harbor-helm#

然后我们就可以在浏览器输入http://10.0.2.15:30002来访问Harbor UI了,其中用户名及密码分别是默认的admin和Harbor12345。

因为我在VirtualBox网络设置做了NAT本地转发,所以我在本地浏览器就能通过127.0.0.1:30002访问啦。

4. Harbor的简单使用

4.1 上传docker镜像

大家也许已经注意到,在更改values.yaml文件配置时我设置了tls.enabled=false,如此的话在上传镜像时就无须证书形式的了,不过还是更新一下docker的设置(因为docker上传镜像默认走https,这儿需要不安全访问的设置):


其中,上图中红色标记区域就是要添加的配置,之后重启docker,运行如下命令即可:

systemctl restart docker

首次上传镜像还需要harbor镜像仓库的登记:

docker login http://10.0.2.15:30002
# 根据提示输入对应的用户名和密码:admin和Harbor12345

然后回到Harbor UI,新建一个项目,假设名称为vienfu-test-repo,其中访问级别设置成public,下面我们就可以在后台上传镜像了:

# 假设上传本地的docker镜像:busybox
docker tag busybox http://10.0.2.15:30002/vienfu-test-repo/busybox
docker push http://10.0.2.15:30002/vienfu-test-repo/busybox

在Harbor UI的操作界面,点开项目vienfu-test-repo即可看到之前上传的镜像。

4.2 上传Chart

本身Harbor操作界面是提供上传Helm Chart的功能的:

但是对于开发者或者操作人员直接操作界面可能不是很方便,下面提供一种后台CLI命令上传Helm Chart的方法。

首先需要安装Helm push插件:

helm plugin install https://github.com/chartmuseum/helm-push

然后就可以上传啦

# 添加helm仓库
helm repo add my-repo http://10.0.2.15:30002/chartrepo/vienfu-test-repo
helm repo list
# 把提前新建好的helm chart上传至harbor
helm push mychart-0.1.0.tgz my-repo --username admin --password Harbor12345
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349