关于kubernetes:Kubernetes-中部署-Maven-私有仓库-Sonatype-Nexus3

4次阅读

共计 6568 个字符,预计需要花费 17 分钟才能阅读完成。

背景:

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 存储

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

cat pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace:kube-ops
  name: sonatype-nexus
  labels:
    app: sonatype-nexus
  
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  storageClassName: cbs-csi
  selector:
    matchLabels:
      app: sonatype-nexus
kubectl apply -f pvc.yaml


嗯 好吧 cbs-csi 不反对 selector 的标签 …. 将就的用吧 … 腾讯始终讲本人往年的开源我的项目是最多的,然而如 kubernetes-csi-tencentcloud 这样的我的项目,三年了吧 提交了 issue 也没有敞开呢也没有人回复。所以能用就行了 … 还是适应它吧 ……

kubectl delete -f pvc.yaml 

cat pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: kube-ops
  name: sonatype-nexus
  labels:
    app: sonatype-nexus
  
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  storageClassName: cbs-csi
kubectl apply -f pvc.yaml
kubectl describe pvc sonatype-nexus -n kube-ops
kubectl get pvc -n kube-ops

2、部署 Sonatype Nexus3

cat nexus.yaml

apiVersion: v1
kind: Service
metadata:
  name: sonatype-nexus
  labels:
    app: sonatype-nexus
spec:
  type: ClusterIP
  ports:
  - name: sonatype-nexus
    port: 8081
    targetPort: 8081
    protocol: TCP
  selector:
    app: sonatype-nexus
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonatype-nexus
  labels:
    app: sonatype-nexus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonatype-nexus
  template:
    metadata:
      labels:
        app: sonatype-nexus
    spec:
      containers:
      - name: sonatype-nexus
        image: sonatype/nexus3:3.30.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: server
          containerPort: 8081
        livenessProbe:          #存活探针
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 30
          periodSeconds: 30
          failureThreshold: 6
        readinessProbe:         #就绪探针
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 30
          periodSeconds: 30
          failureThreshold: 6
        env:
        - name: INSTALL4J_ADD_VM_PARAMS  #设置分配资源大小,肯定要等于或小于 resources 设置的值
          value: "
                  -Xms1200M 
                  -Xmx1200M 
                  -XX:MaxDirectMemorySize=2G 
                  -XX:+UnlockExperimentalVMOptions 
                  -XX:+UseCGroupMemoryLimitForHeap
                 "
        resources:      #资源限度
          limits:
            cpu: 1000m  #举荐设置为 4000m 以上 cpu,因为资源无限,所以都是设置的最小值
            memory: 2048Mi   
          requests:
            cpu: 500m
            memory: 1024Mi
        volumeMounts:
        - name: sonatype-nexus-data
          mountPath: /nexus-data
      volumes:
      - name: sonatype-nexus-data
        persistentVolumeClaim:
          claimName: sonatype-nexus     #设置为下面创立的 PVC
[root@sh-master-01 qa]# kubectl get pods -n kube-ops
NAME                              READY   STATUS             RESTARTS   AGE
gitlab-b9d95f784-7h8dt            1/1     Running            0          49d
gitlab-redis-cd56f5cc9-g9gm8      1/1     Running            0          61d
jenkins-0                         2/2     Running            0          49d
postgresql-5bd6b44d45-wzkwr       1/1     Running            1          61d
sonatype-nexus-5d98d78b86-nk75v   0/1     CrashLoopBackOff   6          9m5s

查看报错如下:

嗯权限不够 咋整 …. 嗯 因为 pvc 只能挂载单个 pod, 先执行:

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

而后批改 nexus.yaml 如下:
cat nexus.yaml

apiVersion: v1
kind: Service
metadata:
  name: sonatype-nexus
  labels:
    app: sonatype-nexus
spec:
  type: ClusterIP
  ports:
  - name: sonatype-nexus
    port: 8081
    targetPort: 8081
    protocol: TCP
  selector:
    app: sonatype-nexus
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonatype-nexus
  labels:
    app: sonatype-nexus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonatype-nexus
  template:
    metadata:
      labels:
        app: sonatype-nexus
    spec:
      initContainers:
      - name: init
        image: busybox
        command: ["sh", "-c", "chown -R 200:200 /nexus-data"]
        volumeMounts:
        - name: sonatype-nexus-data
          mountPath: /nexus-data
      containers:
      - name: sonatype-nexus
        image: sonatype/nexus3:3.30.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: server
          containerPort: 8081
        livenessProbe:          #存活探针
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 30
          periodSeconds: 30
          failureThreshold: 6
        readinessProbe:         #就绪探针
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 30
          periodSeconds: 30
          failureThreshold: 6
        env:
        - name: INSTALL4J_ADD_VM_PARAMS  #设置分配资源大小,肯定要等于或小于 resources 设置的值
          value: "
                  -Xms1200M 
                  -Xmx1200M 
                  -XX:MaxDirectMemorySize=2G 
                  -XX:+UnlockExperimentalVMOptions 
                  -XX:+UseCGroupMemoryLimitForHeap
                 "
        resources:      #资源限度
          limits:
            cpu: 1000m  #举荐设置为 4000m 以上 cpu,因为资源无限,所以都是设置的最小值
            memory: 2048Mi   
          requests:
            cpu: 500m
            memory: 1024Mi
        volumeMounts:
        - name: sonatype-nexus-data
          mountPath: /nexus-data
      volumes:
      - name: sonatype-nexus-data
        persistentVolumeClaim:
          claimName: sonatype-nexus     #设置为下面创立的 PVC

