您现在的位置是:首页 > 文章详情

Kubernetes建立数据中心级apt镜像站点

日期:2018-12-12点击:305

对于企业级开发团队,搭建软件包的镜像站点(以及Docker Images Registry镜像站点)是减少网络带宽占用、加速软件开发过程的必备措施。

1、基本用法


对与Ubuntu(以及其他基于deb的系统)来说,一般有几种方法:

上面的这几种方法都是使用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 rsync COPY 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中的配置文件,内容如下:

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。安装后,就可以通过浏览器来访问镜像站点了。

  • 其中,映射了两个卷,一个为数据卷、一个为Nginx的配置文件,都对应到主存储PVC的子目录中。
  • Nginx为官网的镜像(没有任何定制修改),启动时从配置子目录读取参数,启用目录浏览功能。
  • 服务使用了LoadBalancer,本地集群可以安装MetalLB来实现,云上使用厂商提供的负载均衡器。

第一次同步的时间比较长(下载将近1TB,一般要7天左右)。以后只是更新,就快多了。

因为使用了Kubernertes,需要的话可以对Nginx服务站点进行伸缩,遇到故障时系统可以自动重启或节点漂移,可以满足大规模数据中心级的软件安装和更新的需要。为了更高的可靠性,Kubernetes集群本身应该配置Master高可用机制,存储系统应该有备份和多拷贝。

3、极速方法

正如上面所述,这种镜像机制可以对内部网的软件安装和更新过程大幅度加速,但是目前传输速度还是不够快,而且依赖于上级的镜像站点的可靠性。如果与BT和IPFS之类的p2p传输机制结合,将会进一步带来速度和可靠性的大幅度提升。

目前的状态,还存在一些障碍有待攻克,但是随着IPFS等的改进和FileCoin的推出和完善,这一方案最终是完全可行的,留待后述。

本文转自开源中国-Kubernetes建立数据中心级apt镜像站点

原文链接:https://yq.aliyun.com/articles/679238
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章