k8s與CICD--利用flux部署應用到kubernetes

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

前言

一直在思考部署應用到k8s的姿勢,之前介紹過helm,個人感覺helm把記錄資料放到k8s中不是特別好。最近發現了weave的flux項目(A tool for deploying container images to Kubernetes services)。

flux簡介

先,大致看一下flux的理念。

1:flux可以和任意你喜歡的CI工具搭配比如:Drone,CircleCI,gitlabCI等。實際上flux做了CD的環節。

2:flux可以協助你安全地更新應用。一般會遇到更新過程出錯的問題,在flux實現中,每個部署的應用的yaml檔案儲存體在git倉庫裡,所以每次更改都可以追蹤,所以也能精確復原到任何一個曆史版本。這點我是覺得非常好的。當應用需要更新鏡像的時候,有以下幾個步驟:

  1. Clone the repository
  2. Find the deployment files that use the image in question (there may be more than one, since we have e.g., sidecar containers that use the same image)
  3. Update a few fields in each file, in slightly different ways (e.g., put the image name in the template, and the tag part of the image name in a version label)
  4. Commit and push the change back to Git

如果只是更新鏡像,其實有更簡單的操作:

$ fluxctl release --service=sock-shop/front-end --update-all-imagesSubmitting release job...Release job submitted, ID cc08f78c-0e66-24a9-9215-82c80cc646c3Status: Complete.Here's what happened:1. Submitted job.2. Calculating release actions.3. Release latest images to sock-shop/front-end4. Clone the config repo.5. Clone OK.6. Update 1 images(s) in the resource definition file for sock-shop/front-end: front-end (weaveworksdemos/front-end:0.2.0 -> weaveworksdemos/front-end:7f511af2d21fd601b86b3bed7baa6adfa9c8c669).7. Update pod controller OK.8. Commit and push the config repo.9. Pushed commit: Release latest images to sock-shop/front-end10. Regrade 1 service(s): sock-shop/front-end.Took 55.810622454s

此處會引出和drone結合的想法。這一點在總結裡會詳細闡述。

3: flux很好解決了可視化的問題,通過幾個指令就能清楚知道正在發生的。flux有controller的概念。

$ fluxctl list-services --namespace=sock-shopSERVICE                 CONTAINER   IMAGE                           RELEASE  POLICYsock-shop/cart          cart        weaveworksdemos/cart:0.3.0                   sock-shop/cart-db       cart-db     mongo                                        sock-shop/catalogue     catalogue   weaveworksdemos/catalogue:0.2.0              sock-shop/catalogue-db  catalogue-db  weaveworksdemos/catalogue-db:0.2.0         sock-shop/front-end     front-end   weaveworksdemos/front-end:0.2.0              sock-shop/orders        orders      weaveworksdemos/orders:0.3.0                 sock-shop/orders-db     orders-db   mongo                                        sock-shop/payment       payment     weaveworksdemos/payment:0.3.0                sock-shop/queue-master  queue-master  weaveworksdemos/queue-master:0.3.0         sock-shop/rabbitmq      rabbitmq    rabbitmq:3                                   sock-shop/shipping      shipping    weaveworksdemos/shipping:0.3.0               sock-shop/user          user        weaveworksdemos/user:0.3.0                   sock-shop/user-db       user-db     weaveworksdemos/user-db:0.3.0
$ fluxctl list-images --service=sock-shop/front-endSERVICE             CONTAINER  IMAGE                                  CREATEDsock-shop/front-end  front-end  weaveworksdemos/front-end                                         |   7f511af2d21fd601b86b3bed7baa6adfa9c8c669  18 Nov 16 16:00 UTC                     |   latest                                    18 Nov 16 16:00 UTC                     |   snapshot                                  18 Nov 16 16:00 UTC                     |   16007dddd86b2ebb41d8880c53b77580cab13f02  18 Nov 16 11:23 UTC                     |   a0eaf8c8ffc0b81c2199adaf7d67466891bb3205  17 Nov 16 10:22 UTC                     |   1b664cc81d9cf25b213000686a9e8d6477ae1b06  16 Nov 16 20:31 UTC                     |   ce1efe59c32d3c53344defdda4d5b643272022b8  16 Nov 16 20:29 UTC                     |   4c33b216ae902ad89b9c3ca30a99a44833d16d5b  10 Nov 16 21:34 UTC                     |   7015587783bc8f7a9c3d5cfa51e5a257fe188c4b  10 Nov 16 11:34 UTC                     |   4d3ea2896c81e40a630d36cdc29ce740e7617016  10 Nov 16 11:30 UTC                     :                                                                '-> 0.2.0                                     09 Nov 16 10:26 UTC

4:flux實際上可以做到完全自動檢測git倉庫中yaml檔案的變化以及registry中鏡像的更新,進而自動部署。
It is most useful when used as a deployment tool at the end of a Continuous Delivery pipeline. Flux will make sure that your new container images and config changes are propagated to the cluster。
大意就是,利用flux作為cd pipeline中最後一個環節,可以很好地解決鏡像更新或是yaml檔案更新。

總結

1:如何與drone結合?
首先我是認可將yaml儲存在git倉庫中,做版本控制。
實際上flux類似於helm,分為fluxd和一個命令列工具fluxctl,fluxd相當於server部署在k8s叢集中。那麼將fluxctl做成一個drone的外掛程式。放在drone pipeline中最後一步。類似於現在很多helm外掛程式的玩法。
這個外掛程式只需要完成更新鏡像的功能即可。畢竟整個pipeline中,更新代碼到倉庫中觸發編譯,構建,推送新鏡像到harbor,最終對於k8s叢集,只需要應用替換新的鏡像即可。所以不需要去監聽registry這個功能。也不需要關注git中yaml的變化,拉取最新的即可。
具體實現思路:

  • 將新的鏡像name和要更新的目標,以及yaml檔案路徑等作為drone外掛程式的輸入參數
  • 然後執行下面的類似命令
$ fluxctl release --controller=default:deployment/helloworld --update-image=quay.io/weaveworks/helloworld:master-a000001Submitting release ...Commit pushed: 33ce4e3Applied 33ce4e38048f4b787c583e64505485a13c8a7836CONTROLLER                     STATUS   UPDATESdefault:deployment/helloworld  success  helloworld: quay.io/weaveworks/helloworld:master-9a16ff945b9e -> master-a000001$ fluxctl list-images --controller default:deployment/helloworldCONTROLLER                     CONTAINER   IMAGE                          CREATEDdefault:deployment/helloworld  helloworld  quay.io/weaveworks/helloworld                                           |   master-9a16ff945b9e        20 Jul 16 13:19 UTC                                           |   master-b31c617a0fe3        20 Jul 16 13:19 UTC                                           |   master-a000002             12 Jul 16 17:17 UTC                                           '-> master-a000001             12 Jul 16 17:16 UTC                               sidecar     quay.io/weaveworks/sidecar                                           '-> master-a000002             23 Aug 16 10:05 UTC                                               master-a000001             23 Aug 16 09:53 UTC

2:拋開drone,對於整個paas平台。flux提供的自動監控registry和yaml git 倉庫的功能,也能豐富使用者的各種玩法。

3:接下來就是準備寫drone-flux外掛程式。逐步實現本文的想法。由於是利用業餘時間,可能該外掛程式不會儘快完成。也希望感興趣的,一起參與完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.