[root@sh-master-01 nexus]# kubectl apply -f nexus.yaml -n kube-ops
service/sonatype-nexus created
deployment.apps/sonatype-nexus created
[root@sh-master-01 nexus]# kubectl get pods -n kube-ops
NAME                              READY   STATUS     RESTARTS   AGE
gitlab-b9d95f784-7h8dt            1/1     Running    0          49d
gitlab-redis-cd56f5cc9-g9gm8      1/1     Running    0          61d
jenkins-0                         2/2     Running    0          49d
postgresql-5bd6b44d45-wzkwr       1/1     Running    1          61d
sonatype-nexus-79f85cc57c-scb9b   0/1     Init:0/1   0          28s
[root@sh-master-01 nexus]# kubectl get pods -n kube-ops
NAME                              READY   STATUS            RESTARTS   AGE
gitlab-b9d95f784-7h8dt            1/1     Running           0          49d
gitlab-redis-cd56f5cc9-g9gm8      1/1     Running           0          61d
jenkins-0                         2/2     Running           0          49d
postgresql-5bd6b44d45-wzkwr       1/1     Running           1          61d
sonatype-nexus-79f85cc57c-scb9b   0/1     PodInitializing   0          2m
kubectl describe pods sonatype-nexus-79f85cc57c-scb9b -n kube-ops


嗯能够 running 了
而后获取一下用户名 明码:

3. ingress 代理对外裸露利用

做一个 ingress 代理?
cat ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nexus-ingress
  namespace: kube-ops
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: / 
    kubernetes.io/ingress.class: traefik  
    traefik.ingress.kubernetes.io/router.entrypoints: web
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'

spec:
  rules:
  - host: nexus.sainaihe.com
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: sonatype-nexus
            port:
              number: 8081

kubectl apply -f ingress.yaml

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



嗯跨域了可咋整?我的两个主域名都泛域名强制跳转 https 了,短时间没有想好怎么解决 …. 我就间接用了另外一个独自域名。不强跳能够间接拜访了。同理我是不是能够加一个 https 的独自的设置 …. 有工夫了再试一下。先跑通一下 nexus 的代理利用 ……
http 拜访:
如下。第一次是要批改明码的对于初始密码的获取能够参照:1.2 中获取初始密码的形式

嗯 对了呢 记得敞开匿名拜访。anonymous

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

1. 增加一个 aliyun maven 代理

关上 Repositories->Create repository->maven2(proxy) 并设置要代理的 Maven 仓库名称与地址

设置“仓库名称”与“仓库地址”。

  • aliyun 仓库地址:http://maven.aliyun.com/nexus…


保留下面设置后回到仓库页面,能够看到曾经增加了一个新的仓库 aliyun.

2. 设置 aliyun maven 优先级

关上 Repositories->maven public 并设置代理仓库优先级置顶

3. 本地 maven 私服仓库配置

设置 maven 的 Settings.xml 文件,依照上面配置进行设置私服地址和验证的用户名、明码。

3 . 创立一个 maven 我的项目测试

1. 拉取测试

顺手关上一个 idea 我的项目增加了一个 <dependency> 进行拉取测试

更新 maven 我的项目:

ok 如下能够从集体配置的 maven 代理仓库更新了!

2. 推送设置

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

<distributionManagement>
    <!-- Maven 上传设置 -->
    <repository>
        <id>nexus</id>   <!-- 放弃和 Settings.xml 中配置的 Server ID 统一 -->
        <name>releases</name>
        <url>http://http://nexus.xxx.com//repository/maven-releases/</url>  <!-- 推送到 Maven 仓库的 maven-releases 下 -->
    </repository>
</distributionManagement>

. 当然了仓库本人新建了两个:zhangpeng-releases 对应 release
zhangpeng-snapshots 对应 snapshots


mvn deploy 打包:

登陆 nexus:

嗯对我来说这就算是胜利了 ……

总结:

1. 腾讯云开源的 cbs 组件不反对 selector。

2. 当 pv,pvc 须要运行权限时候能够应用 initContainers 的形式,执行脚本命令。

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

正文完
 0