日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自动替换 Kubernetes 镜像

發(fā)布時間:2024/8/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自动替换 Kubernetes 镜像 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

來源:云原生指北
作者:Addo Zhang

最近萌生了個想法,維護(hù)一個后網(wǎng)絡(luò)友好的倉庫鏡像,在 Pod 創(chuàng)建時將鏡像倉庫切換到自維護(hù)的倉庫,從自維護(hù)的倉庫拉取鏡像。

前幾天體驗了極狐Gitlab 的容器鏡像庫,便是為這個想法做的準(zhǔn)備。當(dāng)然其他的云廠商也有提供針對個人版的免費(fèi)鏡像倉庫和企業(yè)版?zhèn)}庫。

正好?Pipy 作為策略引擎,非常適合實現(xiàn)這種策略的執(zhí)行。

實現(xiàn)思路

Admission Webhook

Kubernetes 動態(tài)準(zhǔn)備控制的?MutatingWebhookConfiguration?可以 hook Pod 的創(chuàng)建或者更新,然后調(diào)用目標(biāo)服務(wù)對 Pod 資源對象進(jìn)行 patch 操作。

策略引擎

Pipy 作為應(yīng)用的核心,也就是?MutatingWebhookConfiguration?的目標(biāo)服務(wù),以策略引擎的角色完成策略的執(zhí)行。

Pipy 支持從文件或者 HTTP 地址加載腳本,這里為了便于策略的更新,使用了后者。

對于從 HTTP 地址加載腳本,HTTP 地址返回內(nèi)容的第一行會作為 Pipy 的主腳本,Pipy 啟動時會加載主腳本,其他的文件也會被緩存到內(nèi)存中。

#地址 http://localhost:6080/repo/registry-mirror/ $ curl http://localhost:6080/repo/registry-mirror/ /main.js /config.json

Pipy 會每隔 5s 檢查腳本和配置文件的?etag(就是文件的最后更新時間),假如與當(dāng)前文件的 etag 不一致,則會緩存并重新加載。

利用 Pipy 的這個特性,便可以策略和配置的準(zhǔn)實時更新。

策略

對于策略的部分,我們將其邏輯和配置進(jìn)行了分離。配置部分,配置了需要進(jìn)行替換的鏡像的前綴,以及替換成的內(nèi)容;而邏輯,這是對?MutatingWebhookConfiguration?的?AdmissionReview?的對象進(jìn)行檢查。

配置:

{ "registries":{ "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd":"registry.gitlab.cn/flomesh/registry-mirror/tekton-pipeline" } }

比如說,對于鏡像?gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/controller:v0.28.1,將?gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd?替換成?registry.gitlab.cn/addozhang/registry-mirror/tekton-pipeline。

Demo

本文使用所有的源碼都已上傳到了 github。

腳本服務(wù)器

既然選用了 HTTP 方式加載 Pipy 的腳本,那就需要實現(xiàn)一個腳本服務(wù)器。實現(xiàn)的方式有兩種:使用腳本實現(xiàn)腳本服務(wù)器和使用 Pipy 內(nèi)置的 Codebase。

使用腳本實現(xiàn)腳本服務(wù)器

根據(jù)需求定義兩種路由:

?/repo/registry-mirror/:返回腳本和配置的文件列表?/repo/registry-mirror/[File Name]:返回對應(yīng)的文件的內(nèi)容,同時需要在響應(yīng)頭添加?etag,值是文件的更新時間

具體腳本如下:

#repo.js pipy({_serveFile:(req, type, filename)=>(filename = req.head.path.substring(22),os.stat(filename)?( newMessage( {bodiless: req.head.method ==='HEAD',headers:{ 'etag': os.stat(filename)?.mtime |0, 'content-type': type, }, },req.head.method ==='HEAD'?null: os.readFile(filename), ) ):( newMessage({ status:404},`file ${filename} not found`) ) ),_router:new algo.URLRouter({ '/repo/registry-mirror/':()=>newMessage('/main.js\n/config.json'), '/repo/registry-mirror/*': req => _serveFile(req,'text/plain') }), }) .listen(6080) .serveHTTP(req => _router.find(req.head.path)(req) )%$ pipy repo.js 2021-10-0521:40:25[info][config] 2021-10-0521:40:25[info][config]Module/repo.js 2021-10-0521:40:25[info][config]=============== 2021-10-0521:40:25[info][config] 2021-10-0521:40:25[info][config][Listen on :::6080] 2021-10-0521:40:25[info][config]----->| 2021-10-0521:40:25[info][config]| 2021-10-0521:40:25[info][config] serveHTTP 2021-10-0521:40:25[info][config]| 2021-10-0521:40:25[info][config]<-----| 2021-10-0521:40:25[info][config] 2021-10-0521:40:25[info][listener]Listening on port 6080 at ::

檢查路由:

$ curl http://localhost:6080/repo/registry-mirror/ /main.js /config.json $ curl http://localhost:6080/repo/registry-mirror/main.js #省略 main.js 的內(nèi)容 $ curl http://localhost:6080/repo/registry-mirror/config.json #省略 config.json 的內(nèi)容

使用 Pipy 內(nèi)置的 Codebase

在最新發(fā)布的 Pipy 內(nèi)置了一個 Codebase,大家可以理解成腳本倉庫,但是比單純的倉庫功能更加強(qiáng)大(后面會有文檔介紹該特性)。

目前版本的 Codebase 還未支持持久化的存儲,數(shù)據(jù)都是保存在內(nèi)存中。后續(xù)會提供 KV store 或者 git 類型的持久化支持。

