背景:

java程序员们想弄一个私有maven仓库,嗯 正常的是用nexus or artfactory? artfactory是两三年前听jfrog的讲座知道的,程序说他原来用的nexus。那就搞一个nexus了。
基础环境参照:https://cloud.tencent.com/developer/article/1806089—kubernetes集群1.20.5版本(当然了进行了小版本升级1.21了,系列笔记中有提)
https://cloud.tencent.com/developer/article/1806896—网关层的代理traefik
https://cloud.tencent.com/developer/article/1806549—存储块腾讯云cbs
all on kubernetes 是个人的原则。就在kubernetes的环境上搭建一个私有maven仓库了。

1. nexus3 on kubernetes

注: 不做特殊说明,工具类软件我都安装在kube-ops namespace命名空间下

1. 创建pv,pvc

嗯 存储用的都是腾讯云的cbs存储

  1. [root@sh-master-01 ~]# kubectl get storageclass
  2. NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
  3. cbs-csi com.tencent.cloud.csi.cbs Delete Immediate false 70d

cat pvc.yaml

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. namespace kube-ops
  5. name: sonatype-nexus
  6. labels:
  7. app: sonatype-nexus
  8. spec:
  9. accessModes:
  10. - ReadWriteOnce
  11. resources:
  12. requests:
  13. storage: 50Gi
  14. storageClassName: cbs-csi
  15. selector:
  16. matchLabels:
  17. app: sonatype-nexus
  1. kubectl apply -f pvc.yaml

image.png
嗯 好吧cbs-csi 不支持selector的标签….将就的用吧…腾讯一直讲自己今年的开源项目是最多的,但是如kubernetes-csi-tencentcloud这样的项目,三年了吧 提交了issue也没有关闭呢也没有人回复。所以能用就行了…还是适应它吧……

  1. kubectl delete -f pvc.yaml

cat pvc.yaml

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. namespace: kube-ops
  5. name: sonatype-nexus
  6. labels:
  7. app: sonatype-nexus
  8. spec:
  9. accessModes:
  10. - ReadWriteOnce
  11. resources:
  12. requests:
  13. storage: 50Gi
  14. storageClassName: cbs-csi
  1. kubectl apply -f pvc.yaml
  2. kubectl describe pvc sonatype-nexus -n kube-ops
  3. kubectl get pvc -n kube-ops

image.png

2、部署 Sonatype Nexus3

cat nexus.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: sonatype-nexus
  5. labels:
  6. app: sonatype-nexus
  7. spec:
  8. type: ClusterIP
  9. ports:
  10. - name: sonatype-nexus
  11. port: 8081
  12. targetPort: 8081
  13. protocol: TCP
  14. selector:
  15. app: sonatype-nexus
  16. ---
  17. apiVersion: apps/v1
  18. kind: Deployment
  19. metadata:
  20. name: sonatype-nexus
  21. labels:
  22. app: sonatype-nexus
  23. spec:
  24. replicas: 1
  25. selector:
  26. matchLabels:
  27. app: sonatype-nexus
  28. template:
  29. metadata:
  30. labels:
  31. app: sonatype-nexus
  32. spec:
  33. containers:
  34. - name: sonatype-nexus
  35. image: sonatype/nexus3:3.30.0
  36. imagePullPolicy: IfNotPresent
  37. ports:
  38. - name: server
  39. containerPort: 8081
  40. livenessProbe: #存活探针
  41. httpGet:
  42. path: /
  43. port: 8081
  44. initialDelaySeconds: 30
  45. periodSeconds: 30
  46. failureThreshold: 6
  47. readinessProbe: #就绪探针
  48. httpGet:
  49. path: /
  50. port: 8081
  51. initialDelaySeconds: 30
  52. periodSeconds: 30
  53. failureThreshold: 6
  54. env:
  55. - name: INSTALL4J_ADD_VM_PARAMS #设置分配资源大小,一定要等于或小于resources设置的值
  56. value: "
  57. -Xms1200M
  58. -Xmx1200M
  59. -XX:MaxDirectMemorySize=2G
  60. -XX:+UnlockExperimentalVMOptions
  61. -XX:+UseCGroupMemoryLimitForHeap
  62. "
  63. resources: #资源限制
  64. limits:
  65. cpu: 1000m #推荐设置为4000m以上cpu,由于资源有限,所以都是设置的最小值
  66. memory: 2048Mi
  67. requests:
  68. cpu: 500m
  69. memory: 1024Mi
  70. volumeMounts:
  71. - name: sonatype-nexus-data
  72. mountPath: /nexus-data
  73. volumes:
  74. - name: sonatype-nexus-data
  75. persistentVolumeClaim:
  76. claimName: sonatype-nexus #设置为上面创建的 PVC
  1. [root@sh-master-01 qa]# kubectl get pods -n kube-ops
  2. NAME READY STATUS RESTARTS AGE
  3. gitlab-b9d95f784-7h8dt 1/1 Running 0 49d
  4. gitlab-redis-cd56f5cc9-g9gm8 1/1 Running 0 61d
  5. jenkins-0 2/2 Running 0 49d
  6. postgresql-5bd6b44d45-wzkwr 1/1 Running 1 61d
  7. sonatype-nexus-5d98d78b86-nk75v 0/1 CrashLoopBackOff 6 9m5s

