Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Re tla sebelisa Gitlab CI le GitOps ea tataiso ho kenya ts'ebetsong le ho sebelisa Canary deployment ho Kubernetes

Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Lingoliloeng ho tsoa letotong lena:

Re tla tsamaisa Canary ka letsoho ka GitOps le ho theha / ho fetola lisebelisoa tsa mantlha tsa Kubernetes. Sengoliloeng sena se reretsoe ho qala ka hore na ho tsamaisoa ho sebetsa joang Kubernetes Canary, kaha ho na le mekhoa e sebetsang haholoanyane ea ho iketsetsa, eo re tla e tšohla lihloohong tse latelang.


Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

https://www.norberteder.com/canary-deployment/

Ho tsamaisoa ha Canary

Ka leano la Canary, lintlafatso li qala ho sebelisoa ho sehlopha sa basebelisi feela. Ka ho beha leihlo, data ea log, tlhahlobo ea matsoho, kapa liteishene tse ling tsa maikutlo, tokollo e lekoa pele e lokolloa ho basebelisi bohle.

Tšebeliso ea Kubernetes (ntlafatso e ntseng e tsoela pele)

Leano la kamehla la Kubernetes Deployment ke ho ntlafatsa, moo palo e itseng ea li-pods e qalisoang ka mefuta e mecha ea litšoantšo. Haeba li bōpiloe ntle le mathata, li-pods tse nang le mefuta ea khale ea litšoantšo li felisoa, 'me li-pods tse ncha li bōptjoa ka tsela e tšoanang.

GitOps

Re sebelisa GitOps mohlaleng ona hobane re:

  • sebelisa Git e le mohloli o le mong oa 'nete
  • re sebelisa Git Operations bakeng sa ho aha le ho tsamaisa (ha ho na litaelo tse ling ntle le git tag/merge tse hlokahalang)

Mohlala:

Ha re itloaetse hantle - ho ba le polokelo e le 'ngoe ea khoutu ea kopo le e' ngoe bakeng sa meaho.

Sebaka sa polokelo

Ena ke Python + Flask API e bonolo haholo e khutlisetsang karabo joalo ka JSON. Re tla haha ​​​​sephutheloana ka GitlabCI ebe re sutumelletsa sephetho ho Gitlab Registry. Ho registry re na le mefuta e 'meli e fapaneng ea tokollo:

  • wuestkamp/k8s-deployment-example-app:v1
  • wuestkamp/k8s-deployment-example-app:v2

Phapang e le 'ngoe feela lipakeng tsa bona ke phetoho ea faele ea JSON e khutlisitsoeng. Re sebelisa sesebelisoa sena ho bona habonolo kamoo ho ka khonehang hore na re buisana le mofuta ofe.

Sebaka sa polokelo ea meaho

Ho turnip ena re tla tsamaisa ka GitlabCI ho ea Kubernetes, .gitlab-ci.yml ke ka tsela e latelang:

image: traherom/kustomize-docker

before_script:
   - printenv
   - kubectl version

stages:
 - deploy

deploy test:
   stage: deploy
   before_script:
     - echo $KUBECONFIG
   script:
     - kubectl get all
     - kubectl apply -f i/k8s

   only:
     - master

Ho e tsamaisa o tla hloka sehlopha, o ka sebelisa Gcloud:

gcloud container clusters create canary --num-nodes 3 --zone europe-west3-b

gcloud compute firewall-rules create incoming-80 --allow tcp:80

U hloka ho fereko https://gitlab.com/wuestkamp/k8s-deployment-example-canary-infrastructure le ho theha phetoho KUBECONFIG ho GitlabCI, e tla ba le config bakeng sa phihlello kubectl sehlopheng sa hao.

U ka bala ka mokhoa oa ho fumana mangolo a tumello bakeng sa sehlopha (Gcloud) hona mona.

Infrastructure Yaml

Sebakeng sa polokelo ea litšebeletso re na le litšebeletso:

apiVersion: v1
kind: Service
metadata:
 labels:
   id: app
 name: app
spec:
 ports:
 - port: 80
   protocol: TCP
   targetPort: 5000
 selector:
   id: app
 type: LoadBalancer

Le deployment in deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: app
spec:
 replicas: 10
 selector:
   matchLabels:
     id: app
     type: main
 template:
   metadata:
     labels:
       id: app
       type: main
   spec:
     containers:
     - image: registry.gitlab.com/wuestkamp/k8s-deployment-example-app:v1
       name: app
       resources:
         limits:
           cpu: 100m
           memory: 100Mi

Le phetisetso e 'ngoe deploy-canary.yaml:

