https://blog.51cto.com/life2death/1708417
3.搭建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仓库中。
[root@m01 /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协议访问
创建一个本地源
[root@m01]# 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”
[root@m01 /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.yml
#本地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