|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Author: Bin Wu <[email protected]> |
| 4 | + |
| 5 | +pwd=`pwd` |
| 6 | +cluster_name=elk-demo |
| 7 | +region=asia-east1 |
| 8 | +# zone=asia-east1-a |
| 9 | +project_id=du-hast-mich |
| 10 | +default_pool=default-pool |
| 11 | +nodes_per_zone=5 # per zone |
| 12 | +machine_type=e2-standard-2 |
| 13 | +release_channel=None # None -> static, e.g. rapid, regular, stable |
| 14 | +gke_version=1.23.6-gke.1500 |
| 15 | +eck_version=2.2.0 |
| 16 | +es_cluster_name=dingo-demo |
| 17 | + |
| 18 | +__create_gke() { |
| 19 | + #--zone "${zone}" \ |
| 20 | + #--node-locations "${region}-a,${region}-b,${region}-c" |
| 21 | + #--num-nodes "1" for regional/multi-zone cluster, this is the number in each zone |
| 22 | + gcloud beta container \ |
| 23 | + --project "${project_id}" clusters create "$cluster_name" \ |
| 24 | + --zone "${region}-a" \ |
| 25 | + --node-locations "${region}-a" \ |
| 26 | + --no-enable-basic-auth \ |
| 27 | + --enable-dataplane-v2 \ |
| 28 | + --release-channel "${release_channel}" \ |
| 29 | + --cluster-version "${gke_version}" \ |
| 30 | + --machine-type "$machine_type" \ |
| 31 | + --image-type "COS_CONTAINERD" \ |
| 32 | + --disk-type "pd-ssd" \ |
| 33 | + --disk-size "20" \ |
| 34 | + --metadata disable-legacy-endpoints=true \ |
| 35 | + --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \ |
| 36 | + --num-nodes "$nodes_per_zone" \ |
| 37 | + --logging=SYSTEM,WORKLOAD \ |
| 38 | + --monitoring=SYSTEM,WORKLOAD \ |
| 39 | + --enable-ip-alias \ |
| 40 | + --network "projects/${project_id}/global/networks/default" \ |
| 41 | + --subnetwork "projects/${project_id}/regions/$region/subnetworks/default" \ |
| 42 | + --default-max-pods-per-node "110" \ |
| 43 | + --no-enable-master-authorized-networks \ |
| 44 | + --addons HorizontalPodAutoscaling,HttpLoadBalancing \ |
| 45 | + --no-enable-autoupgrade \ |
| 46 | + --max-surge-upgrade 1 \ |
| 47 | + --max-unavailable-upgrade 0 \ |
| 48 | + --enable-autorepair |
| 49 | + |
| 50 | + __init |
| 51 | +} |
| 52 | + |
| 53 | +# setup the deployment enviroment for Elastic Stack |
| 54 | +__init() { |
| 55 | + # Set kubectl to target the created cluster |
| 56 | + gcloud container clusters get-credentials $cluster_name \ |
| 57 | + --zone "${region}-a" \ |
| 58 | + --project ${project_id} |
| 59 | + |
| 60 | + # sysctl -w vm.max_map_count=262144 for every GKE node |
| 61 | + # Option 1 |
| 62 | + # $pwd/bin/gke_sysctl_vmmaxmapcount.sh |
| 63 | + # Option 2 |
| 64 | + kubectl apply -f $pwd/conf/node-daemon.yml |
| 65 | + |
| 66 | + # Install ECK |
| 67 | + [ -f $pwd/conf/crds.yaml ] || \ |
| 68 | + curl https://download.elastic.co/downloads/eck/$eck_version/crds.yaml --output $pwd/conf/crds.yaml |
| 69 | + kubectl create -f $pwd/conf/crds.yaml |
| 70 | + |
| 71 | + [ -f $pwd/conf/operator.yaml ] || \ |
| 72 | + curl https://download.elastic.co/downloads/eck/$eck_version/operator.yaml --output $pwd/conf/operator.yaml |
| 73 | + kubectl apply -f $pwd/conf/operator.yaml |
| 74 | + |
| 75 | + # create storage class |
| 76 | + kubectl create -f $pwd/conf/storage.yml |
| 77 | + |
| 78 | + ## make it default |
| 79 | + |
| 80 | + # 1. switch default class to false |
| 81 | + #kubectl patch storageclass standard \ |
| 82 | + #-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}' |
| 83 | + |
| 84 | + # 2. switch the default class to true for custom storage class |
| 85 | + #kubectl patch storageclass dingo-pdssd \ |
| 86 | + #-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}' |
| 87 | +} |
| 88 | + |
| 89 | +__init_gcp_credentials() { |
| 90 | + # FIXME: you may want a minimum privilege service account here just for GCS |
| 91 | + [ -f $pwd/conf/gcs.client.default.credentials_file ] || \ |
| 92 | + cp $GOOGLE_APPLICATION_CREDENTIALS $pwd/conf/gcs.client.default.credentials_file |
| 93 | + |
| 94 | + # Optional: setup a GCP service account that can manipulate GCS for snapshots |
| 95 | + kubectl create secret generic gcs-credentials \ |
| 96 | + --from-file=$pwd/conf/gcs.client.default.credentials_file |
| 97 | +} |
| 98 | + |
| 99 | +__deploy_elastic() { |
| 100 | + __init_gcp_credentials |
| 101 | + |
| 102 | + kubectl apply -f $pwd/templates/es.demo.yml |
| 103 | + kubectl apply -f $pwd/templates/kbn.demo.yml |
| 104 | +} |
| 105 | + |
| 106 | +__deploy_demo() { |
| 107 | + __create_gke |
| 108 | + |
| 109 | + __deploy_elastic |
| 110 | +} |
| 111 | + |
| 112 | +__password() { |
| 113 | + # kubectl get secret ${es_cluster_name}-es-elastic-user -o=jsonpath='{.data.elastic}' | base64 --decode |
| 114 | + kubectl get secret ${es_cluster_name}-es-elastic-user -o go-template='{{.data.elastic | base64decode}}' |
| 115 | +} |
| 116 | + |
| 117 | +__password_reset() { |
| 118 | + kubectl delete secret ${es_cluster_name}-es-elastic-user |
| 119 | +} |
| 120 | + |
| 121 | +__status() { |
| 122 | + passwd=$(__password) |
| 123 | + lb_ip=`kubectl get services ${es_cluster_name}-es-http -o jsonpath='{.status.loadBalancer.ingress[0].ip}'` |
| 124 | + |
| 125 | + #curl -u "elastic:$passwd" -k "https://$lb_ip:9200" |
| 126 | + |
| 127 | + kbn_ip=`kubectl get service dingo-demo-kbn-kb-http -o jsonpath='{.status.loadBalancer.ingress[0].ip}'` |
| 128 | + kbn_port=5601 |
| 129 | + kbn_url=https://${kbn_ip}:${kbn_port} |
| 130 | + |
| 131 | + echo; echo "=================================" |
| 132 | + echo "Access Kibana at: " ${kbn_url} |
| 133 | + echo "Username: " elastic |
| 134 | + echo "Password: " ${passwd} |
| 135 | + echo "================================="; echo |
| 136 | +} |
| 137 | + |
| 138 | +__clean() { |
| 139 | + echo "Y" | gcloud container clusters delete $cluster_name \ |
| 140 | + --zone "${region}-a" |
| 141 | +} |
| 142 | + |
| 143 | +__main() { |
| 144 | + if [ $# -eq 0 ] |
| 145 | + then |
| 146 | + __deploy_demo |
| 147 | + __status |
| 148 | + else |
| 149 | + case $1 in |
| 150 | + password|pwd|pw|p) |
| 151 | + __password |
| 152 | + ;; |
| 153 | + pwdreset|pwreset) |
| 154 | + __password_reset |
| 155 | + ;; |
| 156 | + status|s) |
| 157 | + __status |
| 158 | + ;; |
| 159 | + clean) |
| 160 | + __clean |
| 161 | + ;; |
| 162 | + *) |
| 163 | + __deploy_demo |
| 164 | + __status |
| 165 | + ;; |
| 166 | + esac |
| 167 | + fi |
| 168 | +} |
| 169 | + |
| 170 | +__main $@ |
0 commit comments