-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.tf
228 lines (194 loc) · 6.67 KB
/
deploy.tf
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
###############################################################################
#
# A simple K8s cluster in DO
#
###############################################################################
###############################################################################
#
# Get variables from command line or environment
#
###############################################################################
variable "do_token" {}
variable "do_region" {
default = "nyc3"
}
variable "ssh_fingerprint" {}
variable "ssh_private_key" {
default = "~/.ssh/id_rsa"
}
variable "number_of_workers" {
default = "3"
}
variable "k8s_version" {
default = "v1.10.3"
}
variable "cni_version" {
default = "v0.6.0"
}
variable "prefix" {
default = ""
}
variable "size_master" {
default = "2gb"
}
variable "size_worker" {
default = "2gb"
}
###############################################################################
#
# Specify provider
#
###############################################################################
provider "digitalocean" {
token = "${var.do_token}"
}
###############################################################################
#
# Master host
#
###############################################################################
resource "digitalocean_droplet" "k8s_master" {
image = "coreos-stable"
name = "${var.prefix}k8s-master"
region = "${var.do_region}"
private_networking = true
size = "${var.size_master}"
ssh_keys = ["${split(",", var.ssh_fingerprint)}"]
provisioner "file" {
source = "./00-master.sh"
destination = "/tmp/00-master.sh"
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
provisioner "file" {
source = "./install-kubeadm.sh"
destination = "/tmp/install-kubeadm.sh"
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
# Install dependencies and set up cluster
provisioner "remote-exec" {
inline = [
"export K8S_VERSION=\"${var.k8s_version}\"",
"export CNI_VERSION=\"${var.cni_version}\"",
"chmod +x /tmp/install-kubeadm.sh",
"sudo -E /tmp/install-kubeadm.sh",
"export MASTER_PRIVATE_IP=\"${self.ipv4_address_private}\"",
"export MASTER_PUBLIC_IP=\"${self.ipv4_address}\"",
"chmod +x /tmp/00-master.sh",
"sudo -E /tmp/00-master.sh"
]
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
# copy secrets to local
provisioner "local-exec" {
command =<<EOF
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${var.ssh_private_key} core@${digitalocean_droplet.k8s_master.ipv4_address}:"/tmp/kubeadm_join /etc/kubernetes/admin.conf" ${path.module}/secrets
mv "${path.module}/secrets/admin.conf" "${path.module}/secrets/admin.conf.bak"
sed -e "s/${self.ipv4_address_private}/${self.ipv4_address}/" "${path.module}/secrets/admin.conf.bak" > "${path.module}/secrets/admin.conf"
EOF
}
}
###############################################################################
#
# Worker hosts
#
###############################################################################
resource "digitalocean_droplet" "k8s_worker" {
count = "${var.number_of_workers}"
image = "coreos-stable"
name = "${var.prefix}${format("k8s-worker-%02d", count.index + 1)}"
region = "${var.do_region}"
size = "${var.size_worker}"
private_networking = true
# user_data = "${data.template_file.worker_yaml.rendered}"
ssh_keys = ["${split(",", var.ssh_fingerprint)}"]
depends_on = ["digitalocean_droplet.k8s_master"]
# Start kubelet
provisioner "file" {
source = "./01-worker.sh"
destination = "/tmp/01-worker.sh"
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
provisioner "file" {
source = "./install-kubeadm.sh"
destination = "/tmp/install-kubeadm.sh"
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
provisioner "file" {
source = "./secrets/kubeadm_join"
destination = "/tmp/kubeadm_join"
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
# Install dependencies and join cluster
provisioner "remote-exec" {
inline = [
"export K8S_VERSION=\"${var.k8s_version}\"",
"export CNI_VERSION=\"${var.cni_version}\"",
"chmod +x /tmp/install-kubeadm.sh",
"sudo -E /tmp/install-kubeadm.sh",
"export NODE_PRIVATE_IP=\"${self.ipv4_address_private}\"",
"chmod +x /tmp/01-worker.sh",
"sudo -E /tmp/01-worker.sh"
]
connection {
type = "ssh",
user = "core",
private_key = "${file(var.ssh_private_key)}"
}
}
provisioner "local-exec" {
when = "destroy"
command = <<EOF
export KUBECONFIG=${path.module}/secrets/admin.conf
kubectl drain --delete-local-data --force --ignore-daemonsets ${self.name}
kubectl delete nodes/${self.name}
EOF
}
}
# use kubeconfig retrieved from master
resource "null_resource" "deploy_microbot" {
depends_on = ["digitalocean_droplet.k8s_worker"]
provisioner "local-exec" {
command = <<EOF
export KUBECONFIG=${path.module}/secrets/admin.conf
sed -e "s/\$EXT_IP1/${digitalocean_droplet.k8s_worker.0.ipv4_address}/" < ${path.module}/02-microbot.yaml > ./secrets/02-microbot.rendered.yaml
until kubectl get pods 2>/dev/null; do printf '.'; sleep 5; done
kubectl create -f ./secrets/02-microbot.rendered.yaml
EOF
}
}
resource "null_resource" "deploy_digitalocean_cloud_controller_manager" {
depends_on = ["digitalocean_droplet.k8s_worker"]
provisioner "local-exec" {
command = <<EOF
export KUBECONFIG=${path.module}/secrets/admin.conf
sed -e "s/\$DO_ACCESS_TOKEN/${var.do_token}/" < ${path.module}/03-do-secret.yaml > ./secrets/03-do-secret.rendered.yaml
until kubectl get pods 2>/dev/null; do printf '.'; sleep 5; done
kubectl create -f ./secrets/03-do-secret.rendered.yaml
kubectl create -f https://raw.githubusercontent.com/digitalocean/digitalocean-cloud-controller-manager/master/releases/v0.1.3.yml
EOF
}
}