查看报错如下:
image.png
嗯权限不够 咋整….嗯 由于pvc只能挂载单个pod,先执行:

  1. kubectl delete -f nexus.yaml -n kube-ops

然后修改nexus.yaml如下:
cat nexus.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: sonatype-nexus
  5. labels:
  6. app: sonatype-nexus
  7. spec:
  8. type: ClusterIP
  9. ports:
  10. - name: sonatype-nexus
  11. port: 8081
  12. targetPort: 8081
  13. protocol: TCP
  14. selector:
  15. app: sonatype-nexus
  16. ---
  17. apiVersion: apps/v1
  18. kind: Deployment
  19. metadata:
  20. name: sonatype-nexus
  21. labels:
  22. app: sonatype-nexus
  23. spec:
  24. replicas: 1
  25. selector:
  26. matchLabels:
  27. app: sonatype-nexus
  28. template:
  29. metadata:
  30. labels:
  31. app: sonatype-nexus
  32. spec:
  33. initContainers:
  34. - name: init
  35. image: busybox
  36. command: ["sh", "-c", "chown -R 200:200 /nexus-data"]
  37. volumeMounts:
  38. - name: sonatype-nexus-data
  39. mountPath: /nexus-data
  40. containers:
  41. - name: sonatype-nexus
  42. image: sonatype/nexus3:3.30.0
  43. imagePullPolicy: IfNotPresent
  44. ports:
  45. - name: server
  46. containerPort: 8081
  47. livenessProbe: #存活探针
  48. httpGet:
  49. path: /
  50. port: 8081
  51. initialDelaySeconds: 30
  52. periodSeconds: 30
  53. failureThreshold: 6
  54. readinessProbe: #就绪探针
  55. httpGet:
  56. path: /
  57. port: 8081
  58. initialDelaySeconds: 30
  59. periodSeconds: 30
  60. failureThreshold: 6
  61. env:
  62. - name: INSTALL4J_ADD_VM_PARAMS #设置分配资源大小,一定要等于或小于resources设置的值
  63. value: "
  64. -Xms1200M
  65. -Xmx1200M
  66. -XX:MaxDirectMemorySize=2G
  67. -XX:+UnlockExperimentalVMOptions
  68. -XX:+UseCGroupMemoryLimitForHeap
  69. "
  70. resources: #资源限制
  71. limits:
  72. cpu: 1000m #推荐设置为4000m以上cpu,由于资源有限,所以都是设置的最小值
  73. memory: 2048Mi
  74. requests:
  75. cpu: 500m
  76. memory: 1024Mi
  77. volumeMounts:
  78. - name: sonatype-nexus-data
  79. mountPath: /nexus-data
  80. volumes:
  81. - name: sonatype-nexus-data
  82. persistentVolumeClaim:
  83. claimName: sonatype-nexus #设置为上面创建的 PVC
  1. [root@sh-master-01 nexus]# kubectl apply -f nexus.yaml -n kube-ops
  2. service/sonatype-nexus created
  3. deployment.apps/sonatype-nexus created
  4. [root@sh-master-01 nexus]# kubectl get pods -n kube-ops
  5. NAME READY STATUS RESTARTS AGE
  6. gitlab-b9d95f784-7h8dt 1/1 Running 0 49d
  7. gitlab-redis-cd56f5cc9-g9gm8 1/1 Running 0 61d
  8. jenkins-0 2/2 Running 0 49d
  9. postgresql-5bd6b44d45-wzkwr 1/1 Running 1 61d
  10. sonatype-nexus-79f85cc57c-scb9b 0/1 Init:0/1 0 28s
  11. [root@sh-master-01 nexus]# kubectl get pods -n kube-ops
  12. NAME READY STATUS RESTARTS AGE
  13. gitlab-b9d95f784-7h8dt 1/1 Running 0 49d
  14. gitlab-redis-cd56f5cc9-g9gm8 1/1 Running 0 61d
  15. jenkins-0 2/2 Running 0 49d
  16. postgresql-5bd6b44d45-wzkwr 1/1 Running 1 61d
  17. sonatype-nexus-79f85cc57c-scb9b 0/1 PodInitializing 0 2m
  1. kubectl describe pods sonatype-nexus-79f85cc57c-scb9b -n kube-ops

