Tsib nco thaum xa thawj daim ntawv thov ntawm Kubernetes

Tsib nco thaum xa thawj daim ntawv thov ntawm KubernetesFail los ntawm Aris Dreamer

Ntau tus neeg xav tias nws txaus los hloov daim ntawv thov mus rau Kubernetes (txawm yog siv Helm lossis manually) - thiab yuav muaj kev zoo siab. Tab sis tsis yog txhua yam yooj yim heev.

pab neeg Mail.ru Huab Solutions txhais ib tsab xov xwm los ntawm DevOps engineer Julian Gindy. Nws qhia txog qhov teeb meem uas nws lub tuam txhab tau ntsib thaum lub sijhawm tsiv teb tsaws chaw kom koj tsis txhob nqis los ntawm tib lub rake.

Kauj Ruam Ib: Teem Pod Thov thiab txwv

Cia peb pib los ntawm kev teeb tsa ib puag ncig huv uas peb cov pods yuav khiav. Kubernetes yog qhov zoo ntawm kev teem caij pods thiab ua tsis tiav. Tab sis nws tau muab tawm tias tus teem sijhawm qee zaum tsis tuaj yeem tso lub plhaub taum yog tias nws nyuaj rau kwv yees tias nws xav tau ntau npaum li cas los ua haujlwm tau zoo. Qhov no yog qhov kev thov rau cov peev txheej thiab cov kev txwv yuav tshwm sim. Muaj ntau qhov kev sib cav txog txoj hauv kev zoo tshaj plaws rau kev teeb tsa kev thov thiab kev txwv. Qee lub sij hawm nws zoo nkaus li tias qhov no yog ib qho txuj ci ntau dua li kev tshawb fawb. Nov yog peb txoj hauv kev.

Pod thov yog tus nqi tseem ceeb uas siv los ntawm tus teem sijhawm kom pom zoo tso lub pod.

Ntawm qhov Kubernetes cov ntaub ntawv: Cov kauj ruam lim txhais cov txheej txheem uas lub Pod tuaj yeem teem sijhawm. Piv txwv li, PodFitsResources lim tshuaj xyuas kom pom tias lub node muaj peev txheej txaus los ua kom tau raws li cov kev thov tshwj xeeb los ntawm lub pod.

Peb siv daim ntawv thov kev thov raws li txoj hauv kev uas peb tuaj yeem kwv yees ntau npaum li cas cov peev txheej qhov tseeb Daim ntawv thov xav kom nws ua haujlwm zoo. Txoj kev no tus teem sijhawm tuaj yeem tso cov nodes tiag tiag. Thaum xub thawj, peb xav kom dhau sijhawm thov kom paub meej cov peev txheej txaus rau txhua lub Pod, tab sis peb pom tias lub sijhawm teem sijhawm tau nce ntxiv, thiab qee lub Pods tsis tau teem sijhawm, zoo li tsis muaj kev thov rau lawv.

Nyob rau hauv cov ntaub ntawv no, tus teem sij hawm yuav feem ntau "sab tawm" cov pods thiab tsis muaj peev xwm rov teem dua lawv vim hais tias lub dav hlau tswj tsis muaj lub tswv yim ntau npaum li cas cov kev pab cuam daim ntawv thov yuav xav tau, uas yog ib feem tseem ceeb ntawm lub sij hawm algorithm.

Pod txwv yog ib qho kev txwv kom meej meej rau lub plhaub. Nws sawv cev rau qhov siab tshaj ntawm cov peev txheej uas pawg yuav faib rau lub thawv.

Ib zaug ntxiv, los ntawm cov ntaub ntawv raug cai: Yog hais tias ib lub thawv muaj lub cim xeeb txwv ntawm 4 GiB, ces lub kubelet (thiab lub thawv runtime) yuav tswj nws. Lub sijhawm ua haujlwm tiv thaiv lub thawv los ntawm kev siv ntau tshaj li qhov kev txwv txwv. Piv txwv li, thaum cov txheej txheem hauv ib lub thawv sim siv ntau tshaj li qhov tso cai ntawm lub cim xeeb, cov kab ke hauv lub cev txiav tawm cov txheej txheem nrog "tsis nco qab" (OOM) yuam kev.

