对于企业级开发团队,搭建软件包的镜像站点(以及Docker Images Registry镜像站点)是减小网络带宽占用、加速软件开发过程的必备措施。html
对与Ubuntu(以及其余基于deb的系统)来讲,通常有几种方法:nginx
上面的这几种方法都是使用apt-mirror来完成,须要配置镜像参数,指定须要的版本。git
若是须要完整的Ubuntu Archive镜像,能够编写一个脚本(参考:建立Ubuntu安装包服务镜像的脚本),使用rsync所有镜像Ubuntu archive仓库,速度更快,但会占用较大的磁盘空间(>1TB),初始同步须要较多的时间。而后,再建立一个Nginx实例提供服务。github
为了便于管理,我将同步脚本建立为一个容器,而后挂载到Kubernetes中的定时任务中执行。ubuntu
#/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)
#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 rsync COPY mirror-ubuntu.sh /home
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的数据开始同步。api
建立一个Nginx服务站点,将其主目录指向上面同步的同一个存储目录,而后开启目录浏览功能。浏览器
Kubernetes中的配置文件,内容以下:网络
apiVersion: v1 kind: ServiceAccount metadata: name: apt-mirror namespace: ipfs2 --- kind: Service apiVersion: v1 metadata: name: mirror-ubuntu-service namespace: ipfs2 labels: app: mirror-ubuntu-service spec: ports: - name: mirror-service port: 80 type: LoadBalancer selector: app: mirror-ubuntu-service --- kind: Deployment apiVersion: apps/v1 metadata: name: mirror-ubuntu-service namespace: ipfs2 spec: selector: matchLabels: app: mirror-ubuntu-service replicas: 1 strategy: type: Recreate template: metadata: labels: app: mirror-ubuntu-service spec: serviceAccount: apt-mirror containers: - name: mirror-ubuntu-service image: nginx ports: - name: mirror-service containerPort: 80 securityContext: capabilities: add: - DAC_READ_SEARCH - SYS_RESOURCE env: - name: RESYNC_PERIOD value: 2h imagePullPolicy: "IfNotPresent" volumeMounts: - name: mirror-volume mountPath: /usr/share/nginx/html subPath: mirror-ubuntu - name: mirror-volume mountPath: /etc/nginx/conf.d/ subPath: mirror-ubuntu/service-config volumes: - name: mirror-volume persistentVolumeClaim: claimName: ipfs-storage-ipfs2-ipfs-0
我在其中建立了一个帐户、一个Service和一个Nginx的Deployment。安装后,就能够经过浏览器来访问镜像站点了。app
第一次同步的时间比较长(下载将近1TB,通常要7天左右)。之后只是更新,就快多了。负载均衡
由于使用了Kubernertes,须要的话能够对Nginx服务站点进行伸缩,遇到故障时系统能够自动重启或节点漂移,能够知足大规模数据中心级的软件安装和更新的须要。为了更高的可靠性,Kubernetes集群自己应该配置Master高可用机制,存储系统应该有备份和多拷贝。
正如上面所述,这种镜像机制能够对内部网的软件安装和更新过程大幅度加速,可是目前传输速度仍是不够快,并且依赖于上级的镜像站点的可靠性。若是与BT和IPFS之类的p2p传输机制结合,将会进一步带来速度和可靠性的大幅度提高。
目前的状态,还存在一些障碍有待攻克,可是随着IPFS等的改进和FileCoin的推出和完善,这一方案最终是彻底可行的,留待后述。