首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自建Debian/CentOS镜像站

自建Debian/CentOS镜像站

原创
作者头像
RokasYang
修改2022-04-23 21:03:45
4.8K1
修改2022-04-23 21:03:45
举报
文章被收录于专栏:RokasYangRokasYang

搭建Debian镜像站

一、使用场景

自建镜像站基本有两大使用场景:

  • 提升软件包拉取速度,安装在有大带宽的机器上提供服务;
  • 局域网客户端无外网访问能力,需搭建内网镜像站作为源站。

二、安装apt-mirror

这里以Debian streach为例,其他版本同理,服务器所用的系统并不需要和搭建的镜像站匹配,一个系统上可以搭建多个操作系统、不同代号版本的镜像站,你可以理解为镜像站只是基于HTTP服务提供文件下载、实时更新的功能即可。

apt-get install apt-mirror

三、配置镜像站源站

配置文件所在路径:/etc/apt/mirror.list

设置源站地址,即从哪里同步拉取镜像仓库,这里以清华源为例,设置源站为Debian11 bullseye版本的源站地址:

############# config ##################
#
# set base_path    /var/spool/apt-mirror
#
# set mirror_path  $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

# mirror additional architectures
#deb-alpha http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-amd64 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-armel http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-hppa http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-i386 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-ia64 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-m68k http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-mips http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-mipsel http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-powerpc http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-s390 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-sparc http://ftp.us.debian.org/debian unstable main contrib non-free

clean http://ftp.cn.debian.org/debian

四、设置定时任务

增加一个定时任务,每天定时向上游源(清华源)同步更新镜像站

crontab -e

写入:

0 23 * * * apt-mirror  #每天23点执行一次apt-mirror,定时向上游更新镜像站文件
0 22 * * * /var/spool/apt-mirror/var/clean.sh  #提前一个小时清理被上游镜像站遗弃的软件包

如果想立马拉取,临时改成特定时间点即可,或者后台运行(第一次拉取会花很长时间,不建议前台执行):

nohup apt-mirror &

五、修正apt-mirror脚本bug

apt-mirror有个小bug,可以参考bug report页面,它不处理.xz后缀的压缩文件,会导致同步后有些版本仓库404,比如bullseye-backports

修正方法也很简单:

vim $(which apt-mirror)  #修改apt-mirror脚本

大概在第523行,正则匹配后面加上.xz后缀情况就行:

#修改前:
                    if ( $filename =~ 
m{^$component/i18n/Translation-[^./]*\.bz2$} )
#修改后:
                    if ( $filename =~ 
m{^$component/i18n/Translation-[^./]*\.(bz2|xz)$} )

或者使用sed命令写入即可:

sed -ine '523s/bz2/(bz2|xz)/g' $(which apt-mirror) 

六、拉取路径配置与web

apt-mirror拉取镜像站默认存储到的路径为:/var/spool/apt-mirror,通过/etc/apt/mirror.listset base_path指定,如果需要修改路径,修改此参数即可。

也可以将拉取的路径设置一个软链接到web路径:

ln -sf /var/spool/apt-mirror/mirrors.tuna.tsinghua.edu.cn/ /var/www/mirror

之后在web配置文件里指定路径为/var/www/mirror即可,同时需要开启允许列出目录文件,否则会403,以Nginx为例:

root /var/www/mirror;

location / {
    autoindex on;
}

七、设置源站为自建站

$ sudo vim /etc/apt/sources.list  #修改成自建站
$ sudo apt-get update   #更新软件源

八、拉取软件效果

完成后用浏览器访问效果如图:

拉取速度:

内网速度越快提升则越明显。

搭建Centos镜像站

一、使用场景

自建镜像站基本有两大使用场景:

  • 提升软件包拉取速度,安装在有大带宽的机器上提供服务;
  • 局域网客户端无外网访问能力,需搭建内网镜像站作为源站。

二、同步脚本与定时任务配置

Centos的镜像站搭建更为简单,只需要通过rsync将软件同步到本地仓库就行。

以centos7即epel源为例,如需拉取多个版本,甚至全版本,修改版本号或某个大目录即可。

创建一个同步脚本centos_rsync.sh

$ mkdir -p /server/scripts/
$ cd $_   
$ vim centos_rsync.sh
#!/bin/bash
rsync -avrt rsync://mirrors4.tuna.tsinghua.edu.cn/epel/7/ /var/www/mirror/centos/epel/7/  
rsync -avrt rsync://mirrors4.tuna.tsinghua.edu.cn/centos/7/ /var/www/mirror/centos/7/
$ chmod + x centos_rsync.sh

之后写入到定时任务即可:

crontab -e

内容如下:

00 12 * * * /server/scripts/centos_rsync.sh  #每天中午12点执行更新与同步

三、拉取路径配置与web

与Debian同理,将拉取的路径设置为web路径即可,确保客户端能通过HTTP方式GET到文件。

以Nginx为例,配置对应路径,开启autoindex

root /var/www/mirror;

location / {
    autoindex on;
}

四、修改源站为自建站

1.修改base源

将域名替换成自建的镜像站地址即可:

$ cd /etc/yum.repos.d/
$ vim CentOS-Base.repo
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.linux-code.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.linux-code.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.linux-code.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.linux-code.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
$
2.修改epel源

将域名替换成自建的镜像站地址即可:

$ cd /etc/yum.repos.d/
$ vim epel-7.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.linux-code.com/centos/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.linux-code.com/centos/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.linux-code.com/centos/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
$
3.生成缓存

删除之前的缓存,并生成新缓存:

yum clean all
yum makecache

五、拉取软件效果

完成后用浏览器访问效果如图:

拉取速度:

写在最后

自建镜像站,需要足够大的磁盘,同时磁盘读写能力、内网传输速度、系统负载、内存(buffer很重要)大小都会影响软件拉取速度。

单个版本,如Debian11Centos7+epel,给500G左右空间就好,如果涉及到全版本,建议上4-8T NAS盘。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 搭建Debian镜像站
    • 一、使用场景
      • 二、安装apt-mirror
        • 三、配置镜像站源站
          • 四、设置定时任务
            • 五、修正apt-mirror脚本bug
              • 六、拉取路径配置与web
                • 七、设置源站为自建站
                  • 八、拉取软件效果
                  • 搭建Centos镜像站
                    • 一、使用场景
                      • 二、同步脚本与定时任务配置
                        • 三、拉取路径配置与web
                          • 四、修改源站为自建站
                            • 1.修改base源
                            • 2.修改epel源
                            • 3.生成缓存
                          • 五、拉取软件效果
                          • 写在最后
                          相关产品与服务
                          容器镜像服务
                          容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档