image-20230425094321462

如遇图片无法加载请使用代理访问

前言

在拆分微服务前,我们需要先把公共的代码(比如数据库连接、工具包等)封装成多个jar包,这样以后无论是创建一个新项目,还是更改公共部分的代码,只需要更改这个jar包,其他项目引用最新的这个jar包即可。因此我们需要一个组件,像harbor一样,管理不同版本号的jar包,而且需要maven的加持,Nexus就是解决方案

Nexus需要挂在pvc,本文将挂载nfs的pvc,如果你没部署过nfs以及挂载,可以看这篇文章 存储服务


环境声明

  • Nexus: 3.32.0

安装

创建命名空间

1
kubectl create ns nexus

创建 nexus-deploy.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nexus-data-pvc
# 自定义命名空间
namespace: nexus
spec:
accessModes:
- ReadWriteMany
# 指定 storageClass 的名字,这里使用nfs的存储类名称,也可以选择默认standard
# storageClassName: "standard"
storageClassName: "nfs-client-m3"
resources:
requests:
storage: 10Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: nexus
name: nexus3
labels:
app: nexus3
spec:
replicas: 1
selector:
matchLabels:
app: nexus3
template:
metadata:
labels:
app: nexus3
spec:
containers:
- name: nexus3
image: sonatype/nexus3:3.32.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8081
name: web
protocol: TCP
livenessProbe:
httpGet:
path: /
port: 8081
initialDelaySeconds: 100
periodSeconds: 30
failureThreshold: 6
readinessProbe:
httpGet:
path: /
port: 8081
initialDelaySeconds: 100
periodSeconds: 30
failureThreshold: 6
# 这里根据服务器实际情况填写
resources:
limits:
cpu: 2000m
memory: 2Gi
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- name: nexus-data
mountPath: /nexus-data
# 我指定优先部署在m3节点,如果不需要则不用配置此项
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- m3
volumes:
- name: nexus-data
persistentVolumeClaim:
# 这里挂载的是上面第一个我们创建的PersistentVolumeClaim的name
claimName: nexus-data-pvc

---
apiVersion: v1
kind: Service
metadata:
name: nexus3
namespace: nexus
labels:
app: nexus3
spec:
selector:
app: nexus3
type: ClusterIP
ports:
- name: web
protocol: TCP
port: 8081
targetPort: 8081

执行安装

1
kubectl apply -f nexus-deploy.yaml

查看pod状态

1
2
3
4
5
kubectl get pods -n nexus

##
NAME READY STATUS RESTARTS AGE
nexus3-86886b959c-gjvcx 1/1 Running 0 2m8s

创建svc,方便访问主页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Service
metadata:
name: prod-nexus
annotations:
{}
labels:
{}
namespace: nexus
spec:
selector:
app: nexus3
ports:
- name: port
port: 8081
protocol: TCP
targetPort: 8081
nodePort: 30400
sessionAffinity: None
type: NodePort

最后访问任意节点:30400即可跳转Nexus首页

获取 admin 密码

1
kubectl exec $(kubectl get pods -n nexus | awk 'NR==2{print $1}') -n nexus -- cat /nexus-data/admin.password

参考文章

[1] 在 k8s 中安装并使用 nexus_惜鸟的博客-CSDN博客