一.创建命名空间
1.命令方式
-
通过命令方式创建一个名为nexus3的namespace
kubectl create namespace nexus3
2.yaml方式
-
通过yaml文件方式创建一个名为nexus3的namespace
apiVersion: v1 kind: Namespace metadata: name: nexus3 labels: name: nexus3
kubectl apply repo-nexus-ns.yaml
二.创建PV/PVC
1.创建存储类
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nexus3-nfs
provisioner: example.com/external-nfs
parameters:
server: 10.23.23.45 #nfs服务器IP地址
path: /usr1/nfs-share/nexus #nfs共享路径
readOnly: "false"
kubectl apply nexus_class.yaml
注意:/usr1/nfs-share/nexus #nfs共享路径 设置成777权限
2.创建PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: nexus-pv
namespace: nexus3
spec:
capacity:
storage: 600Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nexus3-nfs
nfs:
path: /usr1/nfs-share/nexus #nfs共享路径
server: 10.23.23.45 #nfs服务器IP地址
kubectl apply nexus_pv.yaml
3.创建PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nexus-pvc
namespace: nexus3
spec:
storageClassName: nexus3-nfs
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 600Gi
kubectl apply nexus_pvc.yaml
4.查看PV和PVC创建情况
kubectl get pv
三.K8S部署Nexus
1.创建配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nexus3
name: nexus3
namespace: nexus3
spec:
replicas: 1
selector:
matchLabels:
app: nexus3
template:
metadata:
labels:
app: nexus3
name: nexus3
namespace: nexus3
spec:
containers:
- name: nexus3
image: sonatype/nexus3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8081
protocol: TCP
name: web
- containerPort: 8082
protocol: TCP
name: docker-hosted
- containerPort: 8083
protocol: TCP
name: docker-group
volumeMounts:
- name: nexus-data
mountPath: /nexus-data
volumes:
- name: nexus-data
persistentVolumeClaim:
claimName: nexus-pvc
---
apiVersion: v1
kind: Service
metadata:
name: nexus3
namespace: nexus3
labels:
app: nexus3
spec:
selector:
app: nexus3
type: NodePort
sessionAffinity: None
ports:
- name: web
protocol: TCP
port: 8081
targetPort: 8081
nodePort: 30005
- name: docker-hosted
protocol: TCP
port: 8082
targetPort: 8082
nodePort: 30006
- name: docker-group
protocol: TCP
port: 8083
targetPort: 8083
nodePort: 30007
2.执行命令使其生效
kubectl apply -f nexus.yaml
四.配置Nexus服务器
1.查询初始密码
kubectl exec XXXXXXXX -n nexus3 -- cat /nexus-data/admin.password