date: 2020-12-20title: nexus3配置yum私有仓库 #标题
tags: yum #标签
categories: linux大杂烩 # 分类

生产环境,多数主机不可出外网,yum安装起来不太方便,之前是由nginx做了个yum代理,但同一时间只能有两个客户端使用yum安装,否则会超时报错。使用nginx配置yum代理,可以参考博文:配置nginx代理yum源及DNS

随着生产环境主机数量增多,考虑换一种姿势配置yum代理,就想到了nexus这个东西,特此记录。
参考文档:

环境准备

OS hostname IP role 是否可出网
CentOS 7.5 nexus 192.168.20.5 nexus服务端
CentOS 7.5 client 192.168.20.6 yum客户端

系统默认参数优化

  1. $ cat > /etc/security/limits.conf << EOF
  2. * - nofile 655360
  3. * - memlock unlimited
  4. * - stack 655360
  5. * - nproc unlimited
  6. EOF
  7. $ cat > /etc/sysctl.conf << EOF
  8. kernel.sysrq = 0
  9. kernel.core_uses_pid = 1
  10. kernel.msgmnb = 65536
  11. kernel.msgmax = 65536
  12. fs.file-max=655360
  13. kernel.shmmax = 68719476736
  14. kernel.shmall = 4294967296
  15. kernel.pid_max = 655360
  16. net.ipv4.tcp_tw_reuse = 1
  17. net.ipv4.tcp_tw_recycle = 0
  18. net.ipv4.tcp_max_tw_buckets = 10000
  19. net.ipv4.tcp_fin_timeout = 30
  20. net.ipv4.tcp_timestamps = 0
  21. net.ipv4.tcp_sack = 1
  22. net.ipv4.tcp_window_scaling = 1
  23. net.ipv4.tcp_ecn = 0
  24. net.ipv4.tcp_keepalive_time = 600
  25. net.ipv4.tcp_keepalive_intvl = 30
  26. net.ipv4.tcp_keepalive_probes = 3
  27. net.ipv4.tcp_max_orphans = 655360
  28. net.ipv4.tcp_max_syn_backlog = 262144
  29. net.ipv4.tcp_mem = 65536 131072 262144
  30. net.ipv4.udp_mem = 65536 131072 262144
  31. net.ipv4.tcp_rmem = 4096 87380 16777216
  32. net.ipv4.tcp_wmem = 4096 16384 16777216
  33. net.ipv4.ip_local_port_range = 1024 65535
  34. net.ipv4.route.gc_timeout = 100
  35. # 禁止icmp重定向报文
  36. net.ipv4.conf.all.accept_redirects = 0
  37. # 禁止icmp源路由
  38. net.ipv4.conf.all.accept_source_route = 0
  39. net.core.somaxconn = 65535
  40. net.core.rmem_default = 8388608
  41. net.core.wmem_default = 8388608
  42. net.core.rmem_max = 16777216
  43. net.core.wmem_max = 16777216
  44. net.core.netdev_max_backlog = 262144
  45. vm.swappiness = 10
  46. vm.overcommit_memory = 1
  47. vm.max_map_count = 262144
  48. EOF

配置jdk

自行去oracle官网下载java包 jdk-8u261-linux-x64.tar.gz ( 版本需要在1.8以上)然后上传至服务器。

$ tar zxf jdk-8u261-linux-x64.tar.gz -C /opt/
$ cat >> /etc/profile << EOF
export JAVA_HOME=/opt/jdk1.8.0_261
export CLASSPATH=\$JAVA_HOME/lib
export PATH=\$JAVA_HOME/bin:\$PATH
EOF
$ source /etc/profile
$ java -version      # 查看版本信息
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)

安装nexus

nexus的二进制包不好从官方下,如果你们有什么国内的下载地址,可以评论里分享下,我这里费尽周折下载了一个最新版本(2020-12-12日下载),提取链接:百度网盘( 提取码:1233 )。

下载及解压

# 官方下载链接(如已下载我网盘的包,可不执行wget,大概率下载不下来)
# wget https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/3/nexus-3.29.0-02-unix.tar.gz


$ tar zxf nexus-3.29.0-02-unix.tar.gz -C /opt/
$ cd /opt/
$ mv sonatype-work nexus-3.29.0-02/

修改配置文件

$ cd /opt/nexus-3.29.0-02/

$ vim etc/nexus-default.properties     # 修改监听地址及端口(可不修改)
application-port=8081
application-host=0.0.0.0

$ vim bin/nexus.vmoptions     # 修改jvm参数、日志等存储路径
-Xms4096m        # 最小可用内存
-Xmx4096m        # 最大可用内存(建议与最小可用内存保持一致)
-XX:MaxDirectMemorySize=4096m
-XX:+UnlockDiagnosticVMOptions
-XX:+LogVMOutput
-XX:LogFile=/opt/nexus-3.29.0-02/sonatype-work/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=/opt/nexus-3.29.0-02/sonatype-work/nexus3
-Dkaraf.log=/opt/nexus-3.29.0-02/sonatype-work/nexus3/log
-Djava.io.tmpdir=/opt/nexus-3.29.0-02/sonatype-work/nexus3/tmp
# 将上面的路径都修改为绝对路径

创建普通用户并启动

注:root用户启动可能会有问题。

$ useradd nexus
$ chown -R nexus /opt/nexus-3.29.0-02
$ ln -sf /opt/nexus-3.29.0-02/bin/nexus /usr/local/bin/

$ su -s /bin/bash -c "nexus start" nexus      # 启动nexus

$ ss -lnptu | grep 8081      # 等待片刻后,确认8081端口在监听(8081端口启动较慢,多点耐心)
tcp    LISTEN     0      50        *:8081                  *:*                   users:(("java",pid=7587,fd=788))

配置yum代理

初始化nexus

访问nexus的web管理页,URL:http://nexus_ip:8081 ,如下:

nexus3配置yum私有仓库 - 图1

查看admin密码

$ cat /opt/nexus-3.29.0-02/sonatype-work/nexus3/admin.password
aea9dee2-5add-4f57-acc0-1b0cd2da4b35

nexus3配置yum私有仓库 - 图2

nexus3配置yum私有仓库 - 图3

修改默认密码

nexus3配置yum私有仓库 - 图4

开启匿名用户访问

nexus3配置yum私有仓库 - 图5

完成初始化配置

nexus3配置yum私有仓库 - 图6

创建rpm包存储系统

nexus3配置yum私有仓库 - 图7

nexus3配置yum私有仓库 - 图8

nexus3配置yum私有仓库 - 图9

创建存储库

创建centos存储库

nexus3配置yum私有仓库 - 图10

nexus3配置yum私有仓库 - 图11

仓库类型说明

类型 说明
hosted 本地存储。像官方仓库一样提供本地私库功能
proxy 从远程中央仓库中寻找数据的仓库
group 组类型,能够组合多个仓库为一个地址提供服务

下面的 Remote storage填写: https://opsx.alibaba.com/centos/

nexus3配置yum私有仓库 - 图12

nexus3配置yum私有仓库 - 图13

创建epel存储库

nexus3配置yum私有仓库 - 图14

nexus3配置yum私有仓库 - 图15

下面的 Remote storage填写: http://mirrors.aliyun.com/epel/

nexus3配置yum私有仓库 - 图16

nexus3配置yum私有仓库 - 图17

查看存储库的URL

nexus3配置yum私有仓库 - 图18

分别查看上面创建的两个存储库,如下:

nexus3配置yum私有仓库 - 图19

nexus3配置yum私有仓库 - 图20

至此,nexus配置完成。

配置客户端

以下操作到不能出网的客户端进行配置。

# 配置默认的repo文件
$ cd /etc/yum.repos.d/
$ mkdir bak
$ mv *.repo bak/


# 将下面的baseurl换成你自己的maven服务地址
$ cat > nexus.repo << EOF
[os]
name=os
baseurl=http://192.168.20.5:8081/repository/centos/\$releasever/os/\$basearch/
enabled=1
gpgcheck=0
[updates]
name=updates
baseurl=http://192.168.20.5:8081/repository/centos/\$releasever/updates/\$basearch/
enabled=1
gpgcheck=0
[extras]
name=extras
baseurl=http://192.168.20.5:8081/repository/centos/\$releasever/extras/\$basearch/
enabled=1
gpgcheck=0
[centosplus]
name=centosplus
baseurl=http://192.168.20.5:8081/repository/centos/\$releasever/centosplus/\$basearch/
enabled=1
gpgcheck=0
[configmanagement]
name=configmanagement
baseurl=http://192.168.20.5:8081/repository/centos/\$releasever/configmanagement/\$basearch/ansible-29/
enabled=1
gpgcheck=0
[epel]
name=Extra Packages for Enterprise Linux 7 - \$basearch
baseurl=http://192.168.20.5:8081/repository/epel/7/\$basearch
enabled=1
gpgcheck=0
EOF




$ yum clean all && yum makecache    
# 如果makecache报错,请关注最后一条输出,确认是哪个url找不到,然后进行修改。


$ yum repolist      # 查看可用rpm包数量
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
源标识                                                                  源名称                                                                                        状态
centosplus/7/x86_64                                                     centosplus                                                                                        45
configmanagement/7/x86_64                                               configmanagement                                                                                  33
epel/x86_64                                                             Extra Packages for Enterprise Linux 7 - x86_64                                                13,486
extras/7/x86_64                                                         extras                                                                                           448
os/7/x86_64                                                             os                                                                                            10,072
updates/7/x86_64                                                        updates                                                                                          778
repolist: 24,862      # 确认可用rpm包数量



# 随便yum几个试试效果
yum -y install nginx php ansible lsof strace

查看nexus缓的rpm包

nexus3配置yum私有仓库 - 图21

齐活,以后每个需要yum的客户端,只需要按照上面配置客户端进行即可。