Site icon Learning & Doing

Continuous Delivery Pipelines with Spinnaker and Kubernetes Engine

pipelines

“Continuous Delivery Pipelines with Spinnaker and Kubernetes Engine”

Pengantar

Lab praktis ini menunjukkan cara membuat pipeline continuous delivery menggunakan Google Kubernetes Engine, Google Cloud Source Repositories, Google Cloud Container Builder, dan Spinnaker. Setelah Anda membuat aplikasi sampel, Anda mengonfigurasi layanan ini untuk membangun, menguji, dan menerapkannya secara otomatis. Saat Anda mengubah kode aplikasi, perubahan tersebut memicu alur pengiriman berkelanjutan untuk secara otomatis membuat ulang, menguji ulang, dan menerapkan ulang versi baru.

Pipeline architecture

Application delivery pipeline

Praktikum

Task 1. Set up your environment

gcloud config set compute/zone us-east1-d
gcloud container clusters create spinnaker-tutorial \
    --machine-type=n1-standard-2

Configure identity and access management

gcloud iam service-accounts create spinnaker-account \
    --display-name spinnaker-account
export SA_EMAIL=$(gcloud iam service-accounts list \
    --filter="displayName:spinnaker-account" \
    --format='value(email)')
export PROJECT=$(gcloud info --format='value(config.project)')
gcloud projects add-iam-policy-binding $PROJECT \
    --role roles/storage.admin \
    --member serviceAccount:$SA_EMAIL
gcloud iam service-accounts keys create spinnaker-sa.json \
     --iam-account $SA_EMAIL

Task 2. Set up Cloud Pub/Sub to trigger Spinnaker pipelines

gcloud pubsub topics create projects/$PROJECT/topics/gcr
gcloud pubsub subscriptions create gcr-triggers \
    --topic projects/${PROJECT}/topics/gcr
export SA_EMAIL=$(gcloud iam service-accounts list \
    --filter="displayName:spinnaker-account" \
    --format='value(email)')
gcloud beta pubsub subscriptions add-iam-policy-binding gcr-triggers \
    --role roles/pubsub.subscriber --member serviceAccount:$SA_EMAIL

Task 3. Deploying Spinnaker using Helm

Configure Helm

kubectl create clusterrolebinding user-admin-binding \
    --clusterrole=cluster-admin --user=$(gcloud config get-value account)
kubectl create clusterrolebinding --clusterrole=cluster-admin \
    --serviceaccount=default:default spinnaker-admin
helm repo add stable https://charts.helm.sh/stable
helm repo update

Configure Spinnaker

export PROJECT=$(gcloud info \
    --format='value(config.project)')

export BUCKET=$PROJECT-spinnaker-config

gsutil mb -c regional -l us-east1 gs://$BUCKET
export SA_JSON=$(cat spinnaker-sa.json)
export PROJECT=$(gcloud info --format='value(config.project)')
export BUCKET=$PROJECT-spinnaker-config
cat > spinnaker-config.yaml <<EOF
gcs:
  enabled: true
  bucket: $BUCKET
  project: $PROJECT
  jsonKey: '$SA_JSON'
dockerRegistries:
- name: gcr
  address: https://gcr.io
  username: _json_key
  password: '$SA_JSON'
  email: 1234@5678.com
# Disable minio as the default storage backend
minio:
  enabled: false
# Configure Spinnaker to enable GCP services
halyard:
  spinnakerVersion: 1.19.4
  image:
    repository: us-docker.pkg.dev/spinnaker-community/docker/halyard
    tag: 1.32.0
    pullSecrets: []
  additionalScripts:
    create: true
    data:
      enable_gcs_artifacts.sh: |-
        \$HAL_COMMAND config artifact gcs account add gcs-$PROJECT --json-path /opt/gcs/key.json
        \$HAL_COMMAND config artifact gcs enable
      enable_pubsub_triggers.sh: |-
        \$HAL_COMMAND config pubsub google enable
        \$HAL_COMMAND config pubsub google subscription add gcr-triggers \
          --subscription-name gcr-triggers \
          --json-path /opt/gcs/key.json \
          --project $PROJECT \
          --message-format GCR
EOF

Deploy the Spinnaker chart

helm install -n default cd stable/spinnaker -f spinnaker-config.yaml \
           --version 2.0.0-rc9 --timeout 10m0s --wait
export DECK_POD=$(kubectl get pods --namespace default -l "cluster=spin-deck" \
    -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward --namespace default $DECK_POD 8080:9000 >> /dev/null &

Task 4. Building the Docker image

Create your source code repository

gsutil -m cp -r gs://spls/gsp114/sample-app.tar .

mkdir sample-app
tar xvf sample-app.tar -C ./sample-app

cd sample-app

git config --global user.email "$(gcloud config get-value core/account)"

git config --global user.name "[USERNAME]"

git init

git add .

git commit -m "Initial commit"

gcloud source repos create sample-app

git config credential.helper gcloud.sh

export PROJECT=$(gcloud info --format='value(config.project)')

git remote add origin https://source.developers.google.com/p/$PROJECT/r/sample-app

git push origin master

Configure your build triggers

    Name: sample-app-tags

    Event: Push new tag

    Select your newly created sample-app repository.

    Tag: .*(any tag)

    Configuration: Cloud Build configuration file (yaml or json)

    Cloud Build configuration file location: /cloudbuild.yaml

Prepare your Kubernetes Manifests for use in Spinnaker

export PROJECT=$(gcloud info --format='value(config.project)')

gsutil mb -l us-east1 gs://$PROJECT-kubernetes-manifests
gsutil versioning set on gs://$PROJECT-kubernetes-manifests
sed -i s/PROJECT/$PROJECT/g k8s/deployments/*
git commit -a -m "Set project ID"

Build your image

git tag v1.0.0

git push --tags

Task 5. Configuring your deployment pipelines

Install the spin CLI for managing Spinnaker

curl -LO https://storage.googleapis.com/spinnaker-artifacts/spin/1.14.0/linux/amd64/spin

chmod +x spin

Create the deployment pipeline

./spin application save --application-name sample \
                        --owner-email "$(gcloud config get-value core/account)" \
                        --cloud-providers kubernetes \
                        --gate-endpoint http://localhost:8080/gate
export PROJECT=$(gcloud info --format='value(config.project)')
sed s/PROJECT/$PROJECT/g spinnaker/pipeline-deploy.json > pipeline.json
./spin pipeline save --gate-endpoint http://localhost:8080/gate -f pipeline.json

Manually trigger and view your pipeline execution

Task 6. Triggering your pipeline from code changes

sed -i 's/orange/blue/g' cmd/gke-info/common-service.go
git commit -a -m "Change color to blue"

git tag v1.0.1

git push --tags

Task 7. Observe the canary deployments

git revert v1.0.1

git tag v1.0.2

git push --tags

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Continuous Delivery Pipelines with Spinnaker and Kubernetes Engine. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

Exit mobile version