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

Stella981
• 阅读 444

对于企业级开发团队,搭建软件包的镜像站点(以及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://.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的推出和完善,这一方案最终是完全可行的,留待后述。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这