Loading
Loading...

Kubernetes 中部署 Maven 私有仓库 Sonatype Nexus3

环境说明:

一、Sonatype Nexus3 简介

简介

Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 Maven仓库,极大的简化了本地内部仓库的维护和外部仓库的访问,同时是一套开箱即用的系统不需要数据库,并且还提供强大的仓库管理、构建、搜索等功能。

优点

  • 节省外网带宽
  • 加速Maven构建
  • 可以为本地建立本地内部仓库
  • 方便项目组存放各种 Jar 的管理

二、Kubernetes 部署 Sonatype Nexus3

1、创建 PV & PVC

存储驱动为 NFS,需要提前在 NFS 目录下创建对应存储文件夹并设置一定的文件夹权限,然后设置 PV 与 NFS 地址和目录绑定。

nexus-storage.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: sonatype-nexus
labels:
app: sonatype-nexus
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
mountOptions: #NFS挂在选项
- hard
- nfsvers=4.1
nfs: #NFS设置
server: 192.168.2.11
path: /nfs/data/nexus
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonatype-nexus
labels:
app: sonatype-nexus
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi #和PV提供的容量保持一致
selector:
matchLabels:
app: sonatype-nexus

创建 PV & PVC

操作 kubectl 部署 PV & PVC,注意”-n”后面要指定应用所在的 Namespace

Terminal window
$ kubectl apply -f nexus-storage.yaml -n mydlqcloud

2、部署 Sonatype Nexus3

nexus.yaml

apiVersion: v1
kind: Service
metadata:
name: sonatype-nexus
labels:
app: sonatype-nexus
spec:
type: NodePort
ports:
- name: sonatype-nexus
port: 8081
targetPort: 8081
nodePort: 30881 #设置NodePort端口值
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.17.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

创建 Nexus

操作 kubectl 部署 Nexus,注意”-n”后面要指定应用所在的 Namespace

Terminal window
$ kubectl apply -f nexus.yaml -n mydlqcloud

3、获取用户、密码

当 Sonatype Nexus3 创建完成后,会创建默认管理员用户”admin”,且生成一个随机密码存储在持久化目录 admin.password 文件中。我们可以先获取创建的 Pod 名称,再利用 exec 命令获取容器内密码文件 /nexus-data/admin.password 内容。

注意:请替换”-n”后面的 namespace 名称

获取 Pod 名称

  • -n:Pod 所在的 namespace
Terminal window
$ kubectl get pods -n mydlqcloud | grep sonatype-nexus
sonatype-nexus-d66769d75-plzct 1/1 Running 0 114m

可以知道 Pod 名称为: “sonatype-nexus-d66769d75-plzct”

获取密码

  • -n:Pod 所在的 namespace
  • -i:在容器内要执行的 shell 命令
Terminal window
$ kubectl exec sonatype-nexus-d66769d75-plzct -i cat /nexus-data/admin.password -n mydlqcloud
38da62ba-7ca5-46a3-957f-8e66b9059fdc

可以知道 Nexus 密码为: “sonatype-nexus-d66769d75-plzct”

三、访问 Sonatype Nexus 并修改初始密码

输入 Kubernetes 集群入口的 IP 地址和 Sonatype Service 端口号进入 Sonatype,例如我这里为: http://192.168.2.11:30881 ,进入后可以看到:

进入后右上角有个登录按钮,点一下后弹出登录框,用”admin”用户和上面在容器文件内获取的随机密码登录

然后会提醒修改密码,这里输入一下新密码

接下来会询问,是否开启匿名访问

然后设置完成,进入 Sonatype Nexus 页面

四、添加 Maven 代理

1、添加 Aliyun Maven 代理

由于中央仓库访问速度慢导致连接经常超时,所以这里添加一个 Aliyun 的 Maven 仓库代理,用于优先从 Aliyun 中拉取相关 JAR。

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

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

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

2、设置 Aliyun Maven 优先级

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

将 Aliyun 仓库添加到 Members 一栏,然后将其位置置顶。

五、Maven 设置私服仓库配置

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

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!-- 设置私有Nexus仓库的用户名/密码 -->
<server>
<id>nexus</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<!-- 设置私有仓库地址 -->
<mirror>
<id>nexus</id>
<name>my-nexus</name>
<mirrorOf>central</mirrorOf>
<url>http://192.168.2.11:30881/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
</profiles>
</settings>

六、创建项目进行测试

1、拉取测试

创建一个 Java Maven 项目,且 Pom.xml 中随便引用一个公网相关项目,查看能否正常拉取,如果能就就代表配置没有问题。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>club.mydlq</groupId>
<artifactId>nexus-maven-test</artifactId>
<version>0.0.1</version>
<name>nexus-maven-test</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.seratch</groupId>
<artifactId>jslack</artifactId>
<version>1.7.3</version>
</dependency>
</dependencies>
</project>

然后能看到能够正常拉取。

2、推送设置

这里用一个示例项目,配置好 Maven 仓库的参数,然后执行 Deploy 命令将其推送到私有仓库进行测试,看其是否能够推送成功。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>club.mydlq</groupId>
<artifactId>nexus-maven-test</artifactId>
<version>0.0.1</version>
<name>nexus-maven-test</name>
<properties>
<java.version>1.8</java.version>
</properties>
<distributionManagement>
<!-- Maven 上传设置 -->
<repository>
<id>nexus</id> <!-- 保持和Settings.xml中配置的Server ID一致 -->
<name>releases</name>
<url>http://192.168.2.11:30881/repository/maven-releases/</url> <!-- 推送到Maven仓库的maven-releases下 -->
</repository>
</distributionManagement>
</project>

执行 Maven 的 Deploy 命令,将项目推送到 Maven 私有仓库

Terminal window
$ mvn deploy

推送成功查看 Nexus 下的 maven-releases 仓库里面,可以看到已经推送成。

---END---
如果本文对你有帮助,可以关注我的公众号 "小豆丁技术栈" 了解最新动态,顺便也请帮忙 Github 点颗星哦,感谢~

本文作者:超级小豆丁 @ 小豆丁技术栈

本文链接:http://www.mydlq.club/article/26/

本文标题:Kubernetes 中部署 Maven 私有仓库 Sonatype Nexus3

本文版权:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!