Te tango i tetahi peka ahua tawhito i roto i te roopu Kubernetes

Te tango i tetahi peka ahua tawhito i roto i te roopu Kubernetes

Kia pai! Peka ahua (aka horahia te arokite, taupānga arotake) - koinei te wa e tukuna ana te peka matua, engari me tono toia ki tetahi URL ahurei. Ka taea e koe te tirotiro mena kei te mahi te waehere i roto i te taiao whakangao; ka taea te whakaatu te ahuatanga ki etahi atu hötaka, tohunga hua ranei. I a koe e mahi ana i roto i te tono kume, ka mukua ia tukunga hou o naianei mo te waehere tawhito, ka hurihia te tukunga hou mo te waehere hou. Ka puta ake nga patai ina hanumi koe i tetahi tono kume ki te peka matua. Kare e hiahia ana koe ki te peka ahua, engari kei roto tonu nga rauemi Kubernetes i te roopu.

He korero ano mo nga manga waahanga

Ko tetahi huarahi ki te hanga i nga manga ahua ki Kubernetes ko te whakamahi mokowāingoa. Hei poto, penei te ahua o te whirihoranga whakaputa:

kind: Namespace
apiVersion: v1
metadata:
  name: habr-back-end
...

kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: habr-back-end
spec:
  replicas: 3
...

Mo tetahi peka ahua, ka hangaia he mokowāingoa me tana tautohu (hei tauira, te tau tono toia) me etahi momo prefix/postfix (hei tauira, -pr-):

kind: Namespace
apiVersion: v1
metadata:
  name: habr-back-end-pr-17
...

kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: habr-back-end-pr-17
spec:
  replicas: 1
...

I te nuinga, i tuhi ahau Kaiwhakahaere Kubernetes (he tono e uru ana ki nga rauemi tautau), hono ki te kaupapa i runga i Github. Ka tangohia e ia nga mokowāingoa nō ngā manga āhuatanga tawhito. I Kubernetes, ki te mukua e koe he mokowāingoa, ka mukua aunoatia etahi atu rauemi kei taua mokowāingoa.

$ kubectl get pods --all-namespaces | grep -e "-pr-"
NAMESPACE            ... AGE
habr-back-end-pr-264 ... 4d8h
habr-back-end-pr-265 ... 5d7h

Ka taea e koe te panui me pehea te whakauru i nga manga ahua ki roto i te tautau konei и konei.

Te whakatenatena

Ka titiro tatou ki tetahi huringa ora tono kume me te whakauru tonu (continuous integration):

  1. Ka panaia e matou he here hou ki te peka.
  2. I runga i te hanga, ka whakahaerehia nga linters me/ranei nga whakamatautau.
  3. Ko nga whirihoranga tono kumea a Kubernetes ka mahia i runga i te rere (hei tauira, ka whakauruhia tona nama ki te tauira kua oti).
  4. Ma te whakamahi i te kubectl, ka taapirihia nga whirihoranga ki te tautau (whakaahua).
  5. Ka hanumi te tono kume ki te peka matua.

I a koe e mahi ana i roto i te tono kume, ka mukua ia tukunga hou o naianei mo te waehere tawhito, ka hurihia te tukunga hou mo te waehere hou. Engari ka hanumi he tono toia ki te peka matua, ko te peka matua anake ka hanga. Ko te mutunga mai, kua wareware tatou ki te tono toia, kei roto tonu ana rauemi Kubernetes i roto i te roopu.

Me pehea te whakamahi

Tāutahia te kaupapa me te whakahau i raro nei:

$ kubectl apply -f https://raw.githubusercontent.com/dmytrostriletskyi/stale-feature-branch-operator/master/configs/production.yml

Waihangahia he konae me nga mea e whai ake nei ka whakauru ma kubectl apply -f:

apiVersion: feature-branch.dmytrostriletskyi.com/v1
kind: StaleFeatureBranch
metadata:
  name: stale-feature-branch
spec:
  namespaceSubstring: -pr-
  afterDaysWithoutDeploy: 3

Taumahi mokowāingoaSubstring me tātari mokowāingoa mō ngā tono kumea mai i ētahi atu mokowāingoa. Hei tauira, mena kei te tautau nga mokowāingoa e whai ake nei: habr-back-end, habr-front-end, habr-back-end-pr-17, habr-back-end-pr-33, katahi ko nga kaitono mo te whakakore habr-back-end-pr-17, habr-back-end-pr-33.

Taumahi afterDaysWithoutDeploy e hiahiatia ana ki te muku i nga mokowā ingoa tawhito. Hei tauira, mena ka hangaia te mokowā ingoa 3 дня 1 час ki muri, ka tohu te tawhā 3 дня, ka mukua tenei mokowā ingoa. Ka mahi ano i te huarahi rereke mena ka hangaia te mokowā ingoa 2 дня 23 часа ki muri, ka tohu te tawhā 3 дня, e kore tenei mokowā ingoa e mukua.

