对于企业级开发团队,搭建软件包的镜像站点(以及Docker Images Registry镜像站点)是减少网络带宽占用、加速软件开发过程的必备措施。
- 本文持续更新,地址 https://my.oschina.net/u/2306127/blog/2032372
- 本文代码仓库,请按照自己的资源配置参数进行修改使用。
1、基本用法
对与Ubuntu(以及其他基于deb的系统)来说,一般有几种方法:
- 建立Ubuntu apt的本地Mirror,使用apt-mirror来做,比较简单。适合小规模团队(<20人)。
- 使用容器技术搭建apt镜像站,将镜像工具和镜像服务(apt-mirror-http-server)都放到了容器里运行,更好管理。适合中等规模团队(<50人)。
- 使用Kubernetes建立apt镜像服务,将镜像工具和镜像服务放到K8s集群中运行。可以满足任何规模团队的需要。
上面的这几种方法都是使用apt-mirror来完成,需要配置镜像参数,指定需要的版本。
2、高级用法
如果需要完整的Ubuntu Archive镜像,可以编写一个脚本(参考:创建Ubuntu安装包服务镜像的脚本),使用rsync全部镜像Ubuntu archive仓库,速度更快,但会占用较大的磁盘空间(>1TB),初始同步需要较多的时间。然后,再创建一个Nginx实例提供服务。
第一步,创建CronJob
为了便于管理,我将同步脚本创建为一个容器,然后挂载到Kubernetes中的定时任务中执行。
A、同步脚本
-
内容如下:
#/bin/dash
fatal() { echo "$1" exit 1 }
warn() { echo "$1" }
Find a source mirror near you which supports rsync on
https://launchpad.net/ubuntu/+archivemirrors
rsync://<iso-country-code>.rsync.archive.ubuntu.com/ubuntu should always work
#RSYNCSOURCE=rsync://archive.ubuntu.mirror.isp.com/ubuntu
实验发现rsync不通了,用下面这个:
RSYNCSOURCE=archive.ubuntu.com::ubuntu
Define where you want the mirror-data to be on your mirror
#BASEDIR=/var/www/ubuntuarchive/
改成自己的目录:
#BASEDIR=/media/smw/Appdata/ipfs-export/mirrors/ubuntu BASEDIR=/home/mirror-ubuntu
echo "From:" $RSYNCSOURCE echo "To:" $BASEDIR
if [ ! -d ${BASEDIR} ]; then warn "${BASEDIR} does not exist yet, trying to create it..." mkdir -p ${BASEDIR} || fatal "Creation of ${BASEDIR} failed." fi
rsync --recursive --times --links --safe-links --hard-links
--stats
--exclude "Packages*" --exclude "Sources*"
--exclude "Release*" --exclude "InRelease"
${RSYNCSOURCE} ${BASEDIR} || fatal "First stage of sync failed."rsync --recursive --times --links --safe-links --hard-links
--stats --delete --delete-after
${RSYNCSOURCE} ${BASEDIR} || fatal "Second stage of sync failed."date -u > ${BASEDIR}/project/trace/$(hostname -f)
B、容器创建Dockerfile
-
内容如下:
#This Docker Mirror Ubuntu Archive to a persistent volume of kubernetes. #Created by openthings,2018-09-04. NO WARRANTS. #Please visit https://github.com/openthings/kubernetes-tools/mirror-ubuntu. FROM ubuntu:16.04
RUN apt update &&
apt upgrade -y RUN apt install -y rsyncCOPY mirror-ubuntu.sh /home
C、定时任务CronJob
-
内容如下:
apiVersion: batch/v1beta1 kind: CronJob metadata: name: mirror-ubuntu-cronjob namespace: ipfs2 spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: restartPolicy: OnFailure containers: - name: mirror-ubuntu image: openthings/mirror-ubuntu args: - /bin/sh - /home/mirror-ubuntu.sh imagePullPolicy: "IfNotPresent" volumeMounts: - name: mirror-volume mountPath: /home/mirror-ubuntu subPath: mirror-ubuntu volumes: - name: mirror-volume persistentVolumeClaim: claimName: ipfs-storage-ipfs2-ipfs-0
将上面的内容保存为文件,然后运行Docker build进行容器构建和Kubectl apply安装,即可看到Kubernetes集群中job和pod被创建出来,然后Ubuntu Archive的数据开始同步。
- 注意,这里的ipfs-storage-ipfs2-ipfs-0是我为了下一步的工作,与IPFS服务共用的存储卷,你可以改成使用自己的PVC存储卷声明。
第二步,创建Nginx服务
创建一个Nginx服务站点,将其主目录指向上面同步的同一个存储目录,然后开启目录浏览功能。
Kubernetes中的配置文件,内容如下:
1apiVersion: v1 2kind: ServiceAccount 3metadata: 4 name: apt-mirror 5 namespace: ipfs2 6--- 7kind: Service 8apiVersion: v1 9metadata: 10 name: mirror-ubuntu-service 11 namespace: ipfs2 12 labels: 13 app: mirror-ubuntu-service 14spec: 15 ports: 16 - name: mirror-service 17 port: 80 18 type: LoadBalancer 19 selector: 20 app: mirror-ubuntu-service 21--- 22kind: Deployment 23apiVersion: apps/v1 24metadata: 25 name: mirror-ubuntu-service 26 namespace: ipfs2 27spec: 28 selector: 29 matchLabels: 30 app: mirror-ubuntu-service 31 replicas: 1 32 strategy: 33 type: Recreate 34 template: 35 metadata: 36 labels: 37 app: mirror-ubuntu-service 38 spec: 39 serviceAccount: apt-mirror 40 containers: 41 - name: mirror-ubuntu-service 42 image: nginx 43 ports: 44 - name: mirror-service 45 containerPort: 80 46 securityContext: 47 capabilities: 48 add: 49 - DAC_READ_SEARCH 50 - SYS_RESOURCE 51 env: 52 - name: RESYNC_PERIOD 53 value: 2h 54 imagePullPolicy: "IfNotPresent" 55 volumeMounts: 56 - name: mirror-volume 57 mountPath: /usr/share/nginx/html 58 subPath: mirror-ubuntu 59 - name: mirror-volume 60 mountPath: /etc/nginx/conf.d/ 61 subPath: mirror-ubuntu/service-config 62 volumes: 63 - name: mirror-volume 64 persistentVolumeClaim: 65 claimName: ipfs-storage-ipfs2-ipfs-0
我在其中创建了一个账户、一个Service和一个Nginx的Deployment。安装后,就可以通过浏览器来访问镜像站点了。
- 其中,映射了两个卷,一个为数据卷、一个为Nginx的配置文件,都对应到主存储PVC的子目录中。
- Nginx为官网的镜像(没有任何定制修改),启动时从配置子目录读取参数,启用目录浏览功能。
- 服务使用了LoadBalancer,本地集群可以安装MetalLB来实现,云上使用厂商提供的负载均衡器。
第一次同步的时间比较长(下载将近1TB,一般要7天左右)。以后只是更新,就快多了。
因为使用了Kubernertes,需要的话可以对Nginx服务站点进行伸缩,遇到故障时系统可以自动重启或节点漂移,可以满足大规模数据中心级的软件安装和更新的需要。为了更高的可靠性,Kubernetes集群本身应该配置Master高可用机制,存储系统应该有备份和多拷贝。
3、极速方法
正如上面所述,这种镜像机制可以对内部网的软件安装和更新过程大幅度加速,但是目前传输速度还是不够快,而且依赖于上级的镜像站点的可靠性。如果与BT和IPFS之类的p2p传输机制结合,将会进一步带来速度和可靠性的大幅度提升。
目前的状态,还存在一些障碍有待攻克,但是随着IPFS等的改进和FileCoin的推出和完善,这一方案最终是完全可行的,留待后述。