對於企業級開發團隊,搭建軟件包的鏡像站點(以及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 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鏡像站點