kind: Deployment
metadata:
 name: app-canary
spec:
 replicas: 0
 selector:
   matchLabels:
     id: app
     type: canary
 template:
   metadata:
     labels:
       id: app
       type: canary
   spec:
     containers:
     - image: registry.gitlab.com/wuestkamp/k8s-deployment-example-app:v2
       name: app
       resources:
         limits:
           cpu: 100m
           memory: 100Mi

Hlokomela hore app-deploy ha e na likopi tse hlalositsoeng hajoale.

Ho etsa tlhahiso ea pele

Ho qala thomello ea pele, o ka qala lipeipi tsa GitlabCI ka letsoho lekaleng le ka sehloohong. Ka mor'a moo kubectl e lokela ho hlahisa tse latelang:

Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Rea bona app deployment with 10 replicas and app-canary with 0. Ho boetse ho na le LoadBalancer eo re ka fihlelang ho eona curl ka IP ea kantle:

while true; do curl -s 35.198.149.232 | grep label; sleep 0.1; done

Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Rea bona hore kopo ea rona ea teko e khutlisetsa feela "v1".

Ho etsa phepelo ea Canary

Mohato oa 1: lokolla mofuta o mocha bakeng sa basebelisi ba bang

Re beha palo ea likopi ho 1 faeleng ea deploy-canary.yaml le setšoantšo sa mofuta o mocha:

kind: Deployment
metadata:
 name: app-canary
spec:
 replicas: 1
 selector:
   matchLabels:
     id: app
     type: canary
 template:
   metadata:
     labels:
       id: app
       type: canary
   spec:
     containers:
     - image: registry.gitlab.com/wuestkamp/k8s-deployment-example-app:v2
       name: app
       resources:
         limits:
           cpu: 100m
           memory: 100Mi

Ka faele deploy.yaml re fetotse palo ea likopi ho 9:

kind: Deployment
metadata:
 name: app
spec:
 replicas: 9
 selector:
   matchLabels:
     id: app
...

Re sutumelletsa liphetoho tsena sebakeng sa polokelo eo thomello e tla qala ho eona (ka GitlabCI) 'me u bone ka lebaka leo:

Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Ts'ebeletso ea rona e tla supa lits'ebetso ka bobeli, kaha ka bobeli li na le khetho ea lisebelisoa. Ka lebaka la ts'ebetso ea kamehla ea Kubernetes, re lokela ho bona likarabo tse fapaneng bakeng sa ~ 10% ea likopo:

Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Boemo ba hajoale ba ts'ebeliso ea rona (GitOps, e nkiloeng ho Git joalo ka Mohloli o le Mong oa 'Nete) ke boteng ba li-deployments tse peli tse nang le likopi tse sebetsang, e le ngoe bakeng sa mofuta o mong le o mong.

~ 10% ea basebelisi ba tloaelane le mofuta o mocha ebe ba o leka ba sa rera. Joale ke nako ea ho hlahloba liphoso ho li-log le data ea ho beha leihlo ho fumana mathata.

Mohato oa 2: Lokolla mofuta o mocha ho basebelisi bohle

Re nkile qeto ea hore ntho e 'ngoe le e' ngoe e tsamaile hantle 'me joale re hloka ho hlahisa mofuta o mocha ho basebelisi bohle. Ho etsa sena re mpa re ntlafatsa deploy.yaml ho kenya mofuta o mocha oa setšoantšo le palo ea likopi tse lekanang le 10. In deploy-canary.yaml re beha palo ea likopi ho khutlela ho 0. Kamora ho romelloa, sephetho se tla ba ka tsela e latelang:

Ho tsamaisoa ha Canary ho Kubernetes #1: Gitlab CI

Ho akaretsa

Ho 'na, ho tsamaisa thepa ka letsoho ka tsela ena ho thusa ho utloisisa hore na e ka hlophisoa habonolo hakae ho sebelisa k8s. Kaha Kubernetes e u lumella ho ntlafatsa ntho e 'ngoe le e' ngoe ka API, mehato ena e ka etsoa ka mokhoa o ikemetseng ka mangolo.

Ntho e 'ngoe e lokelang ho kenngoa ts'ebetsong ke sebaka sa ho kena sa tester (LoadBalancer kapa ka Ingress) eo ka eona ho ka fumanoang phetolelo e ncha feela. E ka sebelisoa bakeng sa ho bala ka letsoho.

Lingoliloeng tse tlang, re tla sheba litharollo tse ling tse ikemetseng tse sebelisang boholo ba seo re se entseng.

Hape bala lingoliloeng tse ling ho blog ea rona:

Source: www.habr.com

Eketsa ka tlhaloso