forked from lavishsheth/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create an Internal Load Balancer
133 lines (104 loc) · 4.53 KB
/
Create an Internal Load Balancer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
export REGION=
export ZONE_1=
export ZONE_2=
gcloud compute firewall-rules create app-allow-http \
--network my-internal-app \
--target-tags lb-backend \
--source-ranges 0.0.0.0/0 \
--allow tcp:80
gcloud compute --project=$DEVSHELL_PROJECT_ID firewall-rules create app-allow-health-check --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=lb-backend
gcloud compute instance-templates create instance-template-1 \
--machine-type e2-micro \
--network my-internal-app \
--subnet subnet-a \
--tags lb-backend \
--metadata startup-script-url=gs://cloud-training/gcpnet/ilb/startup.sh \
--region=$REGION
gcloud compute instance-templates create instance-template-2 \
--machine-type e2-micro \
--network my-internal-app \
--subnet subnet-b \
--tags lb-backend \
--metadata startup-script-url=gs://cloud-training/gcpnet/ilb/startup.sh \
--region=$REGION
gcloud beta compute instance-groups managed create instance-group-1 --project=$DEVSHELL_PROJECT_ID --base-instance-name=instance-group-1 --size=1 --template=instance-template-1 --zone=$ZONE_1 --list-managed-instances-results=PAGELESS --no-force-update-on-repair && gcloud beta compute instance-groups managed set-autoscaling instance-group-1 --project=$DEVSHELL_PROJECT_ID --zone=$ZONE_1 --cool-down-period=45 --max-num-replicas=5 --min-num-replicas=1 --mode=on --target-cpu-utilization=0.8
gcloud beta compute instance-groups managed create instance-group-2 --project=$DEVSHELL_PROJECT_ID --base-instance-name=instance-group-2 --size=1 --template=instance-template-2 --zone=$ZONE_2 --list-managed-instances-results=PAGELESS --no-force-update-on-repair && gcloud beta compute instance-groups managed set-autoscaling instance-group-2 --project=$DEVSHELL_PROJECT_ID --zone=$ZONE_2 --cool-down-period=45 --max-num-replicas=5 --min-num-replicas=1 --mode=on --target-cpu-utilization=0.8
gcloud compute instances create utility-vm \
--zone $ZONE_1 \
--machine-type e2-micro \
--network my-internal-app \
--subnet subnet-a \
--private-network-ip 10.10.20.50
TOKEN=$(gcloud auth application-default print-access-token)
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"checkIntervalSec": 5,
"description": "",
"healthyThreshold": 2,
"name": "my-ilb-health-check",
"region": "projects/'"$DEVSHELL_PROJECT_ID"'/regions/'"$REGION"'",
"tcpHealthCheck": {
"port": 80,
"proxyHeader": "NONE"
},
"timeoutSec": 5,
"type": "TCP",
"unhealthyThreshold": 2
}' \
"https://compute.googleapis.com/compute/beta/projects/$DEVSHELL_PROJECT_ID/regions/$REGION/healthChecks"
sleep 30
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"backends": [
{
"balancingMode": "CONNECTION",
"failover": false,
"group": "projects/'"$DEVSHELL_PROJECT_ID"'/zones/'"$ZONE_1"'/instanceGroups/instance-group-1"
},
{
"balancingMode": "CONNECTION",
"failover": false,
"group": "projects/'"$DEVSHELL_PROJECT_ID"'/zones/'"$ZONE_2"'/instanceGroups/instance-group-2"
}
],
"connectionDraining": {
"drainingTimeoutSec": 300
},
"description": "",
"failoverPolicy": {},
"healthChecks": [
"projects/'"$DEVSHELL_PROJECT_ID"'/regions/'"$REGION"'/healthChecks/my-ilb-health-check"
],
"loadBalancingScheme": "INTERNAL",
"logConfig": {
"enable": false
},
"name": "my-ilb",
"network": "projects/'"$DEVSHELL_PROJECT_ID"'/global/networks/my-internal-app",
"protocol": "TCP",
"region": "projects/'"$DEVSHELL_PROJECT_ID"'/regions/'"$REGION"'",
"sessionAffinity": "NONE"
}' \
"https://compute.googleapis.com/compute/v1/projects/$DEVSHELL_PROJECT_ID/regions/$REGION/backendServices"
sleep 20
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"IPAddress": "10.10.30.5",
"IPProtocol": "TCP",
"allowGlobalAccess": false,
"backendService": "projects/'"$DEVSHELL_PROJECT_ID"'/regions/'"$REGION"'/backendServices/my-ilb",
"description": "",
"ipVersion": "IPV4",
"loadBalancingScheme": "INTERNAL",
"name": "my-ilb-forwarding-rule",
"networkTier": "PREMIUM",
"ports": [
"80"
],
"region": "projects/'"$DEVSHELL_PROJECT_ID"'/regions/'"$REGION"'",
"subnetwork": "projects/'"$DEVSHELL_PROJECT_ID"'/regions/'"$REGION"'/subnetworks/subnet-b"
}' \
"https://compute.googleapis.com/compute/v1/projects/$DEVSHELL_PROJECT_ID/regions/$REGION/forwardingRules"