TUNASYNC镜像源后端

为了企业内部建立一些自有的镜像源,因此需要搭一个自有的镜像站。前期看过一些文章,有用原生apt-mirror,rsync等。同时现有做的好的就是阿里源清华源,清华开源了他们的镜像同步管理服务,因此本着不重复造轮子(因为菜)的原则,直接上他!

1. 资源准备

原则上来讲同步服务器的配置相对高点性能好些,特别需要磁盘。由于我是用的公司内部云平台,直接往死里造。建议8C32G起。至于数据盘,根据你要的同步的源大小来定,可以在同步状态中具体查看到。

2. 架构设计

下图来自清华。其主要分为两部分,Manager和Worker。这两部分均可在同一节点

Manager:管理Worker及Worker下的jobs

Worker: 同步镜像,一个配置文件即可启动一个worker。一个Worker下有多个Job,每个job对应一个镜像源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Architecture

- Manager: Central instance for status and job management
- Worker: Runs mirror jobs

+------------+ +---+ +---+
| Client API | | | Job Status | | +----------+ +----------+
+------------+ | +----------------->| |--->| mirror +---->| mirror |
+------------+ | | | w | | config | | provider |
| Worker API | | H | | o | +----------+ +----+-----+
+------------+ | T | Job Control | r | |
+------------+ | T +----------------->| k | +------------+ |
| Job/Status | | P | Start/Stop/... | e | | mirror job |<----+
| Management | | S | | r | +------^-----+
+------------+ | | Update Status | | +---------+---------+
+------------+ | <------------------+ | | Scheduler |
| BoltDB | | | | | +-------------------+
+------------+ +---+ +---+


# Job Run Process


PreSyncing Syncing Success
+-----------+ +----------+ +-----------+ +-------------+ +--------------+
| pre-job +--+->| pre-exec +--->| job run +--->| post-exec +-+-->| post-success |
+-----------+ ^ +----------+ +-----------+ +-------------+ | +--------------+
| |
| +-----------------+ | Failed
+----------------+ post-fail |<---------------+
+-----------------+

2. 搭建步骤

镜像管理器源码使用go语言编写,可以自行编译,同时清华也提供了编译好的release文件,本着别人的最香原则(人太懒了)直接用!

前期资源准备

下载文件

1
wget https://github.com/tuna/tunasync/releases/download/v0.8.0/tunasync-linux-amd64-bin.tar.gz

构建目录及解压包

1
2
3
4
5
mkdir /etc/tunasync			//镜像管理器目录
mkdir /etc/tunasync/conf //镜像管理器配置文件
mkdir /etc/tunasync/log //镜像管理器日志
mkdir /mirrors //镜像数据目录。请将数据盘挂载至该目录
tar -xzvf tunasync-linux-amd64-bin.tar.gz -C /usr/bin/ //解压执行文件,不再额外配置环境变量

配置文件

所有配置文件均放在/etc/tunasync/conf目录下

manager.conf

1
2
3
4
5
6
7
8
9
10
11
12
debug = false	//是否开启debug日志

[server]
addr = "127.0.0.1" //manager服务监听地址
port = 14242 //端口这里建议最好写14242,后面tunasynctl命令可不指定端口
ssl_cert ="" //ssl 配置,不需要 ssl 的不用配置
ssl_key = ""

[files]
db_type = "bolt" //go自带数据库,可无需修改
db_file = "/etc/tunasync/manager.db" //指定数据库文件位置,会生成一个manager.db文件
ca_cert = ""

worker-centos.conf:这里是一个centos的worker配置,可认为一个配置文件一个worker, 官方示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[global]
name = "centos-worker" //worker id
log_dir = "/etc/tunasync/log/{{.Name}}" //tunasync日志路径
mirror_dir = "/mirrors" //镜像的路径,指定到数据盘的挂载地址
concurrent = 10 //线程数
interval = 1440 //同步周期,单位分钟

[manager]
api_base = "http://127.0.0.1:14242" //manager地址,需与 manager 所在服务器配置保持一致
token = ""
ca_cert = ""

[cgroup]
enable = false
base_path = "/sys/fs/cgroup"
group = "tunasync"

[server]
hostname = "localhost" //worker地址
listen_addr = "127.0.0.1"
listen_port = 6000 //一个worker一个端口,不可重复
ssl_cert = ""
ssl_key = ""

[[mirrors]]
name = "centos" //镜像名称,tunasync 会在镜像根目录下建立一个该名称的目录用于下载镜像
provider = "rsync" //同步方式:有rsync和command
upstream = "rsync://mirrors.tuna.tsinghua.edu.cn/centos/" //上游地址,注意,参数最后需要有“/”,否则启动 tunasync 时会报错
use_ipv6 = false

[[mirrors]] //个人认为一个worker一个mirrors,下面这个可以去掉
name = "epel"
provider = "rsync"
upstream = "rsync://rsync.mirrors.ustc.edu.cn/epel/"
use_ipv6 = false

mirrors的provider提供了两种方式,一种是rsync端对端的增量同步,另一种是command,可以编写一个脚本来执行同步,更多的自定义镜像脚本

官方

下载manager和worker启停脚本(该步骤可省略,没太大作用)

1
2
3
4
5
6
7
8
9
10
11
wget -P /etc/init.d/ https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-manager
wget -P /etc/init.d/ https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-worker
chmod +x /etc/init.d/tunasync-manager
chmod +x /etc/init.d/tunasync-worker

vi /etc/init.d/tunasync-manager
vi /etc/init.d/tunasync-worker
两个文件中 OPTIONS= 所在的一行分别改为:

OPTIONS="manager --config /etc/tunasync/conf/manager.conf"
OPTIONS="worker --config /etc/tunasync/conf/worker-centos.conf"

启动服务

启动manager服务

1
2
tunasync manager --config /etc/tunasync/conf/manager.conf >> /etc/tunasync/log/manager.log &	//建议用这个
或者tunasync-manager start //使用方法可以直接看他的脚本

启动worker服务

1
tunasync worker --config /etc/tunasync/conf/worker-centos.conf >> /etc/tunasync/log/worker-centos.log &

至此服务搭建就完成了

3. 提供 http 服务

这里我采用nginx提供web访问服务

1
2
3
rpm -Uvh  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx //安装nginx
systemctl start nginx //启动nginx
1
vi /etc/nginx/conf.d/tunasync.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80 default_server;
server_name _;
root /mirrors;
location ~ ^/ {
index index.html index.htm;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
error_page 500 502 503 504 /50x.html;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}

location ~ .*\.(js|css)?$ {
expires 12h;
}

access_log /etc/tunasync/nginx/nginx_access.log;
error_log /etc/tunasync/nginx/nginx_error.log;
}
1
2
3
nginx -t		//校验配置是否正确
nginx -s stop //停止nginx
nginx启动nginx

之后可在页面访问本机ip查看文件

4. tunasynctl管理

tunasync 还提供了一个管理工具 tunasynctl,可以用于对镜像服务做更多维度的管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
AME:
tunasynctl - control client for tunasync manager

USAGE:
tunasynctl [global options] command [command options] [arguments...]

VERSION:
0.8.0

COMMANDS:
list List jobs of workers
flush Flush disabled jobs
workers List workers
rm-worker Remove a worker
set-size Set mirror size
start Start a job
stop Stop a job
disable Disable a job
restart Restart a job
reload Tell worker to reload configurations
ping
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version

1
2
tunasynctl list --all
tunasynctl workers

通过该接口可生成一个json文件,包含已同步的镜像名称,同步时间,上游地址等

1
wget -c http://localhost:14242/jobs

更多tunasynctl操作