Skip to content

Instantly share code, notes, and snippets.

@yihyang
Last active April 21, 2020 17:30
Show Gist options
  • Save yihyang/a0dc1f65c2d263217349590cbf7bcf15 to your computer and use it in GitHub Desktop.
Save yihyang/a0dc1f65c2d263217349590cbf7bcf15 to your computer and use it in GitHub Desktop.

Revisions

  1. yihyang revised this gist Apr 21, 2020. 1 changed file with 134 additions and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    Continuous Delivery Pipelines with Spinnaker and Kubernetes Engine

    # Set up your environment

    gcloud config set compute/zone us-central1-f
    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


    # 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

    # Deploying Spinnaker using Helm
    wget https://get.helm.sh/helm-v3.1.0-linux-amd64.tar.gz
    tar zxfv helm-v3.1.0-linux-amd64.tar.gz
    cp linux-amd64/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://kubernetes-charts.storage.googleapis.com
    ./helm repo update

    # Configure Spinnaker
    export PROJECT=$(gcloud info \
    --format='value(config.project)')
    export BUCKET=$PROJECT-spinnaker-config

    gsutil mb -c regional -l us-central1 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: [email protected]

    # Disable minio as the default storage backend
    minio:
    enabled: false

    # Configure Spinnaker to enable GCP services
    halyard:
    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

    ./helm install -n default cd stable/spinnaker -f spinnaker-config.yaml \
    --version 1.23.0 --timeout 10m0s --wait
    export DECK_POD=$(kubectl get pods --namespace default -l "cluster=spin-deck" \
    -o jsonpath="{.items[0].metadata.name}")



    # Building the Docker image
    wget https://gke-spinnaker.storage.googleapis.com/sample-app-v2.tgz
    tar xzfv sample-app-v2.tgz
    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


    # Prepare your Kubernetes Manifests for use in Spinnaker
    export PROJECT=$(gcloud info --format='value(config.project)')
    gsutil mb -l us-central1 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


    # Configuring your deployment pipelines
    curl -LO https://storage.googleapis.com/spinnaker-artifacts/spin/1.14.0/linux/amd64/spin
    chmod +x spin
    ./spin application save --application-name sample \
    --owner-email "$(gcloud config get-value core/account)" \
    --cloud-providers kubernetes \
    --gate-endpoint http://localhost:8080/gate

    # 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
  2. yihyang revised this gist Apr 20, 2020. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions 06 - Site Reliability Troubleshooting with Cloud Monitoring APM
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    Site Reliability Troubleshooting with Cloud Monitoring APM

    # Infrastructure setup
    gcloud config set compute/zone us-west1-b
    export PROJECT_ID=$(gcloud info --format='value(config.project)')
    gcloud container clusters list

    # Check your cluster
    gcloud container clusters get-credentials shop-cluster --zone us-west1-b
    kubectl get nodes

    # Deploy application
    git clone -b APM-Troubleshooting-Demo-2 https://github.com/blipzimmerman/microservices-demo-1
    curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/v0.36.0/skaffold-linux-amd64 && chmod +x skaffold && sudo mv skaffold /usr/local/bin
    cd microservices-demo-1
    skaffold run
    kubectl get pods
    export EXTERNAL_IP=$(kubectl get service frontend-external | awk 'BEGIN { cnt=0; } { cnt+=1; if (cnt > 1) print $4; }')
    curl -o /dev/null -s -w "%{http_code}\n" http://$EXTERNAL_IP
    ./setup_csr.sh

    # Develop Sample SLOs and SLIs
  3. yihyang created this gist Apr 18, 2020.
    19 changes: 19 additions & 0 deletions 02 - Cloud Source Repositories: Qwik Start
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    Cloud Source Repositories: Qwik Start

    # Create a new repository
    gcloud source repos create REPO_DEMO

    # Clone the new repository into your Cloud Shell session
    gcloud source repos clone REPO_DEMO


    # Push to the Cloud Source Repository
    cd REPO_DEMO
    echo "Hello World!" > myfile.txt
    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"
    git add myfile.txt
    git commit -m "First file using Cloud Source Repositories" myfile.txt
    git push origin master

    # Browse files in the Google Cloud Source repository