生活是屬于每個人自己的感受临庇,不屬于任何別人的看法反璃。—— 余華《活著》
一. 概述
在學(xué)習(xí)使用一個工具之前假夺,我們需要知道怎么安裝它淮蜈。本文將自己學(xué)習(xí)的過程記錄下來,一方面鞏固學(xué)習(xí)的內(nèi)容已卷,另一方面希望對有同樣需求的小伙伴提供一些幫助梧田。
開源工具 | 描述 | 官方文檔 | 官方安裝文檔 | docker 安裝 |
---|---|---|---|---|
nexus | maven 倉庫管理工具 | nexus 官網(wǎng) | nexus 快速安裝 | docker 安裝 |
上面表格列出了官方的安裝地址,如果需要快速體驗(yàn)使用侧蘸,建議直接使用 docker 安裝裁眯,一行命令就可以啟動應(yīng)用:
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
下文將介紹在 k8s 中安裝并使用 nexus,這里將使用兩種方式安裝:
自己編寫部署清單
nexus-deploy.yaml
安裝使用 helm 安裝
安裝環(huán)境
這里使用 minikube 進(jìn)行安裝讳癌,在 k8s 集群中基本使用是一樣的
- minikube : v1.18.1
- helm : v3.5.3
二. 編寫部署清單 nexus-deploy.yaml
安裝
由于 nexus 需要持久化數(shù)據(jù)穿稳,所以我們需要創(chuàng)建 PVC
,建議使用 storageClass
動態(tài)創(chuàng)建 PVC
晌坤,在 minikube
中有一個默認(rèn)的 storageClass
逢艘,名稱是:standard
,可以使用下面的命令查看:
# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
standard (default) k8s.io/minikube-hostpath Delete Immediate false 50m
storageClass 的使用可以查看官網(wǎng):https://kubernetes.io/zh/docs/concepts/storage/storage-classes/
創(chuàng)建 nexus-deploy.yaml
文件骤菠,文件內(nèi)容如下:
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nexus-data-pvc
namespace: default
spec:
accessModes:
- ReadWriteMany
# 指定 storageClass 的名字它改,這里使用默認(rèn)的 standard
storageClassName: "standard"
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
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: 4000m
memory: 2Gi
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- name: nexus-data
mountPath: /nexus-data
volumes:
- name: nexus-data
persistentVolumeClaim:
claimName: nexus-data-pvc
apiVersion: v1
kind: Service
metadata:
name: nexus3
namespace: default
labels:
app: nexus3
spec:
selector:
app: nexus3
type: ClusterIP
ports:
- name: web
protocol: TCP
port: 8081
targetPort: 8081
使用如下命令部署應(yīng)用:
# kubectl apply -f nexus-deploy.yaml
deployment.apps/nexus3 created
persistentvolumeclaim/nexus-data-pvc created
service/nexus3 created
使用如下命令查看 pod 是否正常運(yùn)行:
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nexus3-6c75965bcf-6tj5w 1/1 Running 0 5m37s
使用如下命令查看 pod 的日志:
kubectl logs -f nexus3-6c75965bcf-6tj5w -n default
看到如下內(nèi)容,則表示應(yīng)用啟動成功:
使用如下命令暴露 pod 端口到本機(jī)娩怎,以便外部訪問:
# 使用說明: kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT
kubectl port-forward service/nexus3 8081:8081
生產(chǎn)使用建議通過 ingress 暴露服務(wù)搔课,這里通過 port-forward 臨時暴露服務(wù)
默認(rèn)登錄 nexus 的賬號和密碼如下:
- 用戶名:admin
- 密碼:默認(rèn)的初始密碼在服務(wù)器的
/nexus-data/admin.password
文件中
使用下面的命令獲取默認(rèn)的密碼:
kubectl exec nexus3-6c75965bcf-6tj5w -- cat /nexus-data/admin.password
登錄 nexus:
登錄成功后需要設(shè)置新密碼:
更新密碼后,配置是否開啟匿名訪問截亦。啟用匿名訪問意味著默認(rèn)情況下爬泥,用戶可以在沒有憑據(jù)的情況下從存儲庫搜索、瀏覽和下載組件崩瓤∨鄯龋考慮到安全的問題,這里選擇禁用匿名訪問却桶。
安裝完成后如下圖:
可以使用下面的命令刪除安裝的 nexus 相關(guān)的資源:
# kubectl delete -f nexus-deploy.yaml
deployment.apps "nexus3" deleted
persistentvolumeclaim "nexus-data-pvc" deleted
service "nexus3" deleted
三. 使用 helm 安裝 nexus
可以去到 helm 官方包管理倉庫查找需要安裝的應(yīng)用境输。
helm 包管理地址:https://artifacthub.io/
這里我選擇安裝下圖所示的 nexus:(可以根據(jù)自己的需求選擇安裝 star 比較多,更新比較頻繁的 chart)
下面將根據(jù)文檔說明進(jìn)行安裝:
使用下面的命令添加 helm 倉庫:
helm repo add sonatype https://sonatype.github.io/helm3-charts/
使用下面的命令更新倉庫:
helm repo update
使用下面的命令搜索 nexus:
helm search repo nexus
使用下面的命令下載 chart 到本地:
helm pull sonatype/nexus-repository-manager
下載下來的是一個壓縮包颖系,可以通過 tar -zxvf
命令解壓嗅剖,根據(jù)需要修改其中的 values.yaml
文件,修改完成后可以通過下面的命令安裝 nexus:
helm install sonatype-nexus ./
如果不需要自定義配置嘁扼,可以直接使用下面的命令安裝:
helm install nexus-rm sonatype/nexus-repository-manager
四. nexus 的基本配置及使用
1. nexus 配置說明
點(diǎn)擊左側(cè)Repositories按鈕信粮,可以看到下圖所示的倉庫內(nèi)容:
倉庫說明:
Name 列
maven-central:maven中央庫,默認(rèn)從
https://repo1.maven.org/maven2/
拉取 jar趁啸。maven-releases:私庫發(fā)行版 jar强缘。
maven-snapshots:私庫快照版(調(diào)試版本)jar督惰。
maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務(wù)旅掂,在本地 maven 基礎(chǔ)配置
settings.xml
中使用赏胚。
Type 列(Nexus默認(rèn)的倉庫類型有以下四種):
group(倉庫組類型):又叫組倉庫,用于方便開發(fā)人員自己選擇倉庫以及設(shè)置倉庫的順序商虐;
hosted(宿主類型):內(nèi)部項(xiàng)目的發(fā)布倉庫(內(nèi)部開發(fā)人員發(fā)布 jar 包存放的倉庫)觉阅;
proxy(代理類型):從遠(yuǎn)程中央倉庫中尋找數(shù)據(jù)的倉庫(可以點(diǎn)擊對應(yīng)的倉庫的Configuration頁簽,其中
Remote Storage Location
屬性的值即被代理的遠(yuǎn)程倉庫的路徑)称龙;virtual(虛擬類型):虛擬倉庫(這個基本用不到留拾,重點(diǎn)關(guān)注上面三個倉庫的使用)戳晌;
2. 創(chuàng)建自定義私有倉庫
自定義發(fā)行版(release)私庫
點(diǎn)擊 Create repository
創(chuàng)建自定義發(fā)行版(release)私庫鲫尊,選擇 maven2 (hosted)
,如下圖所示:
自定義倉庫配置如下:
主要配置如下幾項(xiàng)內(nèi)容:
Name:自定義名稱沦偎,必須唯一疫向,通常發(fā)行版?zhèn)}庫以 xxx-release
結(jié)尾,快照版?zhèn)}庫以 xxx-snapshots
結(jié)尾
Version policy:
- Release 一般是發(fā)行版的 jar
- Snapshot 一般是快照版的 jar
- Mixed 混合的
Deployment policy:Allow redeploy
其他的配置保持默認(rèn)即可豪嚎。
自定義快照版(snapshot)私庫
創(chuàng)建快照版(snapshot)私庫和上面的一樣搔驼,只需要將 Name 修改為 xxx-snapshots
,Version policy
修改為 Snapshot
侈询,如下圖所示:
自定義代理(proxy)倉庫
默認(rèn)從 https://repo1.maven.org/maven2/
拉取 jar舌涨,由于網(wǎng)絡(luò)的原因經(jīng)常導(dǎo)致無法下載相關(guān)的資源,所以這里介紹配置阿里云的 maven 倉庫代理扔字。
阿里云 maven 倉庫代理配置:https://maven.aliyun.com/mvn/guide
創(chuàng)建代理倉庫:
配置內(nèi)容如下圖:
配置說明:
Name:倉庫的名稱必須唯一囊嘉,這里配置為 aliyun-proxy
Version policy :Release
Remote storage:遠(yuǎn)程倉庫地址,這里配置阿里云 maven 倉庫地址:https://maven.aliyun.com/repository/public
自定義 group 倉庫的順序
可以使用默認(rèn)的 maven-public
革为,也可以自定義一個 maven2 (group)
類型的倉庫進(jìn)行設(shè)置扭粱,這里選擇自定義一個maven2 (group)
類型的倉庫,方便學(xué)習(xí)理解震檩。
配置如下圖:
3. 創(chuàng)建 nexus 用戶
五. maven 配置 nexus 說明
點(diǎn)擊前面定義的 maven2 (group)
查看其 URL 地址琢蛤,如下圖所示:
1. 在 settings.xml 文件中配置(全局有效)
打開 maven 的配置文件( windows 機(jī)器一般在 maven 安裝目錄的 conf/settings.xml ),在<mirrors></mirrors>
標(biāo)簽中添加 mirror 子節(jié)點(diǎn)抛虏,復(fù)制上面的地址博其,在 settings.xml
文件中添加如下內(nèi)容:
<servers>
<!-- 這里配置 nexus 的認(rèn)證信息,注意這里的 id 需要和 mirros 中的 id 相同才能夠匹配認(rèn)證 -->
<server>
<id>my-maven</id>
<username>test</username>
<password>maven123456</password>
</server>
</servers>
<mirrors>
<mirror>
<id>my-maven</id>
<!--mirrorof配置為 * 時迂猴,所有的請求都走這個mirror的url慕淡,
mirrorof配置是某個repositoryid時,若構(gòu)建找不到错忱,則會到maven默認(rèn)中央倉庫去獲取的儡率。-->
<mirrorOf>*</mirrorOf>
<name>自定義私有倉庫</name>
<url>http://localhost:8081/repository/my-group/</url>
</mirror>
</mirrors>
配置完成之后挂据,可以在 idea 中指定這個 settings.xml
文件,如下圖所示:
當(dāng)指定配置文件之后儿普,可以看到 idea 從 nexus 私有倉庫中下載 jar 包:
去到 nexus 倉庫中查看倉庫內(nèi)容崎逃,如下圖所示:
2. 在 pom.xml 文件中配置私有倉庫地址(項(xiàng)目有效)
在 pom.xml
中 repositories
標(biāo)簽的作用是用來配置 maven
項(xiàng)目的遠(yuǎn)程倉庫,示例如下:
<repositories>
<repository>
<!--定義倉庫 id ,必須唯一眉孩,需要 server 中的id對應(yīng)个绍,以便認(rèn)證-->
<id>my-maven</id>
<!--倉庫描述-->
<name>自定義私有倉庫</name>
<!--倉庫地址-->
<url>http://localhost:8081/repository/my-group/</url>
<!--是否可以從這個倉庫下載releases版本的構(gòu)件,默認(rèn)為 true-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否可以從這個倉庫下載snapshots版本的構(gòu)件浪汪,默認(rèn)為 true
禁止從公共倉庫下載snapshot構(gòu)件是推薦的做法巴柿,因?yàn)檫@些構(gòu)件不穩(wěn)定
-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
建議在 settings.xml 文件中配置私有倉庫地址,這樣所有項(xiàng)目都可以使用那個地址死遭。
3. 發(fā)布本地 jar 包到 nexus 倉庫
查看自定義的 snapshot 倉庫广恢,目前沒有相關(guān) jar 包資源,下面如何將本地jar包發(fā)布到私有倉庫呀潭。
在 pom.xml 文件中添加如下配置:
<distributionManagement>
<repository>
<!--定義倉庫 id ,必須唯一钉迷,需要 server 中的id對應(yīng),以便認(rèn)證-->
<id>my-maven</id>
<!--倉庫描述-->
<name>自定義發(fā)行版?zhèn)}庫</name>
<!--倉庫地址-->
<url>http://localhost:8081/repository/my-release/</url>
</repository>
<snapshotRepository>
<!--定義倉庫 id ,必須唯一钠署,需要 server 中的id對應(yīng)糠聪,以便認(rèn)證-->
<id>my-maven</id>
<!--倉庫描述-->
<name>自定義快照版?zhèn)}庫</name>
<!--倉庫地址-->
<url>http://localhost:8081/repository/my-snapshots/</url>
</snapshotRepository>
</distributionManagement>
執(zhí)行如下命令發(fā)布項(xiàng)目到 nexus 倉庫:
mvn clean deploy
構(gòu)建成功之后可以查看 nexus 倉庫內(nèi)容如下:
這里有一點(diǎn)值得注意的地方,如何將 jar 包發(fā)布到 release 倉庫谐鼎?
其實(shí)非常簡單舰蟆,只需要將 <version>1.0.0-SNAPSHOT</version>
中的 1.0.0-SNAPSHOT
改為 1.0.0
即可,當(dāng)再次執(zhí)行 mvn clean deploy
則會將 jar 包發(fā)布到 release 倉庫中了狸棍。
還可以直接通過 nexus 控制臺上傳 jar 包到私有倉庫: