自动化部署必备技能—搭建YUM仓库

导言:

YUM主要用于自动安装、升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系。要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库),这个软件仓库我们习惯称为yum源。网络上有大量的yum源,但由于受到网络环境的限制,导致软件安装耗时过长甚至失败。特别是当有大量服务器大量软件包需要安装时,缓慢的进度条令人难以忍受。因此我们在优化系统时,都会更换国内的源。

相比较而言,本地YUM源服务器最大优点是局域网的快速网络连接和稳定性。有了局域网中的YUM源服务器,即便在Internet连接中断的情况下,也不会影响其他YUM客户端的软件安装和升级。

1. 创建yum仓库目录

  1. mkdir -p /application/yum/centos6.6/x86_64/
  2. cd /application/yum/centos6.6/x86_64/
  3. rz # 上传rpm包到此目录,此目录下面还可以包括文件夹

2. 安装createrepo软件

yum -y install createrepo

3. 初始化repodata索引文件

  1. createrepo -pdo /application/yum/centos6.6/x86_64/ /application/yum/centos6.6/x86_64/

4. 提供yum服务

  1. # 可以用Apache或nginx提供web服务,但用Python的http模块更简单,适用于内网环境
  2. cd /application/yum/centos6.6/x86_64/
  3. python -m SimpleHTTPServer 80 &>/dev/null &
  4. 可以通过浏览器输入本机IP查看。

5. 添加新的rpm包

  1. # 只下载软件不安装
  2. yumdownloader pcre-devel openssl-devel
  3. createrepo --update /application/yum/centos6.6/x86_64/
  4. # 每加入一个rpm包就要更新一下。

平时yum安装软件时不删除安装包

  1. # cat /etc/yum.conf
  2. keepcache=1
  3. # 安装包存储目录
  4. cachedir=/var/cache/yum/$basearch/$releasever
  5. # /var/cache/yum/x86_64/6/base/packages

6. 客户端配置

  1. # cd /etc/yum.repos.d
  2. [root@B yum.repos.d]# vi oldboy.repo
  3. [oldboy]
  4. name=Server
  5. baseurl=http://10.0.0.5
  6. enable=1
  7. gpgcheck=0
  8. [root@YUM ~]# yum --enablerepo=oldboy --disablerepo=base,extras,updates,epel list
  9. # 指定使用oldboy库

上面是临时使用内网yum源,想永久并简单使用yum -y install lrzsz命令,就需要修改配置文件将默认的repo文件关闭。

  1. [root@oldboy ~]# cd /etc/yum.repos.d/
  2. [root@oldboy yum.repos.d]# vim CentOS-Base.repo
  3. # 在每一个启动的源加上
  4. # enabled=0 #改为1就启用,没有此参数也是启用。
  5. [base]
  6. …………
  7. enabled=0
  8. [updates]
  9. …………
  10. enabled=0
  11. [extras]
  12. …………
  13. enabled=0
  14. # 还有其他开启的仓库就使用这个办法关闭。

7. 镜像yum源

上面只是将自己制作的rpm包,放入yum源。但还有一种企业需求,说的更具体一点,平时学生上课yum安装软件都是从公网下载的,占用带宽,因此在学校里搭建一个内网yum服务器,但又考虑到学生回家也要使用yum安装软件,如果yum软件的数据库文件repodata不一样,就会有问题。因此我想到的解决方法就是直接使用公网yum源的repodata。

  1. 镜像同步公网yum源
  2. 上游yum源必须要支持rsync协议,否则不能使用rsync进行同步。
  3. http://mirrors.ustc.edu.cn/status/
  4. CentOS官方标准源:rsync://mirrors.ustc.edu.cn/centos/
  5. epel源:rsync://mirrors.ustc.edu.cn/epel/
  6. 同步命令:
  7. # 使用rsync同步yum源,为了节省带宽、磁盘和下载时间,我只同步了CentOS6的rpm包,这样所有的rpm包只占用了21G,全部同步需要300G左右。
  8. # 同步base源,小技巧,我们安装系统的光盘镜像含有部分rpm包,大概3G,这些就不用重新下载。
  9. /usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /data/yum_data/centos/6/os/x86_64/
  10. /usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /data/yum_data/centos/6/extras/x86_64/
  11. /usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /data/yum_data/centos/6/updates/x86_64/
  12. # epel源
  13. /usr/bin/rsync -av --exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /data/yum_data/epel/6/x86_64/
  14. 学生使用内网yum源方法
  15. # 可以自建一个内网dns,如果没有,可使用hosts解析。
  16. echo '192.168.0.200 mirrors.aliyun.com' >>/etc/hosts
  17. 结果展示
  18. [root@KVM data]# du -sh yum_data
  19. 21G yum_data
  20. [root@KVM data]# tree -L 3 yum_data/
  21. yum_data/
  22. ├── centos
  23. │ ├── 6
  24. │ │ ├── extras
  25. │ │ ├── os
  26. │ │ └── updates
  27. │ └── RPM-GPG-KEY-CentOS-6
  28. ├── epel
  29. │ └── 6
  30. │ └── x86_64

 

有了, docker, 我们可以参照下面的方法进行了, 比较简单!

https://github.com/NatGuyton/docker-yumrepo

docker-yumrepo

Run a yum repo in docker, including repo updates in a docker run-n-exit command

Setup

See "runme" for an example - you can even use that script directly. It sets the host port to run the http server on as well as mounts a host directory (so that you can persist your RPMs) inside the container.

Repo updates

See "runme_update" - configured similarly to "runme", this expects that you have put new rpms (or removed some) in the RPMS dir that's mounted into the yumrepo container. It will generate a new yum data structure reflecting the current contents.

Possible enhancements

  • https

 

https://github.com/NatGuyton/docker-yumrepo

发表评论