一、基础环境 1、在tvm-base的基础上,复制一个镜像为tvm-yum来测试。 2、网络: eth0:host-only(用于虚拟内网,手动固定IP,这样从宿主机可以直接连接到这个vm) eth1:NAT(用于上外网,动态IP) [[email protected] ~]# cd /etc/sysconfig/network-scripts/ [[email protected] network-scripts]# cat ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.56.254 PREFIX=24 GATEWAY=192.168.56.1 [[email protected] network-scripts]# cat ifcfg-eth1 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=dhcp [[email protected] ~]# ip a s dev eth0 |grep global inet 192.168.56.254/24 brd 192.168.56.255 scope global eth0 [[email protected] ~]# ip a s dev eth1 |grep global inet 10.0.3.15/24 brd 10.0.3.255 scope global eth1 [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.3.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1 0.0.0.0 10.0.3.2 0.0.0.0 UG 0 0 0 eth1 2、初步计划是: yum仓库,在hosts配置加入: 192.168.56.254 mirrors.office.test 然后这样访问yum仓库: http://mirrors.office.test/centos 接下来需要: 1)配置一个local yum 镜像,并定时从公网的mirrors同步数据。 注1:不需要i386目录,此处只用x86_64的版本。 注2:不需要isos目录,里面存放的是iso文件。 2)配置一个http服务。 3)提供local-office.repo文件,供客户端用。 二、配置目录和脚本,制作本地的yum镜像 [[email protected] ~]# mkdir -p /data/yum/repo/centos/ /data/download/ /data/ops/bin/ 1、上传centos6.5-x64的iso文件到/data/download/ [[email protected] ~]# ls /data/download/ CentOS-6.5-x86_64-bin-DVD1.iso CentOS-6.5-x86_64-bin-DVD2.iso [[email protected] ~]# mkdir -p /data/yum/repo/centos/6/os/x86_64 2、同步iso文件中的内容: [[email protected] ~]# mount -ro loop /data/download/CentOS-6.5-x86_64-bin-DVD1.iso /mnt [[email protected] ~]# rsync -avHPS /mnt/ /data/yum/repo/centos/6/os/x86_64/ [[email protected] ~]# umount /mnt [[email protected] ~]# mount -ro loop /data/download/CentOS-6.5-x86_64-bin-DVD2.iso /mnt [[email protected] ~]# rsync -avHPS /mnt/ /data/yum/repo/centos/6/os/x86_64/ [[email protected] ~]# umount /mnt 3、选一个mirrors,通过脚本从mirrors同步。 查找镜像列表:http://www.centos.org/download/mirrors/ 这里我选择了一个支持rsync协议的镜像:http://mirrors.hust.edu.cn/centos/ 注3:目前最新版本的是centos6.6,因此,实际上同步的后,这个仓库的版本是6.6,做个软连接,6 -> 6.6 [[email protected] ~]# ln -s /data/yum/repo/centos/6 /data/yum/repo/centos/6.6 开始同步 [[email protected] ~]# rsync -avzP --delete --delete-excluded --exclude "local*" --exclude "isos" --exclude "i386" rsync://mirrors.hust.edu.cn/centos/6/ /data/yum/repo/centos/6/ 三、配置http方式来访问yum镜像 1、防火墙放行80端口 2、配置httpd服务 1)若没有禁用selinux,则应当这样配置: [[email protected] ~]# chcon -Rv --type=httpd_sys_content_t /data/yum/repo/ 2)配置httpd服务: [[email protected] ~]# cat /etc/httpd/conf/httpd.conf |grep ServerName |grep ^[^#] ServerName 127.0.0.1 3)配置虚拟主机 [[email protected] ~]# mv /etc/httpd/conf.d/welcome.conf /tmp/ [[email protected] ~]# cat /etc/httpd/conf.d/mirrors.office.test.conf <VirtualHost *:80> ServerAdmin [email protected] ServerName mirrors.office.test DocumentRoot /data/yum/repo ErrorLog logs/mirrors.office.test-error_log CustomLog logs/mirrors.office.test-access_log common <Directory /data/yum/repo> options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> 3、启动httpd服务 [[email protected] ~]# chown -R apache:apache /data/yum/repo [[email protected] ~]# service httpd start 4、测试 宿主机配置hosts文件: 192.168.56.254 mirrors.office.test 访问: http://mirrors.office.test/centos 抓包信息: [[email protected] ~]# curl -o /dev/null -v -I http://mirrors.office.test/centos/ * About to connect() to mirrors.office.test port 80 (#0) * Trying 192.168.56.254... connected * Connected to mirrors.office.test (192.168.56.254) port 80 (#0) > HEAD /centos/ HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: mirrors.office.test > Accept: */* > < HTTP/1.1 200 OK < Date: Tue, 14 Jul 2015 02:32:14 GMT < Server: Apache/2.2.15 (CentOS) < Connection: close < Content-Type: text/html;charset=UTF-8 < % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Closing connection #0 chrome抓包信息: Remote Address:192.168.56.254:80 Request URL:http://mirrors.office.test/centos/ Request Method:GET Status Code:200 OK Remote Address:192.168.56.254:80 Request URL:http://mirrors.office.test/centos/ Request Method:GET Status Code:200 OK 四、提供local-office.repo文件 1、现有的目录: [[email protected] ~]# ls /data/yum/repo/centos/6/ centosplus cloud contrib cr extras fasttrack os SCL updates xen4 2、local-office.repo [[email protected] ~]# cat /etc/yum.repos.d/local-office.repo # local-office.repo # 2015/7/14 # 把Centos-Base.repo更新成自己的mirror,其余的repo移除。 # - 包含基础的os里面的rpm包 [base] name=CentOS-$releasever - Base baseurl=http://mirrors.office.test/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #released updates - 包含可以更新的包 [updates] name=CentOS-$releasever - Updates baseurl=http://mirrors.office.test/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #additional packages that may be useful - 包含额外的包 [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.office.test/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #additional packages that extend functionality of existing packages - 包含功能扩展的包 [centosplus] name=CentOS-$releasever - Plus baseurl=http://mirrors.office.test/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #contrib - packages by Centos Users - 包含用户贡献的包 [contrib] name=CentOS-$releasever - Contrib baseurl=http://mirrors.office.test/centos/$releasever/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 五、等mirros同步完成后,开始测试 1、本机测试 先清除缓存: [[email protected] ~]# yum clean all Loaded plugins: fastestmirror, security Cleaning repos: base extras updates Cleaning up Everything Cleaning up list of fastest mirrors 试着更新下系统: [[email protected] ~]# yum update (略) (369/369): yum-utils-1.1.30-30.el6.noarch.rpm | 110 kB 00:00 ----------------------------------------------------------------------------------------------------- Total 16 MB/s | 430 MB 00:26 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction (略) yum.noarch 0:3.2.29-60.el6.centos yum-plugin-fastestmirror.noarch 0:1.1.30-30.el6 yum-plugin-security.noarch 0:1.1.30-30.el6 yum-utils.noarch 0:1.1.30-30.el6 Replaced: libsss_autofs.x86_64 0:1.9.2-129.el6 Complete! [[email protected] ~]# [[email protected] ~]# cat /etc/issue CentOS release 6.6 (Final) Kernel \r on an \m 2、瞧,已经升级到6.6的版本,建议reboot一下先。 reboot前,先将httpd服务加入开机启动。 [[email protected] centos]# chkconfig httpd on [[email protected] centos]# chkconfig --list |grep httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [[email protected] ~]# cp /etc/yum.repos.d/local-office.repo /data/yum/repo/centos/ [[email protected] ~]# chown -R apache:apache /data/yum/repo/centos/ 用update升级系统后,在/etc/yum.repos.d/里面更新了CentOS相关的repo文件,因此在重启后可以考虑移除 [[email protected] ~]# mv CentOS-* /etc/yum.repos.d/ /tmp/ 3、配置脚本来同步,并放到crontab中执行 [[email protected] ~]# cat /data/ops/bin/repo_update.sh #!/bin/bash # # 2015/7/14 if [ -f /var/lock/subsys/repo_update ]; then echo "[`date`] yum repo update via rsync is already running." exit 0 fi if [ -d /data/yum/repo/centos/6 ]; then touch /var/lock/subsys/repo_update rsync -avzP --delete --delete-excluded --exclude "local*" --exclude "isos" --exclude "i386" rsync://mirrors.hust.edu.cn/centos/6/ /data/yum/repo/centos/6/ rm /var/lock/subsys/repo_update else echo "[`date`] [error] dir not proesent: /data/yum/repo/centos/6" fi chown -R apache:apache /data/yum/repo 放到crontab中 [[email protected] ~]# cat <<_NTP >>/var/spool/cron/$(whoami) # repo update 0 4 * * * /bin/bash /data/ops/bin/repo_update.sh >/tmp/repo.log 2>&1 & _NTP 六、TODO 1、加入epel源 [[email protected] ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm [[email protected] ~]# yum makecache 2、客户端使用local-office.repo文件 [[email protected] ~]# mv /etc/yum.repos.d/CentOS* /tmp/ && echo ‘192.168.56.254 mirrors.office.test‘ >> /etc/hosts && wget http://mirrors.office.test/centos/local-office.repo -O /etc/yum.repos.d/local-office.repo && yum makecach [[email protected] ~]# yum update 符合预期。 ZYXW、参考 1、Creating a local repo (Mirror) with CentOS 6.2+ http://darktraining.com/93/ 2、Creating Local Mirrors for Updates or Installs https://wiki.centos.org/HowTos/CreateLocalMirror 3、List of CentOS Mirrors http://www.centos.org/download/mirrors/ 4、How To Set Up and Use Yum Repositories on a CentOS 6 VPS https://www.digitalocean.com/community/tutorials/how-to-set-up-and-use-yum-repositories-on-a-centos-6-vps
时间: 2024-10-06 02:23:10