|
| 1 | +version: "3" |
| 2 | + |
| 3 | +env: |
| 4 | + CLUSTER_NAME: devops-directive-kubernetes-course |
| 5 | + CIVO_REGION: NYC1 |
| 6 | + GCP_REGION: us-central1 |
| 7 | + GCP_ZONE: us-central1-a |
| 8 | + # Set default gum style options |
| 9 | + BORDER: double |
| 10 | + BORDER_FOREGROUND: "212" |
| 11 | + PADDING: "1 1" |
| 12 | + MARGIN: "1 1" |
| 13 | + |
| 14 | +tasks: |
| 15 | + civo:00-authenticate-cli: |
| 16 | + cmds: |
| 17 | + - cmd: | |
| 18 | + gum style "$(cat <<EOF |
| 19 | + To get an API key you need to: |
| 20 | + --- |
| 21 | + 1. Log in or create an account at https://dashboard.civo.com/ |
| 22 | + 2. Create a team at https://dashboard.civo.com/teams |
| 23 | + 3. Add yourself to the team |
| 24 | + 4. Navigate to https://dashboard.civo.com/security to get the api key |
| 25 | +
|
| 26 | + 🚨🚨🚨 NOTE: Sometimes account verification required for new accounts |
| 27 | + (so sign up before you want to use it!) 🚨🚨🚨 |
| 28 | + EOF |
| 29 | + )" |
| 30 | + silent: true |
| 31 | + - civo apikey save |
| 32 | + - civo apikey ls |
| 33 | + - cmd: gum style "run \`civo apikey current <KEY_NAME>\` to set the current key as the default (if it is not already)" |
| 34 | + silent: true |
| 35 | + desc: Authenticate the Civo CLI |
| 36 | + |
| 37 | + civo:01-create-network: |
| 38 | + cmds: |
| 39 | + - civo network create ${CLUSTER_NAME} --region ${CIVO_REGION} |
| 40 | + desc: Create a Civo network |
| 41 | + |
| 42 | + civo:02-create-firewall: |
| 43 | + cmds: |
| 44 | + - | |
| 45 | + civo firewall create ${CLUSTER_NAME} \ |
| 46 | + --network ${CLUSTER_NAME} \ |
| 47 | + --create-rules false \ |
| 48 | + --region ${CIVO_REGION} |
| 49 | + - | |
| 50 | + ingress_rule_ids=$(civo firewall rule ls --region ${CIVO_REGION} ${CLUSTER_NAME} -o json | jq -r '.[] | select(.direction == "ingress") | .id') |
| 51 | + for rule_id in $ingress_rule_ids; do |
| 52 | + civo firewall rule remove ${CLUSTER_NAME} $rule_id -y --region ${CIVO_REGION} |
| 53 | + done |
| 54 | + - civo firewall rule create ${CLUSTER_NAME} --startport 80 --endport 80 --cidr 0.0.0.0/0 --protocol TCP --region ${CIVO_REGION} |
| 55 | + - civo firewall rule create ${CLUSTER_NAME} --startport 443 --endport 443 --cidr 0.0.0.0/0 --protocol TCP --region ${CIVO_REGION} |
| 56 | + - civo firewall rule create ${CLUSTER_NAME} --startport 6443 --endport 6443 --cidr 0.0.0.0/0 --protocol TCP --region ${CIVO_REGION} |
| 57 | + - cmd: gum style "🚨 If you wanted to lock down access to the k8s api, you could instead only allow traffic on 6443 from your IP (or that of a bastion host)" |
| 58 | + silent: true |
| 59 | + desc: Create a Civo firewall and set up rules |
| 60 | + |
| 61 | + civo:03-create-cluster: |
| 62 | + cmds: |
| 63 | + - | |
| 64 | + civo kubernetes create ${CLUSTER_NAME} \ |
| 65 | + --region ${CIVO_REGION} \ |
| 66 | + --network ${CLUSTER_NAME} \ |
| 67 | + --existing-firewall ${CLUSTER_NAME} \ |
| 68 | + --nodes 2 \ |
| 69 | + --size g4s.kube.medium \ |
| 70 | + --remove-applications "traefik2-nodeport" \ |
| 71 | + --wait |
| 72 | + desc: Create a Civo Kubernetes cluster |
| 73 | + |
| 74 | + civo:04-create-all: |
| 75 | + cmds: |
| 76 | + - task: civo:01-create-network |
| 77 | + - task: civo:02-create-firewall |
| 78 | + - task: civo:03-create-cluster |
| 79 | + desc: Create the Civo network, firewall, and cluster in sequence |
| 80 | + |
| 81 | + civo:05-get-kubeconfig: |
| 82 | + cmds: |
| 83 | + - civo kubernetes config ${CLUSTER_NAME} --region ${CIVO_REGION} --save --switch |
| 84 | + desc: Get kubeconfig for the cluster |
| 85 | + |
| 86 | + civo:06-clean-up: |
| 87 | + cmds: |
| 88 | + - civo kubernetes delete ${CLUSTER_NAME} --region ${CIVO_REGION} -y |
| 89 | + - cmd: gum style "There is some delay on the civo side from cluster being deleted to it being removed from the firewall rule usage" |
| 90 | + silent: true |
| 91 | + - sleep 10 |
| 92 | + - civo firewall delete ${CLUSTER_NAME} --region ${CIVO_REGION} -y |
| 93 | + - civo network delete ${CLUSTER_NAME} --region ${CIVO_REGION} -y |
| 94 | + desc: Clean up the Civo Kubernetes cluster and associated resources |
| 95 | + |
| 96 | + gcp:01-init-cli: |
| 97 | + cmds: |
| 98 | + - gcloud init |
| 99 | + desc: "Authenticate and configure the gcloud CLI" |
| 100 | + |
| 101 | + gcp:02-enable-apis: |
| 102 | + cmds: |
| 103 | + - | |
| 104 | + gcloud services enable \ |
| 105 | + compute.googleapis.com \ |
| 106 | + container.googleapis.com \ |
| 107 | + cloudresourcemanager.googleapis.com \ |
| 108 | + iam.googleapis.com \ |
| 109 | + secretmanager.googleapis.com \ |
| 110 | + servicemanagement.googleapis.com \ |
| 111 | + serviceusage.googleapis.com |
| 112 | + desc: "Enable necessary APIs" |
| 113 | + |
| 114 | + gcp:03-set-region-and-zone: |
| 115 | + cmds: |
| 116 | + - gcloud config set compute/region ${GCP_REGION} |
| 117 | + - gcloud config set compute/zone ${GCP_ZONE} |
| 118 | + desc: "Set default region and zone" |
| 119 | + |
| 120 | + gcp:04-create-vpc: |
| 121 | + cmds: |
| 122 | + - gcloud compute networks create ${CLUSTER_NAME} --subnet-mode=custom |
| 123 | + desc: "Create VPC" |
| 124 | + |
| 125 | + gcp:05-create-subnet: |
| 126 | + cmds: |
| 127 | + - | |
| 128 | + gcloud compute networks subnets create subnet-1 \ |
| 129 | + --network=${CLUSTER_NAME} \ |
| 130 | + --region=${GCP_REGION} \ |
| 131 | + --range=10.0.0.0/20 |
| 132 | + desc: "Create subnet" |
| 133 | + |
| 134 | + gcp:06-create-cluster: |
| 135 | + desc: "Create GKE cluster" |
| 136 | + vars: |
| 137 | + GCP_PROJECT_ID: kubernetes-course-424917 |
| 138 | + cmds: |
| 139 | + - | |
| 140 | + gcloud container clusters create ${CLUSTER_NAME} \ |
| 141 | + --zone ${GCP_ZONE} \ |
| 142 | + --network ${CLUSTER_NAME} \ |
| 143 | + --subnetwork subnet-1 \ |
| 144 | + --machine-type e2-standard-2 \ |
| 145 | + --num-nodes 2 \ |
| 146 | + --gateway-api=standard \ |
| 147 | + --workload-pool={{.GCP_PROJECT_ID}}.svc.id.goog |
| 148 | +
|
| 149 | + gcp:07-create-all: |
| 150 | + cmds: |
| 151 | + - task: gcp:02-enable-apis |
| 152 | + - task: gcp:03-set-region-and-zone |
| 153 | + - task: gcp:04-create-vpc |
| 154 | + - task: gcp:05-create-subnet |
| 155 | + - task: gcp:06-create-cluster |
| 156 | + desc: Create the GCP network, subnet, firewall rules, and cluster in sequence |
| 157 | + |
| 158 | + gcp:09-clean-up: |
| 159 | + cmds: |
| 160 | + - gcloud container clusters delete ${CLUSTER_NAME} --zone ${GCP_ZONE} --quiet |
| 161 | + - gcloud compute networks subnets delete subnet-1 --region=${GCP_REGION} --quiet |
| 162 | + - gcloud compute networks delete ${CLUSTER_NAME} --quiet |
| 163 | + desc: Delete the GCP network, subnet, firewall rules, and cluster in reverse sequence |
| 164 | + |
| 165 | + gcp:08-connect-to-cluster: |
| 166 | + cmds: |
| 167 | + - gcloud container clusters get-credentials ${CLUSTER_NAME} --zone ${GCP_ZONE} |
| 168 | + desc: "Connect to the GKE cluster" |
| 169 | + |
| 170 | + kind:01-generate-config: |
| 171 | + cmds: |
| 172 | + - REPLACE_WITH_ABSOLUTE_PATH=${PWD} envsubst < kind-config.yaml.TEMPLATE > kind-config.yaml |
| 173 | + desc: "Generate kind config with local absolute paths for PV mounts" |
| 174 | + |
| 175 | + kind:02-create-cluster: |
| 176 | + cmds: |
| 177 | + - kind create cluster --config kind-config.yaml |
| 178 | + desc: Create a Kubernetes cluster using kind |
| 179 | + |
| 180 | + kind:03-run-cloud-provider-kind: |
| 181 | + desc: "Run sigs.k8s.io/cloud-provider-kind@latest to enable load balancer services with KinD" |
| 182 | + cmds: |
| 183 | + - sudo cloud-provider-kind |
| 184 | + |
| 185 | + kind:04-delete-cluster: |
| 186 | + cmds: |
| 187 | + - kind delete cluster |
| 188 | + desc: Delete and existing a kind Kubernetes cluster |
0 commit comments