Linux搭建nexus倉庫
1.安裝jdk
1.1 獲取安裝包,解壓到指定目錄:
1 tar xf jdk.tar.gz -C /opt/export
1.2 配置環境變量:
1 # vim /etc/profile 2 export JAVA_HOME=/opt/export/jdk 3 export PATH=$JAVA_HOME/bin:$PATH 4 export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar 5 export RUN_AS_USER=root # 后邊啟動nexus需要 6 7 # source /etc/profile
1.3 出現下面結果,說明部署成功
1 # java -version 2 java version "1.7.0_80" 3 Java(TM) SE Runtime Environment (build 1.7.0_80-b15) 4 Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
2.安裝nexus
2.1下載安裝
下載地址:
https://www.sonatype.com/download-oss-sonatype
https://help.sonatype.com/repomanager2/download/download-archives---repository-manager-oss
1 cd /opt 2 tar xf nexus-2.4.0-09-bundle.tar.gz
解壓后有兩個目錄:
1 $ ls /opt/ 2 nexus-2.4.0-09 sonatype-work
更改目錄名稱:
1 mv nexus-2.4.0-09 nexus
2.2 更改nexus配置文件
默認端口為8081,可根據需要修改:
1 $ vim /opt/nexus/conf/nexus.properties 2 # Jetty section 3 application-port=8081 # 修改為10890 4 application-host=0.0.0.0 5 nexus-webapp=${bundleBasedir}/nexus 6 nexus-webapp-context-path=/nexus 7 8 # Nexus section 9 nexus-work=${bundleBasedir}/../sonatype-work/nexus 10 runtime=${bundleBasedir}/nexus/WEB-INF
2.3 關閉防火牆或打開10890端口
1 /etc/init.d/iptables stop 2 chkconfig iptables off
2.4 啟動nexus
1 $ /opt/nexus/bin/jsw/linux-x86-64/nexus start 2 **************************************** 3 WARNING - NOT RECOMMENDED TO RUN AS ROOT 4 **************************************** 5 Starting Nexus OSS... 6 Started Nexus OSS.
如果沒有配置環境變量RUN_AS_USER=root,會報錯:
1 # /opt/nexus/bin/jsw/linux-x86-64/nexus start 2 **************************************** 3 WARNING - NOT RECOMMENDED TO RUN AS ROOT 4 **************************************** 5 If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.
2.5 檢查進程和端口
3.瀏覽器訪問nexus
1 http://ip地址:8081/nexus 2 登錄,默認用戶名 admin 默認密碼 admin123
訪問登錄:
4.遷移nexus
如果想要將nexus倉庫遷移,只需要打包此目錄,遷移到新nexus主機:
1 $ du -sh /opt/sonatype-work/nexus/storage 2 47G /opt/sonatype-work/nexus/storage/
由於文件過大,可采用rsync的方式。
本文參考:https://blog.csdn.net/sunyanxiong123/article/details/68926610
Nexus高可用方案
描述:由於nexus進程會因為某些原因掛掉,為了不影響使用,決定做nexus高可用。
准備:根據上一章,准備兩台服務器搭建nexus,主:192.168.51.204 maven01,備:192.168.51.207 maven02:
1.搭建keepalived
主:
1 # cat /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 4 global_defs { 5 router_id maven01 6 } 7 vrrp_script chk_http_port { #檢測nexus端口是否存在,不存在進行vip漂移 8 script "</dev/tcp/127.0.0.1/10890" 9 interval 1 10 weight -30 11 fall 1 12 rise 1 13 } 14 vrrp_instance VI_1 { 15 state MASTER 16 interface eth0 17 virtual_router_id 88 18 priority 150 19 advert_int 1 20 authentication { 21 auth_type PASS 22 auth_pass 1111 23 } 24 virtual_ipaddress { 25 192.168.51.210 26 } 27 track_script { 28 chk_http_port 29 } 30 }
備:
1 # cat /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 4 global_defs { 5 router_id maven01 6 } 7 vrrp_script chk_http_port { 8 script "</dev/tcp/127.0.0.1/10890" 9 interval 1 10 weight -30 11 fall 1 12 rise 1 13 } 14 vrrp_instance VI_1 { 15 state MASTER 16 interface eth0 17 virtual_router_id 88 18 priority 100 19 advert_int 1 20 authentication { 21 auth_type PASS 22 auth_pass 1111 23 } 24 virtual_ipaddress { 25 192.168.51.210 26 } 27 track_script { 28 chk_http_port 29 } 30 }
2.同步數據
描述:對比同步數據方式,1⃣️定時任務rsync同步:不及時,容易遺漏數據。2⃣️nfs共享:能夠解決數據一致問題,但是主一旦宕機,備庫起不到任何作用。3⃣️sersync或者inotify+rsync:可以實現實時同步,最后選用inotify方式。
2.1 部署rsync
2.1.1 備:部署rsync服務端
安裝rsync軟件
1 yum install -y rsync
編寫配置文件
1 $ cat /etc/rsyncd.conf 2 #created by yjn at 2018 3 4 uid = rsync 5 gid = rsync 6 use chroot = no 7 max connections = 10 8 strict modes = yes 9 pid file = /var/run/rsyncd.pid 10 lock file = /var/run/rsync.lock 11 log file = /var/log/rsyncd.log 12 13 [nexus] 14 path = /opt/sonatype-work/nexus/storage 15 comment = "nexus backup dir" 16 ignore errors 17 read only = no 18 write only = no 19 hosts allow = 192.168.0.0/16 20 auth users = rsync_backup 21 secrets file = /etc/rsync.password
創建備份目錄的管理用戶
1 useradd -s /sbin/nologin -M rsync
創建安全認證文件
1 echo "rsync_backup:123" >/etc/rsync.password 2 chmod 600 /etc/rsync.password
修改備份目錄屬主
1 chown -R rsync.rsync /opt/sonatype-work/nexus/storage
啟動rsync服務
1 rsync --daemon 2 說明:rsync服務的端口號為873端口(tcp)
2.1.2 主:部署rsync客戶端
安裝rsync軟件
1 yum install -y rsync
創建安全認證文件
1 echo "123" >/etc/rsync.password 2 chmod 600 /etc/rsync.password
2.2 部署inotify-tools軟件
inotify軟件的參考資料鏈接:https://github.com/rvoicilas/inotify-tools/wiki
1 # yum install -y inotify-tools 2 Loaded plugins: fastestmirror, security 3 Setting up Install Process 4 Loading mirror speeds from cached hostfile 5 * base: mirrors.zju.edu.cn 6 * extras: mirror.bit.edu.cn 7 * updates: mirrors.tuna.tsinghua.edu.cn 8 No package inotify-tools available. 9 Error: Nothing to do
沒有這個包,更新epel源:
1 yum install -y epel-release && yum update
2.3 inotify+rsync結合腳本同步nexus
2.3.1 寫同步腳本
#!/bin/bash ########### inotifywait -mrq /opt/sonatype-work/nexus/storage --format '%w%f' -e create,delete,close_write,moved_to|\ while read line do rsync -az --delete /opt/sonatype-work/nexus/storage/* rsync_backup@192.168.51.207::nexus --password-file=/etc/rsync.password &>/dev/null done
2.3.2 后台執行:
1 sh /yjn/scripts/backup.sh &
2.3.3 發現報錯:
1 Failed to watch /opt/sonatype-work/nexus/storage; upper limit on inotify watches reached! 2 Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.
注意:inotify默認監控同步文件的個數是有限制的,8192,通過配置文件/proc/sys/fs/inotify/max_user_watches可以調整監控的個數。此問題明顯就是文件太多,導致無法監控。
2.3.4 解決:
1 echo 8192000 > /proc/sys/fs/inotify/max_user_watches
此時再執行腳本,沒有報錯信息,驗證可以同步。