Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Spinnaker] Design CD on Kubernetes by Spinnaker

Posted on May 14, 2022May 21, 2022 By nim No Comments on [Spinnaker] Design CD on Kubernetes by Spinnaker

Chắc chăn là anh em đã nghe nói nhiều spinnaker được sử dụng để deploy lên K8s.

Bắt đâu làm theo Nim thôi

Contents

Toggle
  • 1) Preparing.
    • 1.1) Create helm package
    • 1.2) Configure the GitHub token on spinnaker.
  • 2) Create a pipeline of spinnaker so manually.
    • 2.1) Deploy according to the current replication of pod.
    • 2.2) Deploy according to values.yaml
    • 2.3) Check the result
  • Q&A
  • Spinnaker Pipeline Expressions

1) Preparing.

1.1) Create helm package

Đầu tiền bạn cần có 1 helm chart.

Tạo 1 helm chart

helm create nimtechnology

Lúc này nó sẽ tạo ra 1 folder nimtechnology.

Giờ thược hiện package lại helm chart

helm package ./nimtechnology/
sau đó bạn sẽ thấy file .tgz

Bạn tạo repo git và push mọi thứ lên đó.
https://github.com/mrnim94/kubernetes-manifest

1.2) Configure the GitHub token on spinnaker.

Bạn cần tạo 1 personal access token.

Sau đó run các command bên dưới trên spinnaker_halyard

hal config features edit --artifacts true
hal config artifact github enable
export GITHUB_ACCOUNT_NAME=<user_name_github>
hal config artifact github account add ${GITHUB_ACCOUNT_NAME} \
    --token <personal_access_token>

2) Create a pipeline of spinnaker so manually.

Giơ mình tạo 1 template bằng tay trước.

Tạo 1 application trước

Giờ tạo pipeline

giờ tạo các stage trong 1 pipeline

Stage 1: Find Artifacts From Resource
Mục đích của case này là lấy được số replicate

Chố này sao mình chọn được thì:
– config spinnaker connect với K8s (Nếu bạn cài helm thì chỗ này auto)
– trong ns default đã tạo sẵn 1 workload (deployment)

Ingnore the failure: chỗ này nên tích vì nếu đây là lần đầu tiên chạy pipeline thì chắc chắn spinnaker không get được thông tin gì(có workload đâu mà get)

2.1) Deploy according to the current replication of pod.

stage 2 là: Bake (Manifest)

chỗ này bạn config package helm

ở phần url của github bạn cần lưu ý chút

ví dụ file package của mình ở link này:
https://github.com/mrnim94/kubernetes-manifest/blob/master/nimtechnology-0.1.0.tgz

thì bạn sẽ input vào spinnaker link sau:

https://api.github.com/repos/mrnim94/kubernetes-manifest/contents/nimtechnology-0.1.0.tgz

Giờ bạn config để spinnaker thấy được file value của helm

Phần url này cũng cần lưu ý như bên trên:

https://api.github.com/repos/mrnim94/kubernetes-manifest/contents/nimtechnology/values.yaml


Bạn còn nhớ ở stage1 chúng ta đã có thể lấy được thông tin của workload(deployment)

Giờ chúng ta action đưa thông tin replicate từ stage 1 to stage 2
Mục tiêu là khi deploy không làm thay đổi số replicas của workload.