Lub thawv ib txwm siv tau ntau dua li qhov kev thov cov peev txheej tshwj xeeb, tab sis nws tsis tuaj yeem siv ntau tshaj qhov txwv. Tus nqi no nyuaj rau kev teeb tsa kom raug, tab sis nws tseem ceeb heev.

Qhov zoo tshaj plaws, peb xav kom cov kev xav tau ntawm lub plhaub taum pauv hloov thaum lub sijhawm lub neej ntawm cov txheej txheem tsis cuam tshuam nrog lwm cov txheej txheem hauv lub cev - qhov no yog lub hom phiaj ntawm kev teeb tsa kev txwv.

Hmoov tsis zoo, kuv tsis tuaj yeem muab cov lus qhia tshwj xeeb ntawm qhov tseem ceeb los teeb tsa, tab sis peb tus kheej ua raws li cov cai hauv qab no:

  1. Siv cov cuab yeej ntsuas kev thauj khoom, peb simulate theem pib ntawm kev khiav tsheb thiab saib xyuas kev siv cov khoom siv hauv pod (nco thiab processor).
  2. Teem lub pod thov rau tus nqi qis qis (nrog rau kev txwv ntawm li 5 npaug ntawm tus nqi thov) thiab saib. Thaum kev thov qis dhau ib theem, cov txheej txheem pib tsis tuaj yeem, feem ntau ua rau cryptic Go runtime yuam kev.

Kuv nco ntsoov tias kev txwv ntau dua ua rau kev teem caij nyuaj dua vim tias cov pod xav tau lub hom phiaj ntawm cov peev txheej txaus.

Xav txog qhov xwm txheej uas koj muaj lub vev xaib sib sib zog nqus nrog cov peev txheej siab heev, zoo li 4 GB ntawm lub cim xeeb. Cov txheej txheem no yuav zoo li yuav tsum tau scaled tawm kab rov tav, thiab txhua lub pod tshiab yuav tsum tau teem caij rau ntawm ib qho nrog tsawg kawg yog 4 GB ntawm lub cim xeeb muaj. Yog tias tsis muaj cov node tshwm sim, pawg yuav tsum qhia txog qhov tshiab los ua cov pod no, uas yuav siv sij hawm qee lub sijhawm. Nws yog ib qho tseem ceeb kom ua tiav qhov sib txawv yam tsawg kawg nkaus ntawm kev thov kev pab thiab kev txwv kom ntseeg tau tias kev ntsuas nrawm thiab du.

Kauj Ruam XNUMX: Teem Lub Neej Nyob thiab Kev Xeem Npaj

Nov yog lwm lub ntsiab lus me me uas feem ntau tau tham hauv lub zej zog Kubernetes. Nws yog ib qho tseem ceeb kom muaj kev nkag siab zoo ntawm Liveness thiab Readiness tests raws li lawv muab cov txheej txheem rau kev ua haujlwm ruaj khov ntawm software thiab txo qis qis. Txawm li cas los xij, lawv tuaj yeem cuam tshuam rau koj daim ntawv thov kev ua tau zoo yog tias tsis teeb tsa kom raug. Hauv qab no yog cov ntsiab lus ntawm ob qho piv txwv yog dab tsi.

Nyob Los Tau qhia tau hais tias lub thawv khiav. Yog tias nws ua tsis tiav, kubelet tua lub thawv thiab txoj cai rov pib dua yog qhib rau nws. Yog tias lub thawv tsis tau nruab nrog Liveness Probe, ces lub xeev lub neej ntawd yuav ua tiav - raws li tau hais hauv Kubernetes cov ntaub ntawv.

Kev sojntsuam Liveness yuav tsum pheej yig, piv txwv li tsis siv ntau cov peev txheej, vim tias lawv ua haujlwm nquag thiab yuav tsum qhia rau Kubernetes tias daim ntawv thov tab tom ua haujlwm.

Yog tias koj teeb tsa qhov kev xaiv los khiav txhua ob, qhov no yuav ntxiv 1 qhov kev thov ib ob, yog li yuav tsum paub tias yuav tsum muaj peev txheej ntxiv los ua cov tsheb khiav no.

Ntawm peb lub tuam txhab, Liveness tests sim cov khoom tseem ceeb ntawm daim ntawv thov, txawm tias cov ntaub ntawv (piv txwv li, los ntawm cov chaw taws teeb database lossis cache) tsis muaj tag nrho.