image.png
嗯可以running了
然后获取一下用户名 密码:
image.png

3. ingress代理对外暴露应用

做一个ingress 代理?
cat ingress.yaml

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: nexus-ingress
  5. namespace: kube-ops
  6. annotations:
  7. nginx.ingress.kubernetes.io/rewrite-target: /
  8. kubernetes.io/ingress.class: traefik
  9. traefik.ingress.kubernetes.io/router.entrypoints: web
  10. nginx.ingress.kubernetes.io/ssl-redirect: 'true'
  11. spec:
  12. rules:
  13. - host: nexus.sainaihe.com
  14. http:
  15. paths:
  16. - pathType: Prefix
  17. path: /
  18. backend:
  19. service:
  20. name: sonatype-nexus
  21. port:
  22. number: 8081
  1. kubectl apply -f ingress.yaml

4. 浏览器访问 nexus服务,并修改nexus初始密码:

image.png
image.png
嗯跨域了可咋整? 我的两个主域名都泛域名强制跳转https了,短时间没有想好怎么解决….我就直接用了另外一个单独域名。不强跳可以直接访问了。同理我是不是可以加一个https的单独的设置….有时间了再试一下。先跑通一下nexus的代理应用……
http访问:
如下。第一次是要修改密码的关于初始密码的获取可以参照:1.2中获取初始密码的方式
image.png
嗯 对了呢 记得关闭匿名访问。anonymous

2. 添加一个aliyun maven代理跑一下

1. 添加一个aliyun maven 代理

打开 Repositories->Create repository->maven2(proxy) 并设置要代理的 Maven 仓库名称与地址image.png
image.png
设置“仓库名称”与“仓库地址”。

image.png
保存上面设置后回到仓库页面,可以看到已经添加了一个新的仓库 aliyun.
image.png

2. 设置aliyun maven优先级

打开 Repositories->maven public 并设置代理仓库优先级置顶
image.png
image.png

3. 本地maven私服仓库配置

设置 maven 的 Settings.xml 文件,按照下面配置进行设置私服地址和验证的用户名、密码。
image.png
image.png

3 .创建一个maven项目测试

1. 拉取测试

随手打开一个idea项目添加了一个进行拉取测试
image.png
更新maven项目:
image.png
ok如下可以从个人配置的maven代理仓库更新了!
738e0e02b23bd69e69445888124b00b.png

2. 推送设置

我是盗用了下程序的ava maven项目,pom.xml添加如下配置:

  1. <distributionManagement>
  2. <!-- Maven 上传设置 -->
  3. <repository>
  4. <id>nexus</id> <!-- 保持和Settings.xml中配置的Server ID一致 -->
  5. <name>releases</name>
  6. <url>http://http://nexus.xxx.com//repository/maven-releases/</url> <!-- 推送到Maven仓库的maven-releases下 -->
  7. </repository>
  8. </distributionManagement>

.当然了仓库自己新建了两个:zhangpeng-releases对应release
zhangpeng-snapshots 对应snapshots
image.png

mvn deploy打包:
image.png
登陆nexus:
image.png
嗯对我来说这就算是成功了……

总结:

1. 腾讯云开源的cbs组件不支持selector。

2. 当pv,pvc需要运行权限时候可以使用initContainers的方式,执行脚本命令。

3. 特别鸣谢豆丁大佬-http://www.mydlq.club/article/26