replicaCount
${ #stage("Get Manifest").context["manifest"]["spec"]["replicas"] }
${#stage("Get Manifest")["status"].toString() == "SUCCEEDED"}

Cấu hình ở trên nghĩa là nếu stage “Get Manifest” thành công thì mới chạy stage “Generate K8s Manifest 1“
==> vì ở Generate K8s Manifest 1 chúng ta đang lấy số replicas từ Get Manifest ,nếu Get Manifest thì Generate K8s Manifest 1 sẽ jenkins ra file manifest sai -> deploy sẽ bị lỗi.

Lăn xuống dưới chúng ta sẽ tạo file manifest cho bước sau để deploy thực hiện đặt tên lại cho đẹp

Giờ đến bước deploy lên k8s

Nếu Generate K8s Manifest 1 thì chạy deploy theo current replicas.
${#stage("Generate K8s Manifest 1")["status"].toString() == "SUCCEEDED"}

2.2) Deploy according to values.yaml

Nếu stage ở mục 2.1 bị fail thì chúng ta sẽ deploy và lấy số replicacount theo như file values.

Sau đây bước gen manifest nếu Get Manifest bị failed

Nếu Get Manifest fail thì mới chạy stage Generate K8s Manifest 2
Đây tạo ra cái Manifest 2

Giờ config deploy theo Manifest 2

Nếu Generate K8s Manifest 2 thành công thì mới chạy stage deploy này
${#stage("Generate K8s Manifest 2")["status"].toString() == "SUCCEEDED"}

Xong khi hoàn tất bạn ần save

sau đó run thử.

2.3) Check the result

Nếu mà lần đầu tiên tạo deployment thì như sau:

Bạn thấy Get Manifest bị fail

Lần thứ 2 deploy:

Đã get được current replicas của deployment

Giờ mình sẽ export template cho các bạn dễ kiểm tra

Bạn thực hiện click như hình để thấy được manifest mà spinnaker gen được.
{
  "keepWaitingPipelines": false,
  "lastModifiedBy": "anonymous",
  "limitConcurrent": true,
  "spelEvaluator": "v4",
  "stages": [
    {
      "account": "default",
      "completeOtherBranchesThenFail": false,
      "continuePipeline": true,
      "failOnFailedExpressions": false,
      "failPipeline": false,
      "location": "default",
      "manifestName": "deployment default-nimtechnology",
      "mode": "static",
      "name": "Get Manifest",
      "refId": "1",
      "requisiteStageRefIds": [],
      "restrictExecutionDuringTimeWindow": false,
      "type": "findArtifactsFromResource"
    },
    {
      "completeOtherBranchesThenFail": false,
      "continuePipeline": false,
      "evaluateOverrideExpressions": false,
      "expectedArtifacts": [
        {
          "defaultArtifact": {
            "customKind": true,
            "id": "98c0e91b-8b99-4d02-9fcf-c52d066f22ee"
          },
          "displayName": "Manifest 1",
          "id": "5f18c089-9347-4e3b-9d1e-43c57347a984",
          "matchArtifact": {
            "artifactAccount": "embedded-artifact",
            "customKind": false,
            "id": "b505ac48-b63b-438a-8cab-96955c11a985",
            "name": "Manifest 1",
            "type": "embedded/base64"
          },
          "useDefaultArtifact": false,
          "usePriorArtifact": false
        }
      ],
      "failOnFailedExpressions": false,
      "failPipeline": true,
      "inputArtifacts": [
        {
          "account": "mrnim94",
          "artifact": {
            "artifactAccount": "mrnim94",
            "id": "445e4ef5-bf73-4a8c-a2c3-6af72b0211f7",
            "name": "nimtechnology-0.1.0.tgz",
            "reference": "https://api.github.com/repos/mrnim94/kubernetes-manifest/contents/nimtechnology-0.1.0.tgz",
            "type": "github/file"
          }
        },
        {
          "account": "mrnim94",
          "artifact": {
            "artifactAccount": "mrnim94",
            "id": "3cea7c6f-e6e2-47e2-9601-f3117f03f5ba",
            "name": "nimtechnology/values.yaml",
            "reference": "https://api.github.com/repos/mrnim94/kubernetes-manifest/contents/nimtechnology/values.yaml",
            "type": "github/file"
          }
        }
      ],
      "name": "Generate K8s Manifest 1",
      "namespace": "default",
      "outputName": "default",
      "overrides": {
        "replicaCount": "${ #stage(\"Get Manifest\").context[\"manifest\"][\"spec\"][\"replicas\"] }"
      },
      "rawOverrides": false,
      "refId": "2",
      "requisiteStageRefIds": [
        "1"
      ],
      "stageEnabled": {
        "expression": "${#stage(\"Get Manifest\")[\"status\"].toString() == \"SUCCEEDED\"}",
        "type": "expression"
      },
      "templateRenderer": "HELM3",
      "type": "bakeManifest"
    },
    {
      "account": "cluster1",
      "cloudProvider": "kubernetes",
      "manifestArtifactId": "5f18c089-9347-4e3b-9d1e-43c57347a984",
      "moniker": {
        "app": "manual-create-app"
      },
      "name": "Deploy (Current ReplicaSet)",
      "namespaceOverride": "default",
      "refId": "3",
      "requisiteStageRefIds": [
        "2"
      ],
      "skipExpressionEvaluation": false,
      "source": "artifact",
      "stageEnabled": {
        "expression": "${#stage(\"Generate K8s Manifest 1\")[\"status\"].toString() == \"SUCCEEDED\"}",
        "type": "expression"
      },
      "trafficManagement": {
        "enabled": false,
        "options": {
          "enableTraffic": false
        }
      },
      "type": "deployManifest"
    },
    {
      "completeOtherBranchesThenFail": false,
      "continuePipeline": false,
      "expectedArtifacts": [
        {
          "defaultArtifact": {
            "customKind": true,
            "id": "2240fe9d-52c5-49ce-9fe2-5b5133f72a8e"
          },
          "displayName": "Manifest 2",
          "id": "c64b9cea-495f-43c0-b391-7fbd0e96c014",
          "matchArtifact": {
            "artifactAccount": "embedded-artifact",
            "customKind": false,
            "id": "784649d9-02a5-4f19-9b4d-ed87b6b8df96",
            "name": "Manifest 2",
            "type": "embedded/base64"
          },
          "useDefaultArtifact": false,
          "usePriorArtifact": false
        }
      ],
      "failPipeline": true,
      "inputArtifacts": [
        {
          "account": "mrnim94",
          "artifact": {
            "artifactAccount": "mrnim94",
            "id": "94e81b5f-ee7d-49a6-a059-5cbf85d0823e",
            "name": "nimtechnology-0.1.0.tgz",
            "reference": "https://api.github.com/repos/mrnim94/kubernetes-manifest/contents/nimtechnology-0.1.0.tgz",
            "type": "github/file"
          }
        },
        {
          "account": "mrnim94",
          "artifact": {
            "artifactAccount": "mrnim94",
            "id": "996f2c8b-178d-4060-a70a-5a06c8e63a27",
            "name": "nimtechnology/values.yaml",
            "reference": "https://api.github.com/repos/mrnim94/kubernetes-manifest/contents/nimtechnology/values.yaml",
            "type": "github/file"
          }
        }
      ],
      "name": "Generate K8s Manifest 2",
      "namespace": "default",
      "outputName": "default",
      "overrides": {},
      "rawOverrides": true,
      "refId": "4",
      "requisiteStageRefIds": [
        "1"
      ],
      "stageEnabled": {
        "expression": "${#stage(\"Get Manifest\")[\"status\"].toString() == \"FAILED_CONTINUE\"}",
        "type": "expression"
      },
      "templateRenderer": "HELM3",
      "type": "bakeManifest"
    },
    {
      "account": "cluster1",
      "cloudProvider": "kubernetes",
      "manifestArtifactId": "c64b9cea-495f-43c0-b391-7fbd0e96c014",
      "moniker": {
        "app": "manual-create-app"
      },
      "name": "Deploy (Value ReplicaSet)",
      "namespaceOverride": "default",
      "refId": "5",
      "requisiteStageRefIds": [
        "4"
      ],
      "skipExpressionEvaluation": false,
      "source": "artifact",
      "stageEnabled": {
        "expression": "${#stage(\"Generate K8s Manifest 2\")[\"status\"].toString() == \"SUCCEEDED\"}",
        "type": "expression"
      },
      "trafficManagement": {
        "enabled": false,
        "options": {
          "enableTraffic": false
        }
      },
      "type": "deployManifest"
    }
  ],
  "triggers": [],
  "updateTs": "1652515439000"
}

Q&A

Tên của workload(deployment) được tạo ntn?

Mình cũng đã thắc mắc sao workload lại có tên như thế
name của workload từ 2 thứ
Đầu tiên là từ config trong helm chart.

https://github.com/mrnim94/kubernetes-manifest/blob/master/nimtechnology/templates/deployment.yaml#L4

thứ 2 là config trong spinnaker.

Bạn sẽ hỏi cách trên manual như thế vậy với 1000 deployment thì tạo gãy tay à?
Có cách đầu tiên bạn export template như mình và chỉ cần sửa 1 vài thông tin rồi lại import vào spinnaker bằng spin (command line).
Hoặc ở bài tiếp theo mình sẽ chỉ 1 cách khác

Spinnaker Pipeline Expressions

One of the nice features in Spinnaker is pipeline expressions – a way to do a little dynamic calculation based on the current state of the pipeline.

Bạn thấy mình cũng có sử dụng trong stage “Get Manifest”

replicaCount
${ #stage("Get Manifest").context["manifest"]["spec"]["replicas"] }

Mình xin giải thích 1 chút chỗ này!
Nếu mà stage Get Manifest thì bước tiếp theo Generate K8s Manifest 1 sẽ lỗi mà manifest của chúng ta sẽ như sau:

Warning
Failed to evaluate [replicaCount] EL1012E: Cannot index into a null value
Bạn thấy đó nếu replicas như ảnh thì không thể nào deploy được!

Ngoài cách trên là mình tạo thêm 1 stage nữa thì chúng ta có thể sửa trong helm chart.

...
...
{{- $replicaCount := regexFind "\\d+\\.\\d+" .Values.replicaCount }}
...
...
  progressDeadlineSeconds: 1200
  {{- end }}
  {{- if .Values.ingress.canary.enabled }}
    {{- if $replicaCountCanary }}
  replicas: {{ $replicaCountCanary }}  
    {{- end }}
  {{- else if $replicaCount }}
  replicas: {{ $replicaCount }}
  {{- else }}
  replicas: 1
  {{- end }}
  selector:
    matchLabels

Nếu có lỗi j thì replicas sẽ bằng 1

Refence links:

https://www.paraesthesia.com/archive/2018/08/07/figuring-out-spinnaker-pipeline-expressions/
https://spinnaker.io/docs/guides/user/pipeline/expressions/

Spinnaker

Post navigation

Previous Post: [Docker] “unsafe repository” when building the Docker image.
Next Post: [Spinnaker] Design mode deploy automatically via template spinnaker!

More Related Articles

[spinnaker/Github] Login spinnaker through github Spinnaker
[Spinnaker] Design mode deploy automatically via template spinnaker! Spinnaker
[Spinnaker] Pipelines of an application hung. Spinnaker
[Spinnaker] Spin CLI Spinnaker
[Spinnaker-Github-Helm-K8S]Hướng dẫn cấu hình Spinnaker deploy lên kubernetes dựa vào Helm CI/CD
[Spinnaker/K8s] Add many Kubernetes clusters into spinnaker. Spinnaker

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025
  • [Windows] Remove the process that consumes too much CPU. June 3, 2025
  • Deploying Web-Based File Managers: File Browser and KubeFileBrowser with Docker and Kubernetes June 3, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.