PROGRAMMING

Nexus3 on Kubernetes

编写 Nexus 声明文件

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nexus-data
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /root/data/nexus-data

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nexus-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nexus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nexus
  template:
    metadata:
      labels:
        app: nexus
    spec:
      containers:
        - name: nexus
          image: sonatype/nexus3
          ports:
            - containerPort: 8081
          volumeMounts:
            - mountPath: /nexus-data
              name: nexus-data
      volumes:
        - name: nexus-data
          persistentVolumeClaim:
            claimName: nexus-data-pvc

---

apiVersion: v1
kind: Service
metadata:
  name: nexus
spec:
  selector:
    app: nexus
  ports:
    - name: web
      port: 80
      targetPort: 8081

---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nexus
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: nexus.domain.com
      http:
        paths:
          - path: /
            backend:
              serviceName: nexus
              servicePort: 80

部署 Nexus 服务

kubectl create -f nexus.yaml

使用 hostPath 创建的 PV 可能会碰到 Permission Denied 问题,可以使用以下方法解决:

在 Pod template spec 声明处添加:

initContainers:
  - name: nexus-data-permission
    image: busybox:latest
    command: ["sh", "-c", "chown -R 200:200 /nexus-data"]
    volumeMounts:
      - name: nexus-data
        mountPath: /nexus-data

配置 Maven Repository

获取 admin 账户的密码:

kubectl exec po/nexus-pod -- cat /nexus-data/admin.password

打开配置界面,选择 Repositories>maven-central, 设置 Proxy 下的 Romote storage:

使用阿里云的仓库:http://maven.aliyun.com/nexus/content/repositories/central

参考资源