公司有很多linux虚拟机,绝大部分都是centos7。但是不能连接外网,所以无法直接用yum下载安装软件包。现在需要搭建一台yum服务器,使用rsync同步yum源到本地,修改客户机yum配置,使其通过yum服务器下载更新软件包,

    思路:

    1、yum服务器两块网卡,一块连接公网出口访问外网,一块与其他linux客户机通信    

    2、使用rsync从远程仓库镜像同步到本地yum服务器,拉取centos7、epel和zabbix远程镜像站点yum仓库到本地

    3、yum服务器使用apache配置web服务,服务目录为/opt/yum_data,为其他linux客户机提供仓库web访问     

    4、使用ansible+shell脚本批量更改linux客户yum配置文件,修改yum源地址为本地yum服务器


一、配置yum服务器基础

systemctl stop firewalld.service
systemctl disable firewalld.service
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
setenforce 0
iptables -F
iptables-save

mkdir /opt/yum_data/centos/7/{extras,os,updates}/x86_64 -p    #创建centos7源镜像文件存放目录 
mkdir /opt/yum_data/epel/7/x86_64/ -p                         #创建epel源镜像文件存放目录
mkdir /opt/yum_data/zabbix/ -p                                #创建zabbix源镜像文件存放目录
cd /opt/yum_data/
tree

完成后的目录结构如下图所示

linux-centos7搭建本地yum服务并使用_第1张图片


二、配置web服务

yum install httpd
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak    #备份配置文件
vim /etc/httpd/conf/httpd.conf           #编辑配置文件 
DocumentRoot "/opt/yum_data/"  #用搜索 /DocumentRoot 的方法,修改服务器文档根目录为/opt/yum_data
     #修改路径

linux-centos7搭建本地yum服务并使用_第2张图片

chown -R apache.apache /opt/yum_data/ #修改目录归属
cd /etc/httpd/conf.d/   
mv conf.d/welcome.conf /tmp     #删除默认主页
systemctl start httpd
systemctl enable httpd #启动apache

使用浏览访问,出现如下表示web服务器配置成功

linux-centos7搭建本地yum服务并使用_第3张图片

三、rsync同步yum源

yum install rsync

确认rsync使用的yum源,由于国内好多yum镜像站点如阿里云不支持rsync方式,本次使用中科大的镜像源,一定注意url

CentOS7源:
//mirrors.ustc.edu.cn/centos/7/os/x86_64/
//rsync.mirrors.ustc.edu.cn/centos/7/extras/x86_64/
//rsync.mirrors.ustc.edu.cn/centos/7/updates/x86_64/
EPEL7源:
rsync://mirrors.ustc.edu.cn/epel/7/x86_64/
Zabbix源:
rsync://mirror.tuna.tsinghua.edu.cn/zabbix/

使用rsync同步yum源,注意放在后台运行

rsync -avz --progress --no-motd rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /opt/yum_data/centos/7/os/x86_64/ 
rsync -avz --progress --no-motd rsync://rsync.mirrors.ustc.edu.cn/centos/7/extras/x86_64/ /opt/yum_data/centos/7/extras/x86_64/ 
rsync -avz --progress --no-motd rsync://rsync.mirrors.ustc.edu.cn/centos/7/updates/x86_64/ /opt/yum_data/updates/7/os/x86_64/ 
rsync -avz --progress --exclude=debug --no-motd rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ /opt/yum_data/epel/7/x86_64/ 
rsync -avz --progress --exclude=rhel/7/SRPMS --no-motd rsync://mirror.tuna.tsinghua.edu.cn/zabbix/ /opt/yum_data/zabbix/

可以使用screen命令,为每个rsync创建一个窗口,保证可以长时间后台运行

等待漫长的更新时间

linux-centos7搭建本地yum服务并使用_第4张图片


四、安装createrepo

createrepo主要进行此程序主要用于生成创建yum仓库,创建索引信息。

yum install createrepo -y

createrepo /opt/yum_data/centos/7/os/x86_64/
createrepo /opt/yum_data/centos/7/extras/x86_64/
createrepo /opt/yum_data/centos/7/updates/x86_64/
createrepo /opt/yum_data/epel/7/x86_64/
createrepo /opt/yum_data/zabbix/


五、客户机配置修改仓库配置文件和测试

客户机上修改yum配置文件/etc/yum.repos.d/CentOS-Base.repo,为主要的配置文件,其余配置文件中的仓库默认均未启用

cd /etc/yum.repos.d/
cp CentOS-Base.repo CentOS-Base.repo.bak
vim CentOS-Base.repo
===================以下为修改的后配置文件=================
# CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/os/x86_64/
gpgcheck=0                            #代表不进行软件包的GPG签名

#released updates
[updates]
name=CentOS-$releasever - Updates - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/updates/x86_64/
gpgcheck=0

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/extras/x86_64/
gpgcheck=0

#additional packages that extend functionality of existing packages
[epel]
name=CentOS-$releasever - epel - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/epel/7/x86_64/
gpgcheck=0


[zabbix]
name=CentOS-$releasever - zabbix - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/zabbix/
gpgcheck=0

linux-centos7搭建本地yum服务并使用_第5张图片


然后客户机更新yum源

yum clean all
yum makecache

blob.png

证明可以利用仓库文件连接到yum服务器

使用yum下载zabbix

linux-centos7搭建本地yum服务并使用_第6张图片



六、编写yum源更新脚本

编写yum_repo.sh脚本,自动更新yum源为yum服务器。

#!/bin/bash
#change yum source
#name=yum_repo.sh

echo "========start============="
cd /etc/yum.repos.d/

echo "====backup repo==========="
mv CentOS-Base.repo CentOS-Base.repo.bak

echo "=create CentOS-Base.repo=="

touch CentOS-Base.repo
cat > CentOS-Base.repo << EOF
# CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/os/x86_64/
gpgcheck=0

#released updates
[updates]
name=CentOS-$releasever - Updates - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/updates/x86_64/
gpgcheck=0

#released updates
[updates]
name=CentOS-$releasever - Updates - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/updates/x86_64/
gpgcheck=0

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/centos/7/extras/x86_64/
gpgcheck=0

#additional packages that extend functionality of existing packages
[epel]
name=CentOS-$releasever - epel - 172.31.101.100
failovermethod=priority
baseurl=http://172.31.101.100/epel/7/x86_64/
gpgcheck=0

[zabbix]

name=CentOS-$releasever - zabbix - 172.31.101.100

failovermethod=priority

baseurl=http://172.31.101.100/zabbix/

gpgcheck=0


EOF


echo "====upgrade yum============"

yum clean all

yum makecache

yum update -y


echo "====dowload tools========="

yum install -y net-tools vim wget


cd ~


echo "=========finish============"


然后使用ansible将脚本分发到各个客户端,使用ansible远程执行shell命令运行脚本,批量更新yum源