1. 搭建 yum 仓库, 准备需要的软件
- mlocate lrzsz tree VIM nc nmap lrzsz wget bash-completion bash-completion-extras cowsay sl htop iotop iftop lsof.NET-tools sysstat unzip bc psmisc ntpdate wc telnet-server bind-utils createrepo
- gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-tools
还有架构中要用到的软件与依赖环境也要下载放到仓库中
服务端 - m01 管理机
提前下载好 nginx
yum install nginx -y
把 rpm 包及其相关依赖全部都下载到本地, 保存好.
手动在 / etc/yum.repos.d / 目录下配置本地仓库信息.
使用 createrepo 命令生成 repodata 信息.
使用 yum repoinfo 检查确认.
yum 仓库本地存放 rpm 包的路径:/yum/
创建一个本地仓库的目录
- mkdir /yum/
- yum install --downloadonly --downloaddir=/yum/ mlocate lrzsz tree VIM nc nmap lrzsz wget bash-completion bash-completion-extras cowsay sl htop iotop iftop lsof.NET-tools sysstat unzip bc psmisc ntpdate wc telnet-server bind-utils gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-tools
只下载不安装的命令:
--downloadonly 只下载不安装
--downloaddir 指定 rpm 包的下载路径
1. 修改 yum 配置文件, 打开本地缓存, 1 为开启, 0 为不开启
这里要把 keepcache 打开
从外网下载的 rpm 包可以到缓存目录 / var/cache/yum / 下查找, 可以将本地缓存下面需要的包都移动到 yum 仓库中.
- [[email protected] /etc/yum.repos.d]# cat /etc/yum.conf
- [main]
- cachedir=/var/cache/yum/$basearch/$releasever
- keepcache=1
- debuglevel=2
- logfile=/var/log/yum.log
- exactarch=1
- obsoletes=1
- gpgcheck=1
- plugins=1
- installonly_limit=5
- ...
2. 安装 createrepo
- # 创建一个本地仓库的目录
- mkdir /yum/
- # 安装
- yum install createrepo -y
- # 生成索引文件
- createrepo /yum/
3. 每次下载了新的 rpm 包需要执行此命令
createrepo --update /yum/
--update 如果元数据已经存在, 且软件仓库中只有部分软件发生了改变或增减, 则可用 update 参数直接对原有元数据进行升级, 效率比重新分析 rpm 包依赖并生成新的元数据要高很多.
4. 通过 http 协议访问
创建一个本地源
- [[email protected]]# VIM /etc/yum.repos.d/yum-Media.repo
- [yum_ck] #本地仓库名
name=CentOS- - 仓库 #注释信息
- baseurl=http://10.0.0.61:8888/ #http 协议, rpm 包存放路径
- gpgcheck=0 #关闭安全验证
- enabled=1 #启用本仓库
- priority=1 #优先级为 1, 最高
通过 http 协议访问
配置. conf 文件
不要与 ngin.conf 文件冲突
- VIM /etc/nginx/conf.d/yum_ck.conf
- server {
- listen 8888;
- server_name 10.0.0.61;
- root /yum;
- index index.html;
- location / {
- root /yum;
- autoindex on;
- }
- }
重启 nginx
- nginx -t
- systemctl start nginx
5. 清除 yum 缓存
yum clean all
6. 下载一款软件放到 / yum / 下测试
- yum install --downloadonly --downloaddir=/yum nfs-utils
- # 执行此命令检查是否是从本地源找的包
- yum provides nfs-utils
- # 显示结果为本地仓库名代表成功
- Repo : @yum_ck
7. 优先级设置
1. 下载第三方插件
yum install yum-plugin-priorities.noarch
2. 检查是否启动服务 (默认是开启的)
- cat /etc/yum/pluginconf.d/priorities.conf
- [main]
- enabled = 1
3. 修改源的优先级 (新建的本地源在创建时已添加优先级为 1)
VIM /etc/yum.repos.d/CentOS-Base.repo
代码中已注释 "# 添加优先级为 2"
- [[email protected] /etc/yum.repos.d]# VIM CentOS-Base.repo
- [base]
- name=CentOS-$releasever - Base - mirrors.aliyun.com
- failovermethod=priority
- baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
- gpgcheck=1
- gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- priority=2 #添加优先级为 2
- #released updates
- [updates]
- name=CentOS-$releasever - Updates - mirrors.aliyun.com
- failovermethod=priority
- baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
- gpgcheck=1
- gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- priority=2 #添加优先级为 2
- #additional packages that may be useful
- [extras]
- name=CentOS-$releasever - Extras - mirrors.aliyun.com
- failovermethod=priority
- baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
- gpgcheck=1
- gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- priority=2 #添加优先级为 2
- #additional packages that extend functionality of existing packages
- [centosplus]
- name=CentOS-$releasever - Plus - mirrors.aliyun.com
- failovermethod=priority
- baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
- gpgcheck=1
- enabled=0
- gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- priority=2 #添加优先级为 2
- #contrib - packages by CentOS Users
- [contrib]
- name=CentOS-$releasever - Contrib - mirrors.aliyun.com
- failovermethod=priority
- baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
- gpgcheck=1
- enabled=0
- gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- priority=2 #添加优先级为 2
8. 对本地仓库进行更新
下载一个新的 rpm 软件包到本地仓库, 此时我们使用'yum repoinfo yum_ck'查看会发现软件包的数量并没有增加, 我们安装新增的软件包也会提示, 找不到此软件包的现象, 可以按照下述步骤, 更新仓库信息.
yum_ck === 本地仓库的名称
查看旧的软件包总数 yum repoinfo yum_ck | grep pkgs
更新本地仓库 createrepo --update /yum/
清除所有缓存 yum clean all
查看新的软件包总数 yum repoinfo yum_ck | grep pkgs
如果软件包的数量增加, 说明仓库更新成功.
9. 将配置推送到所有的客户端
- yum -y install yum-plugin-priorities.noarch
- /etc/yum.repos/yum-Media.repo
- /etc/yum.repos.d/CentOS-Base.repo
用 ansible 剧本实现, 在本地配置好
这里的 / etc/ansible/file 路径是根据自己的环境创建的, 只是为了推送这个本地源的配置文件, 推送到客户端的路径不要写错就好.
- [root@m01 /etc/ansible]# cat /etc/ansible/yum_ck.YAML
- # 本地 yum 仓库搭建
- ---
- - hosts: all
- tasks:
- - name: cp yum-Media.repo
- copy:
- src: /etc/ansible/file/yum-Media.repo
- dest: /etc/yum.repos.d/
- force: yes
- - name: copy .repo
- copy:
- src: /etc/ansible/file/CentOS-Base.repo
- dest: /etc/yum.repos.d/CentOS-Base.repo
- force: yes
- - name: yum-plugin-priorities.noarch
- yum:
- name: yum-plugin-priorities.noarch
- state: present
来源: http://www.bubuko.com/infodetail-3276002.html