啟動 Pipy 的 Codebase很簡單:

$ pipy 2021-10-0521:49:08[info][codebase]Starting codebase service... 2021-10-0521:49:08[info][listener]Listening on port 6060 at ::

對于新的 Codebase 控制臺的使用,這里不做過多的介紹,直接使用 REST API 完成腳本的寫入:

#創(chuàng)建 registry-mirror codebase,會自動創(chuàng)建一個空的 main.js $ curl -X POST http://localhost:6060/api/v1/repo/registry-mirror #更新 main.js $ curl -X POST 'http://localhost:6060/api/v1/repo/registry-mirror/main.js'--data-binary '@scripts/main.js' #創(chuàng)建 config.json $ curl -X POST 'http://localhost:6060/api/v1/repo/registry-mirror/config.json'--data-binary '@scripts/config.json' #檢查 codebase 的版本 $ curl -s http://localhost:6060/api/v1/repo/registry-mirror | jq -r .version #更新版本 $ curl -X POST 'http://localhost:6060/api/v1/repo/registry-mirror'--data-raw '{"version":2}'

安裝

進(jìn)入到項目的根目錄中,執(zhí)行:

$ helm install registry-mirror ./registry-mirror -n default NAME: registry-mirror LAST DEPLOYED:TueOct522:19:262021 NAMESPACE:default STATUS: deployed REVISION:1 TEST SUITE:None

查看 webhook:

$ kubectl get mutatingwebhookconfigurations NAME WEBHOOKS AGE registry-mirror-webhook 12m6s

檢查 pod 的啟動日志:

$ kubectl logs -n pipy -l app=pipy 2021-10-0514:19:28[info][codebase] GET http://192.168.1.101:6060/repo/registry-mirror/ -> 21 bytes 2021-10-0514:19:28[info][codebase] GET /repo/registry-mirror/main.js ->2213 bytes 2021-10-0514:19:28[info][codebase] GET /repo/registry-mirror/config.json ->149 bytes 2021-10-0514:19:28[info][config] 2021-10-0514:19:28[info][config]Module/main.js 2021-10-0514:19:28[info][config]=============== 2021-10-0514:19:28[info][config] 2021-10-0514:19:28[info][config][Listen on :::6443] 2021-10-0514:19:28[info][config]----->| 2021-10-0514:19:28[info][config]| 2021-10-0514:19:28[info][config] acceptTLS 2021-10-0514:19:28[info][config]| 2021-10-0514:19:28[info][config]|-->[tls-offloaded] 2021-10-0514:19:28[info][config] decodeHTTPRequest 2021-10-0514:19:28[info][config] replaceMessage 2021-10-0514:19:28[info][config] encodeHTTPResponse -->| 2021-10-0514:19:28[info][config]| 2021-10-0514:19:28[info][config]<---------------------------------| 2021-10-0514:19:28[info][config] 2021-10-0514:19:28[info][listener]Listening on port 6443 at ::

測試

$ kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/previous/v0.28.1/release.yaml $ kubectl get pod -n tekton-pipelines NAME READY STATUS RESTARTS AGE tekton-pipelines-controller-75974fbfb8-f62dv 1/1Running07m36s tekton-pipelines-webhook-6cc478f7ff-mm5l9 1/1Running07m36s

檢查結(jié)果:

$ kubectl get pod -o json -n tekton-pipelines -l app=tekton-pipelines-controller | jq -r '.items[].spec.containers[].image' registry.gitlab.cn/flomesh/registry-mirror/tekton-pipeline/controller:v0.28.1 $ kubectl get pod -o json -n tekton-pipelines -l app=tekton-pipelines-webhook | jq -r '.items[].spec.containers[].image' registry.gitlab.cn/flomesh/registry-mirror/tekton-pipeline/webhook:v0.28.1

從上面的結(jié)果可以看到結(jié)果是符合預(yù)期的。

總結(jié)

整個實現(xiàn)的策略部分加上配置,只有 70 多行的代碼。并且實現(xiàn)了邏輯與配置的分離之后,后續(xù)的配置也都可以做到實時的更新而無需修改任何邏輯代碼,更無需重新部署。

但是目前的實現(xiàn),是需要手動把鏡像推送的自維護(hù)的鏡像倉庫中。實際上理想的情況是檢查自維護(hù)的倉庫中是否存在鏡像(比如通過 REST API),如果未發(fā)現(xiàn)鏡像,先把鏡像拉取到本地,tag?后再推送到自維護(hù)的倉庫。不過這種操作,還是需要網(wǎng)絡(luò)的暢通。當(dāng)然也嘗試過通過 REST API 觸發(fā) CICD Pipeline 的執(zhí)行拉取鏡像并 tag,但是極狐Gitlab 是部署在某云的環(huán)境上,同樣也受困于網(wǎng)絡(luò)問題。

引用鏈接

Kubernetes 動態(tài)準(zhǔn)備控制:?https://kubernetes.io/zh/docs/reference/access-authn-authz/extensible-admission-controllers/
源碼:?https://github.com/addozhang/registry-mirror

往期推薦

移動云亮相2021 IDC年度盛典

數(shù)學(xué)在左,人生在右

Redis很厲害,使用規(guī)范來啦

低代碼會讓程序員更加內(nèi)卷嗎?

點(diǎn)分享

點(diǎn)收藏

點(diǎn)點(diǎn)贊

點(diǎn)在看

總結(jié)

以上是生活随笔為你收集整理的自动替换 Kubernetes 镜像的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。