harbor高可用可查看《kubernetes部署高可用Harbor》
一、helm搭建harbor
1.安装helm
1.1.安装helm客户端
tar -zxvf helm-v2.14.3-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/
chmod +x /usr/local/bin/helm
helm version
1.2.安装helm tiller(server)端
1.2.1.为应用程序设置serviceaccount
kubectl create -f helm-rbac-config.yaml
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
1.2.2.安装tiller
helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.3 --stable-repo-url [https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts](https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts)
1.2.3.卸载Helm镜像等k8s资源
kubectl get -n kube-system secrets,sa,clusterrolebinding -o name|grep tiller|xargs kubectl -n kube-system delete kubectl get all -n kube-system -l app=helm -o name|xargs kubectl delete -n kube-system
2.安装Harbor
下载 harbor-helm
git clone https://github.com/goharbor/harbor-helm.git
cd XXX/harbor-helm
2.1.设置value.yml
更改挂载卷,为nfs
sed -i 's/storageClass: ""/storageClass: "nfs"/g' values.yaml
更改挂载卷大小/size
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,
# and specify the "subPath" if the PVC is shared with other components
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"
subPath: ""
accessMode: ReadWriteOnce
size: 5Gi
chartmuseum:
existingClaim: ""
storageClass: "nfs"
subPath: ""
accessMode: ReadWriteOnce
size: 5Gi
更改Harbor暴露方式和域名
ingress:
hosts:
core: core.harbor.domain
notary: notary.harbor.domain
externalURL: https://core.harbor.domain
更改Harbor密码
harborAdminPassword: "Harbor12345"
若为非存储卷,创建pv
for i in {1..5}; do
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv00${i}
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteOnce #需要注意
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /volume1/harbor/nfs${i}
server: 10.8.4.133
EOF
done
2.2.安装harbor并将日志写入文件,可编辑文件保留.yaml编排文件,以便以后使用
helm install . --debug --name hub |sed 'w harbor.yaml'
2.3.卸载Harbor
helm del hub --purge
kubectl get pod,pv,pvc -o name|grep hub |xargs kubectl delete
2.4.若Pod未启动成功
2.4.1.查看hub-harbor-database-0 日志
kubectl logs -f hub-harbor-database-0
或
kubectl logs hub-harbor-database-0 -p

若Pod启动失败,报找不到以上database的错误,是有db还未启动成功,在Pod探针失败的情况下强制重启了Pod,导致3个Database脚本没有执行完毕,所以加长探针开始扫描时间和超时时间。(这个问题遇到的的确很坑)
kubectl delete statefulset hub-harbor-database
更改harbor.yaml中图片部分hub-harbor-database的编排文件的initialDelaySeconds、periodSeconds两个属性

kubectl applay -f harbor.yaml
等待Pod全部启动完成如图

2.5.Harbor访问及使用
查看ingress
kubectl get ingress
如图配置域名


访问<u>https://core.harbor.agree</u> ,如图所示

3.配置docker访问密匙
kubectl get secrets/hub-harbor-ingress -o jsonpath="{.data.ca\.crt}" | base64 --decode
如图

mkdir /etc/docker/certs.d/core.harbor.agree -p
kubectl get secrets/hub-harbor-ingress -o jsonpath="{.data.ca\.crt}" | base64 --decode|sed 'w /etc/docker/certs.d/core.harbor.agree/ca.crt'
docker login core.harbor.agree
初次登陆 用户名为admin 密码默认为Harbor12345

创建项目如图

docker push core.harbor.agree/dev/busybox:1.27
至此Harbor安装完成。
4.配置Harbor同步管理
其他区域服务器同上部署Harbor仓库
仓库管理-新建目标,如图,此时目标URL不可访问,请增加如下配置

执行
kubectl edit cm coredns -n kube-system

如图增加:
hosts {
10.8.4.131 core.harbor.agree
fallthrough
}
hosts与/etc/hosts配置的ingress一致。
测试连接,如图,点击确定

选择同步管理-新建规则,如图:

规则:
1、从目标服务器拉取镜像/推送镜像
2、同步以名称、tag、资源过滤后的镜像
3、选择目标服务器
4、选择目标服务器Harbor的Namespace
5、选择触发模式:
l 手动触发
l 定时:
从一日开始,每月执行一次:0 0 0 0 1/1 ?
本月最后一天执行:0 0 0 L ? ?
每周周六凌晨执行:0 0 0 0 0 6 *
定时为Cron表达式,可在<u>http://cron.qqe2.com/</u>线生成
确认后,生产规则
选择规则,点击同步,如图:

如果失败,请进入同步任务,查看具体日志。


由于完整的k8s搭建Harbor资料很少,作者也入了不少坑,如若本篇文章对大家有所帮助,希望留下您的喜欢
网友评论