Peb tau teeb tsa "kev noj qab haus huv" qhov kawg hauv cov ntawv thov uas tsuas yog xa rov qab 200 lo lus teb. Qhov no yog qhov qhia tau tias cov txheej txheem tab tom ua haujlwm thiab muaj peev xwm tuav cov kev thov (tab sis tseem tsis tau muaj tsheb khiav).

Qauv Npaj txhij qhia seb lub thawv puas npaj txhij rau kev thov. Yog tias qhov kev ntsuam xyuas kev npaj ua tsis tiav, tus tswj qhov kawg tshem tawm cov pod's IP chaw nyob los ntawm qhov kawg ntawm txhua qhov kev pabcuam cuam tshuam nrog lub pod. Qhov no kuj tau hais nyob rau hauv cov ntaub ntawv Kubernetes.

Kev tshawb nrhiav kev npaj tau siv ntau cov peev txheej, vim lawv yuav tsum ntaus lub backend hauv txoj hauv kev los qhia tias daim ntawv thov npaj tau txais kev thov.

Muaj kev sib cav ntau hauv zej zog txog seb puas yuav nkag mus rau hauv cov ntaub ntawv ncaj qha. Xav txog cov nyiaj siv ua haujlwm (cov tshev nyiaj ntau zaus, tab sis lawv tuaj yeem tswj tau), peb tau txiav txim siab tias rau qee qhov kev siv, kev npaj ua haujlwm rau tsheb tsuas yog suav tom qab kuaj xyuas cov ntaub ntawv rov qab los ntawm cov ntaub ntawv. Cov kev sim npaj tau zoo tsim tau ua kom muaj qib siab dua thiab tshem tawm lub sijhawm poob thaum lub sijhawm xa mus.

Yog tias koj txiav txim siab nug cov ntaub ntawv los ntsuam xyuas qhov kev npaj ntawm koj daim ntawv thov, xyuas kom tseeb tias nws pheej yig li sai tau. Cia peb muab cov lus nug no:

SELECT small_item FROM table LIMIT 1

Nov yog ib qho piv txwv ntawm yuav ua li cas peb teeb tsa ob qhov tseem ceeb hauv Kubernetes:

livenessProbe: 
 httpGet:   
   path: /api/liveness    
   port: http 
readinessProbe:  
 httpGet:    
   path: /api/readiness    
   port: http  periodSeconds: 2

Koj tuaj yeem ntxiv qee qhov kev xaiv configuration ntxiv:

  • initialDelaySeconds - Ntau npaum li cas vib nas this yuav dhau ntawm qhov kev tso tawm ntawm lub thawv thiab qhov pib ntawm lub community launch ntawm probes.
  • periodSeconds - tos ncua ntawm cov qauv khiav.
  • timeoutSeconds - tus naj npawb ntawm cov vib nas this tom qab uas lub pod yog suav tias yog xwm ceev. Lub sij hawm qub.
  • failureThreshold yog tus naj npawb ntawm qhov kev xeem tsis ua haujlwm ua ntej lub teeb liab rov pib dua raug xa mus rau lub pod.
  • successThreshold yog tus naj npawb ntawm kev sim ua tiav ua ntej lub pod hloov mus rau lub xeev npaj tau (tom qab ua tsis tiav thaum lub pod pib los yog rov qab).

Kauj ruam XNUMX: Teem lub Pod's Default Network Policy

Kubernetes muaj "paj" network topography, los ntawm lub neej ntawd tag nrho cov pods sib txuas lus ncaj qha rau ib leeg. Qee zaum qhov no tsis xav tau.

Qhov teeb meem kev nyab xeeb muaj peev xwm yog tias tus neeg tawm tsam tuaj yeem siv ib daim ntawv thov tsis zoo los xa tsheb mus rau txhua lub pods ntawm lub network. Raws li nyob rau hauv ntau qhov chaw ntawm kev ruaj ntseg, lub hauv paus ntsiab lus ntawm tsawg kawg yog tsim nyog siv ntawm no. Qhov zoo tshaj plaws, cov cai hauv network yuav tsum qhia meej meej tias kev sib txuas ntawm cov pods tau tso cai thiab qhov twg tsis yog.

Piv txwv li, cov hauv qab no yog ib txoj cai yooj yim uas tsis lees paub tag nrho cov tsheb thauj mus los rau ib lub npe tshwj xeeb:

---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:  
 name: default-deny-ingress
