├── .gitignore ├── LICENSE ├── README.md ├── bootstrap ├── deploy.sh └── destroy.sh ├── kubernetes └── jenkins-master │ ├── jenkins-pod-template.yaml │ └── jenkins-service.yaml └── zero-to-cd-on-gcp-diagram_phase1.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | jenkins-pod.yaml 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 James Heggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gcp-bootstrap-infrastructure 2 | Starting point for the GCP and K8S Continuous Delivery Seed 3 | 4 | ## Introduction 5 | 6 | Full details on the code and its usage can be found on the corresponding GCP blog: 7 | 8 | https://medium.com/google-cloud/zero-to-continuous-delivery-with-google-cloud-platform-8e3bf1312fb5#.87imb0tqq 9 | 10 | 11 | ## Instructions 12 | 13 | The bootstrap script takes the following arguments: 14 | 15 | 1) **GCP_PROJECT** The name of your Google Cloud Project 16 | 17 | 2) **GCP_ZONE** The GCP zone for your GKE cluster 18 | 19 | 3) **GCP_MACHINE_TYPE** The GCP machine type 20 | 21 | 4) **NUM_NODES** The number of nodes that should make up your cluster 22 | 23 | ``` 24 | cd bootstrap && sh deploy.sh {project_name} {zone} {machine-type} {number-of-nodes} {service_account_key} 25 | ``` 26 | 27 | For example 28 | 29 | ``` 30 | cd bootstrap && \ 31 | sh deploy.sh \ 32 | my-gcp-project \ 33 | europe-west1-b \ 34 | n1-standard-2 \ 35 | 1 \ 36 | /somedir/docker-meetup-a08ce8d4d8bc.json 37 | ``` 38 | 39 | The script will ask you to log in to your Google Account. 40 | 41 | Once verified it will continue to create a GKE cluster and in turn deploy 42 | a Jenkins master container on to the cluster. 43 | 44 | The console will then confirm the actions it is about to take and ask you to press any key to continue. 45 | 46 | ## Overview 47 | 48 | ![Overview of process](https://raw.githubusercontent.com/eggsy84/gcp-bootstrap-infrastructure/master/zero-to-cd-on-gcp-diagram_phase1.jpg "Process Overview") 49 | 50 | ## IMPORTANT NOTE 51 | 52 | This script will provision infrastructure on Google Cloud and you may incur costs for having active infrastructure within the platform 53 | -------------------------------------------------------------------------------- /bootstrap/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Store base directory for starting point of script executions 4 | BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd ../ && pwd )" 5 | 6 | GCP_PROJECT=${1:-GCP-CD} 7 | GCP_ZONE=${2:-europe-west1-b} 8 | GCP_MACHINE_TYPE=${3:-n1-standard-2} 9 | NUM_NODES=${4:-1} 10 | SERVICE_ACCOUNT_FILE=${5:-./service_account.json} 11 | 12 | validate_environment() { 13 | # Check pre-requisites for required command line tools 14 | 15 | printf "\nChecking pre-requisites for required tooling" 16 | 17 | command -v gcloud >/dev/null 2>&1 || { echo >&2 "Google Cloud SDK required - doesn't seem to be on your path. Aborting."; exit 1; } 18 | command -v kubectl >/dev/null 2>&1 || { echo >&2 "Kubernetes commands required - doesn't seem to be on your path. Aborting."; exit 1; } 19 | 20 | printf "\nAll pre-requisite software seem to be installed :)" 21 | } 22 | 23 | authorise_gcp() { 24 | gcloud auth application-default login 25 | gcloud config set project $GCP_PROJECT 26 | gcloud config set compute/zone $GCP_ZONE 27 | 28 | printf "\nAbout to create a Container Cluster in the '$GCP_PROJECT' GCP project located in '$GCP_ZONE' with $NUM_NODES x '$GCP_MACHINE_TYPE' node(s)\n" 29 | read -rsp $'Press any key to continue...or Ctrl+C to exit\n' -n1 key 30 | } 31 | 32 | build_gcp_cluster() { 33 | gcloud container clusters create "cd-cluster" \ 34 | --zone "$GCP_ZONE" \ 35 | --machine-type "$GCP_MACHINE_TYPE" \ 36 | --num-nodes "$NUM_NODES" \ 37 | --network "default" \ 38 | --username "admin" \ 39 | --cluster-version "1.5.7" 40 | 41 | gcloud config set container/cluster cd-cluster 42 | gcloud container clusters get-credentials cd-cluster 43 | } 44 | 45 | build_jenkins_server() { 46 | printf "\nProvisioning Jenkins Service...\n" 47 | cd $BASE_DIR/kubernetes/jenkins-master 48 | kubectl create -f jenkins-service.yaml 49 | printf "Jenkins Service created\n" 50 | printf "Waiting for public Jenkins ingress point..." 51 | JENKINS_ADDRESS=''; while [[ "e$JENKINS_ADDRESS" == "e" ]]; do JENKINS_ADDRESS=`kubectl describe service/jenkins-ui 2>/dev/null | grep "LoadBalancer\ Ingress" | cut -f2`; printf "."; done; 52 | 53 | cd $BASE_DIR/kubernetes/jenkins-master 54 | printf "\nProvisioning Jenkins Pod...\n" 55 | 56 | # Update Google Cloud Project environment variable 57 | cp jenkins-pod-template.yaml jenkins-pod.yaml 58 | sed -i.bak "s/GCP_PROJECT_DEFAULT_VALUE/$GCP_PROJECT/" jenkins-pod.yaml 59 | kubectl create -f jenkins-pod.yaml 60 | rm jenkins-pod.yaml 61 | rm jenkins-pod.yaml.bak 62 | 63 | printf "\nJenkins service up and running on $JENKINS_ADDRESS\n" 64 | } 65 | 66 | create_service_account_secret() { 67 | printf "\nConfiguring service account k8s secret from $SERVICE_ACCOUNT_FILE\n" 68 | cp "$SERVICE_ACCOUNT_FILE" ./gcloud-svc-account.json 69 | kubectl create secret generic gcloud-svc-account --from-file=gcloud-svc-account.json 70 | rm gcloud-svc-account.json 71 | printf "Completed service account secret creation\n" 72 | } 73 | 74 | 75 | _main() { 76 | 77 | validate_environment 78 | 79 | printf "\nProvisioning development environment...." 80 | 81 | # Authorise google cloud SDK 82 | authorise_gcp 83 | 84 | # Utilise terraform to provision the Google Cluster 85 | build_gcp_cluster 86 | 87 | # Push Go CD out on to the cluster 88 | build_jenkins_server 89 | 90 | # Create k8s secret of Google service account 91 | create_service_account_secret 92 | 93 | printf "\nCompleted provisioning development environment!!\n\n" 94 | } 95 | 96 | _main 97 | -------------------------------------------------------------------------------- /bootstrap/destroy.sh: -------------------------------------------------------------------------------- 1 | #TODO 2 | #Should delete cluster, load balancer, firewall rules... 3 | -------------------------------------------------------------------------------- /kubernetes/jenkins-master/jenkins-pod-template.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ReplicationController 3 | metadata: 4 | name: jenkins-server 5 | spec: 6 | replicas: 1 7 | template: 8 | metadata: 9 | labels: 10 | name: jenkins-server 11 | spec: 12 | containers: 13 | - name: jenkins-server 14 | image: eggsy84/gcp-jenkins-master-k8s-seed:2.32.2-alpine 15 | env: 16 | - name: GCP_PROJECT 17 | value: GCP_PROJECT_DEFAULT_VALUE 18 | ports: 19 | - containerPort: 8080 20 | - containerPort: 50000 21 | -------------------------------------------------------------------------------- /kubernetes/jenkins-master/jenkins-service.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | metadata: 5 | name: jenkins-ui 6 | spec: 7 | type: LoadBalancer 8 | ports: 9 | - port: 80 10 | name: jenkins-http 11 | targetPort: 8080 12 | protocol: TCP 13 | selector: 14 | name: jenkins-server 15 | 16 | --- 17 | kind: Service 18 | apiVersion: v1 19 | metadata: 20 | name: jenkins-discovery 21 | spec: 22 | ports: 23 | - port: 50000 24 | name: jenkins-slaves 25 | targetPort: 50000 26 | protocol: TCP 27 | selector: 28 | name: jenkins-server 29 | -------------------------------------------------------------------------------- /zero-to-cd-on-gcp-diagram_phase1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eggsy84/gcp-bootstrap-infrastructure/7b853f02c14323f61427b4067e0ddfa158b8437d/zero-to-cd-on-gcp-diagram_phase1.jpg --------------------------------------------------------------------------------