Kotahi ano te tawhā, kei a ia te kawenga mo te maha o te tirotiro i nga mokowā ingoa katoa me te tirotiro mo nga ra kaore i tukuna - takiEveryMinutes. Ma te taunoa he rite 30 минутам.

Nahea teie ohipa

I roto i te mahi, ka hiahia koe:

  1. Docker mo te mahi i roto i te taiao taratahi.
  2. Minikube ka whakaarahia he kahui Kubernetes i te rohe.
  3. kubectl — atanga raina whakahau mo te whakahaere tautau.

Ka whakaarahia e matou he roopu Kubernetes i te rohe:

$ minikube start --vm-driver=docker
minikube v1.11.0 on Darwin 10.15.5
Using the docker driver based on existing profile.
Starting control plane node minikube in cluster minikube.

Ka tohu matou kubectl whakamahia te tautau paetata ma te taunoa:

$ kubectl config use-context minikube
Switched to context "minikube".

Tikiake whirihoranga mo te taiao whakaputa:

$ curl https://raw.githubusercontent.com/dmytrostriletskyi/stale-feature-branch-operator/master/configs/production.yml > stale-feature-branch-production-configs.yml

I te mea kua whirihorahia nga whirihoranga whakangao ki te tirotiro i nga mokowā ingoa tawhito, a, karekau i a maatau kahui hou, ka whakakapihia e matou te taurangi taiao. IS_DEBUG i runga i true. Ki tenei uara te tawhā afterDaysWithoutDeploy karekau e arohia, karekau hoki nga mokowā ingoa e tirohia mo nga ra karekau e horahia, mo te puta mai o te aho iti (-pr-).

Mena kei runga koe Linux:

$ sed -i 's|false|true|g' stale-feature-branch-production-configs.yml

Mena kei runga koe macOS:

$ sed -i "" 's|false|true|g' stale-feature-branch-production-configs.yml

Tāuta ana i te kaupapa:

$ kubectl apply -f stale-feature-branch-production-configs.yml

Te tirotiro kua puta mai he rauemi ki te tautau StaleFeatureBranch:

$ kubectl api-resources | grep stalefeaturebranches
NAME                 ... APIGROUP                             ... KIND
stalefeaturebranches ... feature-branch.dmytrostriletskyi.com ... StaleFeatureBranch

Ka tirohia e matou kua puta mai he kaiwhakahaere i te tautau:

$ kubectl get pods --namespace stale-feature-branch-operator
NAME                                           ... STATUS  ... AGE
stale-feature-branch-operator-6bfbfd4df8-m7sch ... Running ... 38s

Ki te titiro koe ki ona poro, kua reri ki te tukatuka rauemi StaleFeatureBranch:

$ kubectl logs stale-feature-branch-operator-6bfbfd4df8-m7sch -n stale-feature-branch-operator
... "msg":"Operator Version: 0.0.1"}
...
... "msg":"Starting EventSource", ... , "source":"kind source: /, Kind="}
... "msg":"Starting Controller", ...}
... "msg":"Starting workers", ..., "worker count":1}

Ka whakauruhia e matou kua rite fixtures (nga whirihoranga kua rite mo te whakatauira i nga rauemi tautau) mo tetahi rauemi StaleFeatureBranch:

$ kubectl apply -f https://raw.githubusercontent.com/dmytrostriletskyi/stale-feature-branch-operator/master/fixtures/stale-feature-branch.yml

Ko nga whirihoranga e tohu ana ki te rapu mokowā ingoa me te aho iti -pr- kotahi i roto i 1 минуту.:

apiVersion: feature-branch.dmytrostriletskyi.com/v1
kind: StaleFeatureBranch
metadata:
  name: stale-feature-branch
spec:
  namespaceSubstring: -pr-
  afterDaysWithoutDeploy: 1 
  checkEveryMinutes: 1

Kua whakautu te kaiwhakahaere, kua reri ki te tirotiro mokowāingoa:

$ kubectl logs stale-feature-branch-operator-6bfbfd4df8-m7sch -n stale-feature-branch-operator
... "msg":"Stale feature branch is being processing.","namespaceSubstring":"-pr-","afterDaysWithoutDeploy":1,"checkEveryMinutes":1,"isDebug":"true"}

Whakauruhia fixtures, e rua nga waahi ingoa (project-pr-1, project-pr-2) me ratou deployments, services, ingress, me era atu:

$ kubectl apply -f https://raw.githubusercontent.com/dmytrostriletskyi/stale-feature-branch-operator/master/fixtures/first-feature-branch.yml -f https://raw.githubusercontent.com/dmytrostriletskyi/stale-feature-branch-operator/master/fixtures/second-feature-branch.yml
...
namespace/project-pr-1 created
deployment.apps/project-pr-1 created
service/project-pr-1 created
horizontalpodautoscaler.autoscaling/project-pr-1 created
secret/project-pr-1 created
configmap/project-pr-1 created
ingress.extensions/project-pr-1 created
namespace/project-pr-2 created
deployment.apps/project-pr-2 created
service/project-pr-2 created
horizontalpodautoscaler.autoscaling/project-pr-2 created
secret/project-pr-2 created
configmap/project-pr-2 created
ingress.extensions/project-pr-2 created