spec:  
 podSelector: {}  
 policyTypes:  
   - Ingress

Visualization ntawm no configuration:

Tsib nco thaum xa thawj daim ntawv thov ntawm Kubernetes
(https://miro.medium.com/max/875/1*-eiVw43azgzYzyN1th7cZg.gif)
Hauv cov ntsiab lus no.

Kauj Ruam XNUMX: Kev Cai Cwj Pwm nrog Hooks thiab Init Containers

Ib qho ntawm peb lub hom phiaj tseem ceeb yog muab kev xa tawm hauv Kubernetes yam tsis muaj sijhawm poob rau cov neeg tsim khoom. Qhov no yog qhov nyuaj vim tias muaj ntau txoj hauv kev rau kev kaw cov ntawv thov thiab tso lawv cov peev txheej siv.

Cov teeb meem tshwj xeeb tshwm sim nrog Nginx. Peb pom tias thaum xa cov Pods no ua ntu zus, kev sib txuas nquag tau cuam tshuam ua ntej ua tiav.

Tom qab kev tshawb fawb dav dav hauv Is Taws Nem, nws tau pom tias Kubernetes tsis tos Nginx kev sib txuas kom tso lawv tus kheej ua ntej kaw lub pod. Nrog kev pab los ntawm kev sib txuas ua ntej nres, peb tau siv cov haujlwm hauv qab no thiab tshem tawm tag nrho lub sijhawm poob:

lifecycle: 
 preStop:
   exec:
     command: ["/usr/local/bin/nginx-killer.sh"]

Tab sis nginx-killer.sh:

#!/bin/bash
sleep 3
PID=$(cat /run/nginx.pid)
nginx -s quit
while [ -d /proc/$PID ]; do
   echo "Waiting while shutting down nginx..."
   sleep 10
done

Lwm qhov txiaj ntsig zoo tshaj plaws yog kev siv cov thawv ntim khoom siv los tswj kev tshaj tawm cov ntawv thov tshwj xeeb. Qhov no yog tshwj xeeb tshaj yog muaj txiaj ntsig yog tias koj muaj cov txheej txheem kev tsiv teb tsaws chaw siv cov ntaub ntawv uas yuav tsum tau khiav ua ntej daim ntawv thov pib. Koj tuaj yeem hais qhia qhov kev txwv ntau dua rau cov txheej txheem no yam tsis tau teeb tsa xws li kev txwv rau daim ntawv thov tseem ceeb.

Lwm cov tswv yim zoo tshaj plaws yog nkag mus rau cov lus zais hauv lub thawv ntim khoom, uas muab cov ntaub ntawv pov thawj no rau lub ntsiab module, uas tiv thaiv kev nkag mus rau tsis pub lwm tus paub los ntawm lub ntsiab daim ntawv thov module nws tus kheej.

Raws li ib txwm muaj, ib qho kev tsocai los ntawm cov ntaub ntawv: init ntim yam xyuam xim khiav cov neeg siv cov cai lossis cov khoom siv hluav taws xob uas yuav ua rau muaj kev ruaj ntseg ntawm daim ntawv thov lub thawv duab. Los ntawm khaws cov cuab yeej tsis tsim nyog cais, koj txwv qhov chaw nres ntawm daim ntawv thov lub thawv duab.

Kauj ruam XNUMX: Kernel Configuration

Thaum kawg, cia peb tham txog cov txheej txheem siab dua.

Kubernetes yog lub platform hloov tau yooj yim uas tso cai rau koj los khiav haujlwm txawm li cas los xij koj pom haum. Peb muaj ib tug xov tooj ntawm cov kev siv tau zoo heev uas muaj peev xwm siv tau ntau heev. Tom qab ua qhov kev sim thauj khoom hnyav, peb pom tias ib qho ntawm cov ntawv thov tau muaj lub sijhawm nyuaj ua raws li qhov xav tau ntawm kev thauj mus los thaum lub sijhawm Kubernetes teeb tsa tau ua haujlwm.

Txawm li cas los xij, Kubernetes tso cai rau koj los khiav lub thawv uas muaj cai uas tsuas yog hloov pauv cov ntsiab lus rau ib lub pod. Nov yog qhov peb tau siv los hloov qhov ntau tshaj plaws ntawm kev sib txuas qhib:

initContainers:
  - name: sysctl
     image: alpine:3.10
     securityContext:
         privileged: true
      command: ['sh', '-c', "sysctl -w net.core.somaxconn=32768"]

Qhov no yog cov txheej txheem ntau dua uas feem ntau tsis xav tau. Tab sis yog tias koj daim ntawv thov muaj teeb meem los daws qhov hnyav, koj tuaj yeem sim tweaking qee qhov ntawm cov chaw no. Xav paub ntau ntxiv txog cov txheej txheem no thiab teeb tsa cov nqi sib txawv - raws li ib txwm muaj nyob rau hauv cov ntaub ntawv official.

Nyob rau hauv xaus

Thaum Kubernetes yuav zoo li qhov kev daws teeb meem tawm ntawm lub thawv, muaj ob peb kauj ruam tseem ceeb uas yuav tsum tau ua kom cov ntawv thov ua haujlwm zoo.

Thoob plaws hauv kev tsiv teb tsaws mus rau Kubernetes, nws yog ib qho tseem ceeb uas yuav tsum ua raws li "load kuaj lub voj voog": khiav daim ntawv thov, sim nws hauv qab thauj khoom, soj ntsuam cov ntsuas ntsuas thiab ntsuas tus cwj pwm, kho qhov teeb tsa raws li cov ntaub ntawv no, tom qab ntawd rov ua lub voj voog no dua.

Ua qhov tseeb txog kev xav tau tsheb khiav thiab sim mus dhau nws kom pom cov khoom tawg ua ntej. Nrog rau qhov kev rov ua dua no, tsuas yog qee qhov ntawm cov lus pom zoo uas tau teev tseg tuaj yeem txaus kom ua tiav. Los yog xav tau kev hloov kho qhov tob ntxiv.

Nco ntsoov nug koj tus kheej cov lus nug no:

  1. Muaj pes tsawg daim ntawv thov siv thiab cov nyiaj no yuav hloov li cas?
  2. Dab tsi yog qhov kev ntsuas qhov ntsuas tiag tiag? Lub app yuav ua li cas rau nruab nrab? Yuav ua li cas txog kev tsheb khiav ceev?
  3. Ntev npaum li cas qhov kev pabcuam yuav tsum tau ntsuas tawm? Yuav ua li cas cov pods tshiab yuav tsum tau nce thiab khiav kom tau txais kev khiav tsheb?
  4. Yuav ua li cas gracefully ua pods kaw? Puas tsim nyog? Nws puas tuaj yeem ua tiav kev xa tawm yam tsis muaj sijhawm poob?
  5. Yuav ua li cas txo qhov kev pheej hmoo ntawm kev ruaj ntseg thiab txo kev puas tsuaj los ntawm ib qho kev cuam tshuam cov pods? Puas muaj cov kev pabcuam muaj kev tso cai lossis nkag mus uas lawv tsis xav tau?

Kubernetes muab lub platform zoo kawg uas tso cai rau koj siv cov kev coj ua zoo tshaj plaws los siv ntau txhiab cov kev pabcuam hauv pawg. Txawm li cas los xij, txhua daim ntawv thov sib txawv. Qee zaum kev siv yuav tsum ua haujlwm me ntsis ntxiv.

Hmoov zoo, Kubernetes muab cov chaw tsim nyog kom ua tiav txhua lub hom phiaj kev kawm. Los ntawm kev siv kev sib xyaw ua ke ntawm cov kev thov thiab kev txwv, Kev Tshawb Fawb Txog Kev Ua Neej thiab Kev Npaj Npaj, cov thawv ntim khoom, cov cai hauv lub network, thiab kev kho cov ntsiav tshuaj, koj tuaj yeem ua tiav kev ua haujlwm siab nrog rau kev ua txhaum cai thiab kev ua kom nrawm nrawm.

Yuav nyeem dab tsi ntxiv:

  1. Cov kev coj ua zoo tshaj plaws thiab cov kev coj ua zoo tshaj plaws rau kev khiav ntim thiab Kubernetes hauv kev tsim khoom ib puag ncig.
  2. 90+ Cov cuab yeej siv tau zoo rau Kubernetes: Kev xa tawm, kev tswj hwm, saib xyuas, kev ruaj ntseg thiab ntau dua.
  3. Peb channel ncig Kubernetes hauv Telegram.

Tau qhov twg los: www.hab.com

Ntxiv ib saib