-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinfra.sh
executable file
·141 lines (131 loc) · 3.91 KB
/
infra.sh
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
134
135
136
137
138
139
140
141
#!/bin/bash
#
# Create and delete GCP infrastructure.
#
# Usage:
#
# infra.sh up|down [network|ldap|authn|k8s]...
#
#------------------------------------------------------------------------------#
action=$1
shift
components=${*:-network ldap authn k8s}
usage() {
cat <<EOF
USAGE
$(basename $0) up|down [network|ldap|authn|k8s]...
NOTE
If only a single argument is provided (up or down), then all components
are assumed (network, ldap, authn, and k8s).
EXAMPLES
# Spin up the network and LDAP infrastructure
$(basename $0) up network ldap
# Delete all infrastructure
$(basename $0) down
EOF
}
# Network infrastructure for the other components
network-up() {
gcloud compute networks create my-net --subnet-mode custom
gcloud compute networks subnets create my-subnet --network my-net --range 10.0.0.0/16
}
network-down() {
gcloud compute networks subnets delete my-subnet
gcloud compute networks delete my-net
}
# Infrastructure for the LDAP server
ldap-up() {
gcloud compute instances create ldap \
--subnet my-subnet \
--machine-type n1-standard-1 \
--image-family ubuntu-1804-lts \
--image-project ubuntu-os-cloud \
--tags ldap
# Allow LDAP traffic from other instances in the subnet
gcloud compute firewall-rules create ldap-internal \
--network my-net \
--target-tags ldap \
--allow tcp:389 \
--source-ranges 10.0.0.0/16
# Allow SSH and LDAP traffic from everywhere (for configuration and testing)
gcloud compute firewall-rules create ldap-admin \
--network my-net \
--target-tags ldap \
--allow tcp:22,tcp:389
}
ldap-down() {
gcloud compute instances delete ldap
gcloud compute firewall-rules delete ldap-internal ldap-admin
}
# Infrastructure for the authentication service
authn-up() {
gcloud compute instances create authn \
--subnet my-subnet \
--machine-type e2-small \
--image-family ubuntu-1804-lts \
--image-project ubuntu-os-cloud \
--tags authn
# Allow HTTPS traffic from the Kubernetes cluster nodes
gcloud compute firewall-rules create authn-internal \
--network my-net \
--target-tags authn \
--allow tcp:443 \
--source-tags k8s
# Allow SSH and HTTPS traffic from everwhere (for configuration and testing)
gcloud compute firewall-rules create authn-admin \
--network my-net \
--target-tags authn \
--allow tcp:22,tcp:443
}
authn-down() {
gcloud compute instances delete authn
gcloud compute firewall-rules delete authn-internal authn-admin
}
# Infrastructure for the Kubernetes cluster
k8s-up() {
gcloud compute instances create k8s-master k8s-worker-1 k8s-worker-2 \
--subnet my-subnet \
--machine-type e2-medium \
--image-family ubuntu-1804-lts \
--image-project ubuntu-os-cloud \
--tags k8s
# Allow all traffic from other cluster nodes
gcloud compute firewall-rules create k8s-internal \
--network my-net \
--target-tags k8s \
--allow tcp,udp,icmp \
--source-tags k8s
# Allow TCP, etcd, ICMP traffic from everywhere (for installation)
gcloud compute firewall-rules create k8s-install \
--network my-net \
--target-tags k8s \
--allow tcp:22,tcp:2379,icmp
# Allow Kubernetes API server traffic from everywhere
gcloud compute firewall-rules create k8s-access \
--network my-net \
--target-tags k8s \
--allow tcp:6443
}
k8s-down() {
gcloud compute instances delete k8s-master k8s-worker-1 k8s-worker-2
gcloud compute firewall-rules delete k8s-internal k8s-install k8s-access
}
# Entry point
case "$action" in
up)
set -e
[[ "$components" =~ network ]] && network-up
[[ "$components" =~ ldap ]] && ldap-up
[[ "$components" =~ authn ]] && authn-up
[[ "$components" =~ k8s ]] && k8s-up
;;
down)
[[ "$components" =~ k8s ]] && k8s-down
[[ "$components" =~ authn ]] && authn-down
[[ "$components" =~ ldap ]] && ldap-down
[[ "$components" =~ network ]] && network-down
;;
*)
usage && exit 1
;;
esac