K8s环境下Docker+Harbor私有仓库搭建与SpringBoot部署指南

一、环境准备与前置条件

1.1 Kubernetes集群基础要求

在部署Harbor私有镜像仓库前,需确保K8s集群满足以下条件:

  • 版本兼容性:K8s版本需≥1.16,推荐使用最新稳定版(如1.28+),以兼容最新容器运行时接口(CRI)
  • 存储类配置:需预先配置动态存储类(如NFS、Ceph或云存储),用于Harbor持久化数据存储
  • 网络策略:确保集群内Pod间通信畅通,并配置Ingress或NodePort暴露Harbor服务
  • 资源配额:建议为Harbor分配至少4核CPU、8GB内存及50GB存储空间

1.2 Docker环境配置

在K8s节点上需安装Docker引擎(版本≥19.03),并配置镜像加速:

  1. # 编辑/etc/docker/daemon.json添加国内镜像源
  2. {
  3. "registry-mirrors": ["https://registry.docker-cn.com"]
  4. }
  5. # 重启服务
  6. systemctl restart docker

二、Harbor私有镜像仓库部署

2.1 Harbor安装方式选择

Harbor支持Helm Chart和离线包两种安装方式,推荐使用Helm实现自动化部署:

  1. # 添加Harbor Helm仓库
  2. helm repo add harbor https://helm.goharbor.io
  3. helm repo update
  4. # 创建命名空间
  5. kubectl create ns harbor

2.2 Helm部署参数配置

通过values.yaml自定义部署参数,关键配置项如下:

  1. expose:
  2. type: ingress
  3. tls:
  4. enabled: true
  5. certSource: secret
  6. secret:
  7. secretName: "harbor-tls"
  8. namespace: "harbor"
  9. ingress:
  10. hosts:
  11. - core: harbor.example.com
  12. annotations:
  13. nginx.ingress.kubernetes.io/proxy-body-size: "0"
  14. persistence:
  15. persistentVolumeClaim:
  16. registry:
  17. storageClass: "nfs-client"
  18. accessMode: ReadWriteOnce
  19. size: 50Gi

2.3 部署与验证

执行部署命令并验证服务状态:

  1. helm install harbor harbor/harbor -f values.yaml -n harbor
  2. kubectl get pods -n harbor
  3. # 等待所有Pod状态变为Running后,访问https://harbor.example.com

三、Docker镜像管理实践

3.1 镜像推送配置

在开发机上配置Docker信任Harbor证书:

  1. # 将Harbor的ca.crt复制到/etc/docker/certs.d/harbor.example.com目录
  2. mkdir -p /etc/docker/certs.d/harbor.example.com
  3. scp harbor-server:/path/to/ca.crt /etc/docker/certs.d/harbor.example.com/
  4. systemctl restart docker

3.2 镜像构建与推送流程

以SpringBoot应用为例,演示完整流程:

  1. Dockerfile编写

    1. FROM openjdk:17-jdk-slim
    2. VOLUME /tmp
    3. ARG JAR_FILE=target/*.jar
    4. COPY ${JAR_FILE} app.jar
    5. ENTRYPOINT ["java","-jar","/app.jar"]
  2. 构建镜像

    1. mvn clean package
    2. docker build -t harbor.example.com/library/springboot-demo:v1 .
  3. 登录并推送

    1. docker login harbor.example.com
    2. docker push harbor.example.com/library/springboot-demo:v1

四、K8s中部署SpringBoot应用

4.1 部署清单编写

创建deployment.yaml文件:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: springboot-demo
  5. spec:
  6. replicas: 2
  7. selector:
  8. matchLabels:
  9. app: springboot
  10. template:
  11. metadata:
  12. labels:
  13. app: springboot
  14. spec:
  15. containers:
  16. - name: springboot
  17. image: harbor.example.com/library/springboot-demo:v1
  18. ports:
  19. - containerPort: 8080
  20. resources:
  21. requests:
  22. cpu: "500m"
  23. memory: "512Mi"
  24. limits:
  25. cpu: "1000m"
  26. memory: "1Gi"

4.2 服务暴露与访问

通过Service和Ingress暴露应用:

  1. # service.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: springboot-service
  6. spec:
  7. selector:
  8. app: springboot
  9. ports:
  10. - protocol: TCP
  11. port: 80
  12. targetPort: 8080
  13. ---
  14. # ingress.yaml
  15. apiVersion: networking.k8s.io/v1
  16. kind: Ingress
  17. metadata:
  18. name: springboot-ingress
  19. annotations:
  20. nginx.ingress.kubernetes.io/rewrite-target: /
  21. spec:
  22. rules:
  23. - host: demo.example.com
  24. http:
  25. paths:
  26. - path: /
  27. pathType: Prefix
  28. backend:
  29. service:
  30. name: springboot-service
  31. port:
  32. number: 80

五、高级配置与优化

5.1 镜像拉取策略优化

在Deployment中配置imagePullPolicy:

  1. spec:
  2. containers:
  3. - name: springboot
  4. image: harbor.example.com/library/springboot-demo:v1
  5. imagePullPolicy: IfNotPresent # 避免每次启动都拉取镜像

5.2 Harbor镜像自动清理

配置Harbor的垃圾回收机制:

  1. 在Harbor管理界面设置存储配额
  2. 配置保留策略(如保留最近3个版本)
  3. 手动执行垃圾回收:
    1. kubectl exec -n harbor harbor-core-xxx -- /harbor/gc.sh

5.3 安全加固建议

  • 启用Harbor的项目权限控制,限制镜像推送权限
  • 配置镜像签名验证,防止篡改
  • 定期轮换访问密钥,使用K8s Secret管理凭证

六、故障排查与常见问题

6.1 镜像推送失败处理

现象x509: certificate signed by unknown authority
解决方案

  1. 检查客户端是否配置了正确的CA证书
  2. 验证Harbor的Ingress TLS配置是否正确
  3. 使用docker pull测试基础镜像是否能正常拉取

6.2 Pod启动失败排查

步骤

  1. 查看Pod事件:kubectl describe pod <pod-name>
  2. 检查镜像是否存在:kubectl get secret regcred -o yaml
  3. 验证存储卷挂载:kubectl exec -it <pod-name> -- df -h

七、总结与最佳实践

  1. 镜像管理:建议按项目划分Harbor命名空间,实施分级存储策略
  2. CI/CD集成:将Harbor集成到Jenkins/GitLab CI流水线中,实现自动化镜像构建与推送
  3. 监控告警:通过Prometheus监控Harbor存储使用率,设置阈值告警
  4. 备份策略:定期备份Harbor数据库和存储卷,建议使用Velero进行集群级备份

通过本指南的实施,企业可构建安全、高效的私有镜像仓库,实现SpringBoot应用在K8s环境中的标准化部署,为微服务架构提供可靠的容器镜像管理基础设施。