使用Kubernetes創建apt鏡像服務

在安裝Ubuntu和Debian操做系統時,經過在局域網創建一個鏡像站點,能夠大幅度加速apt軟件包安裝過程,同時減小主服務器的負擔。git

這裏分享基於Kubernetes來創建apt鏡像服務的方法。github

部署到Kubernetes中,配置文件以下:docker

apiVersion: v1
kind: ServiceAccount
metadata:
  name: apt-mirror
  namespace: apt-mirror
---
kind: Service
apiVersion: v1
metadata:
  name: apt-mirror
  namespace: apt-mirror
  labels:
    app: apt-mirror
spec:
  ports:
    - name: mirror-server
      port: 80
  type: LoadBalancer
  selector:
    app: apt-mirror
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: apt-mirror
  namespace: apt-mirror
spec:
  selector:
    matchLabels:
      app: apt-mirror
  replicas: 1
  strategy:
    type: Recreate 
  template:
    metadata:
      labels:
        app: apt-mirror
    spec:
      serviceAccount: apt-mirror
      containers:
        - name: apt-mirror
          image: seterrychen/apt-mirror-http-server
          ports:
            - name: mirror-server
              containerPort: 80
          securityContext:
            capabilities:
              add:
                - DAC_READ_SEARCH
                - SYS_RESOURCE
          env:
            - name: RESYNC_PERIOD
              value: 12h
          imagePullPolicy: "IfNotPresent"
          volumeMounts:
            - name: mirror-volume
              mountPath: /var/spool/apt-mirror
      volumes:
        - name: mirror-volume
          hostPath:
            path: /home/supermap/apt-mirror

咱們建立了一個Deployment和一個Service。存儲使用了hostpath,只是爲了方便驗證可行性。在生產系統中,volumes能夠使用NFS或其它的網絡分佈式存儲系統,從而能夠遷移、伸縮和容錯。api

將上面的文件保存爲mirror-server.sh,而後運行:瀏覽器

kubectl create ns apt-mirror
kubectl apply -f mirror-server.yaml

便可將該服務安裝到apt-mirror命名空間中。服務器

而後,在瀏覽器中訪問服務中列出的地址便可。能夠經過下面的命令獲取:網絡

kubectl get svc -n apt-mirror

若是鏡像下載有問題,能夠嘗試單獨下載,以下:app

docker pull seterrychen/apt-mirror-http-server
相關文章
相關標籤/搜索