Ka tirohia e matou ko nga rauemi katoa i runga ake nei kua tutuki pai:

$ kubectl get namespace,pods,deployment,service,horizontalpodautoscaler,configmap,ingress -n project-pr-1 && kubectl get namespace,pods,deployment,service,horizontalpodautoscaler,configmap,ingress -n project-pr-2
...
NAME                              ... READY ... STATUS  ... AGE
pod/project-pr-1-848d5fdff6-rpmzw ... 1/1   ... Running ... 67s

NAME                         ... READY ... AVAILABLE ... AGE
deployment.apps/project-pr-1 ... 1/1   ... 1         ... 67s
...

Mai i whakauruhia e matou debug, mokowā ingoa project-pr-1 и project-pr-2, no reira me mukua nga rauemi katoa me te kore e whai whakaaro ki te tawhā afterDaysWithoutDeploy. Ka kitea tenei i roto i nga raarangi kaiwhakahaere:

$ kubectl logs stale-feature-branch-operator-6bfbfd4df8-m7sch -n stale-feature-branch-operator
... "msg":"Namespace should be deleted due to debug mode is enabled.","namespaceName":"project-pr-1"}
... "msg":"Namespace is being processing.","namespaceName":"project-pr-1","namespaceCreationTimestamp":"2020-06-16 18:43:58 +0300 EEST"}
... "msg":"Namespace has been deleted.","namespaceName":"project-pr-1"}
... "msg":"Namespace should be deleted due to debug mode is enabled.","namespaceName":"project-pr-2"}
... "msg":"Namespace is being processing.","namespaceName":"project-pr-2","namespaceCreationTimestamp":"2020-06-16 18:43:58 +0300 EEST"}
... "msg":"Namespace has been deleted.","namespaceName":"project-pr-2"}

Mena ka tirohia e koe te waatea o nga rauemi, kei roto i te mana Terminating (te tukanga muku) kua mukua ranei (he putua te putanga whakahau).

$ kubectl get namespace,pods,deployment,service,horizontalpodautoscaler,configmap,ingress -n project-pr-1 && kubectl get namespace,pods,deployment,service,horizontalpodautoscaler,configmap,ingress -n project-pr-2
...

Ka taea e koe te whakahou i te tukanga hanga fixtures he maha nga wa me te whakarite kia tangohia i roto i te meneti.

Ko etahi atu

He aha te mahi hei utu mo te kaiwhakahaere e mahi ana i roto i te tautau? He maha nga huarahi, he koretake katoa (me o raatau ngoikoretanga he kaupapa), ka whakatauhia e te katoa ko te mea pai mo tetahi kaupapa:

  1. Mukua te peka ahua i te wa e whakauru tonu ana te manga matua.

    • Ki te mahi i tenei, me mohio koe ko tehea tono toia e pa ana ki te kaupapa e hangaia ana. I te mea kei roto i te mokowāingoa peka āhuatanga te pūtāutu tono kume - tōna nama, te ingoa rānei o te peka, me tohu tonu te pūtāutu i roto i te mahi.
    • Kei te rahua te hanga o nga manga matua. Hei tauira, kei a koe nga waahanga e whai ake nei: te tango i te kaupapa, te whakahaere i nga whakamatautau, te hanga i te kaupapa, te tuku tuku, te tuku whakamohiotanga, te whakakore i te peka ahua o te tono toia whakamutunga. Mena ka rahua te hanga i te wa e tuku ana i te panui, me whakakore a ringa koe i nga rauemi katoa i roto i te kohinga.
    • Ki te kore he horopaki tika, ka kore e kitea te whakakore i nga manga ahua i roto i te hanga matua.

  2. Te whakamahi i nga matau tukutuku (tauira).

    • Ehara pea tenei i to huarahi. Hei tauira, in Jenkins, kotahi noa te momo paipa e tautoko ana i te kaha ki te tiaki i ana whirihoranga i roto i te waehere puna. I te wa e whakamahi ana koe i nga matapae tukutuku, me tuhi koe i taau ake tuhinga hei tukatuka. Me tuu tenei tuhinga ki te atanga Jenkins, he uaua ki te pupuri.

  3. Tuhia Cronjob ka tāpirihia he kāhui Kubernetes.

    • Te whakapau wa ki te tuhi me te tautoko.
    • He rite tonu te mahi a te kaiwhakahaere, kua tuhia, kua tautokohia.

Mauruuru koe mo to aro ki te tuhinga. Hononga ki te kaupapa i runga i Github.

Source: will.com

Tāpiri i te kōrero