-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
190 lines (150 loc) · 3.95 KB
/
main.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
# See https://cloud.google.com/compute/docs/load-balancing/network/example
provider "google" {
region = "${var.region}"
project = "${var.project_name}"
credentials = "${file("${var.credentials_file_path}")}"
}
resource "google_compute_network" "calico-net" {
name = "calico-network"
auto_create_subnetworks = "false"
}
resource "google_compute_subnetwork" "calico-subnet" {
name = "calico-subnet"
ip_cidr_range = "10.128.0.0/16"
network = "${google_compute_network.calico-net.self_link}"
region = "${var.region}"
}
data "template_file" "master_init" {
template = "${file("master-config.yaml")}"
vars {
ssh_authorized_key = "${file("${var.public_key_path}")}"
}
}
resource "google_compute_instance" "calico-master" {
count = 1
name = "calico-master-${count.index}"
machine_type = "n1-standard-4"
zone = "${var.region_zone}"
tags = ["calico","master"]
disk {
image = "ubuntu-os-cloud/ubuntu-1610-yakkety-v20170330"
# Find images by:
# gcloud config set project dummy-xxxxx
# gcloud auth login
# gcloud compute images list
}
network_interface {
subnetwork = "${google_compute_subnetwork.calico-subnet.name}"
access_config {
# Ephemeral
}
}
metadata {
#ssh-keys = "root:${file("${var.public_key_path}")}"
user-data = "${data.template_file.master_init.rendered}"
}
service_account {
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
}
provisioner "file" {
source = "install.sh"
destination = "/tmp/install.sh"
connection {
type = "ssh"
user = "ubuntu"
}
}
provisioner "remote-exec" {
connection {
type = "ssh"
user = "ubuntu"
}
inline = [
"chmod +x /tmp/install.sh",
"sudo /tmp/install.sh",
"sudo kubeadm init --token 2a9c0e.31e11f6489468e5e",
"mkdir /home/ubuntu/.kube",
"sudo cp /etc/kubernetes/admin.conf /home/ubuntu/.kube/config",
"sudo chown -R ubuntu /home/ubuntu/.kube"
]
}
}
data "template_file" "node_init" {
template = "${file("node-config.yaml")}"
vars {
master_ip = "${google_compute_instance.calico-master.network_interface.0.address}"
ssh_authorized_key = "${file("${var.public_key_path}")}"
}
}
resource "google_compute_instance" "calico" {
count = 2
name = "calico-${count.index}"
machine_type = "n1-standard-1"
zone = "${var.region_zone}"
tags = ["www-node"]
disk {
image = "ubuntu-os-cloud/ubuntu-1610-yakkety-v20170330"
}
network_interface {
subnetwork = "${google_compute_subnetwork.calico-subnet.name}"
access_config {
# Ephemeral
}
}
metadata {
user-data = "${data.template_file.node_init.rendered}"
}
service_account {
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
}
provisioner "file" {
source = "install.sh"
destination = "/tmp/install.sh"
connection {
type = "ssh"
user = "ubuntu"
}
}
provisioner "remote-exec" {
connection {
type = "ssh"
user = "ubuntu"
}
inline = [
"chmod +x /tmp/install.sh",
"sudo /tmp/install.sh",
"sudo kubeadm join --token 2a9c0e.31e11f6489468e5e ${google_compute_instance.calico-master.network_interface.0.address}:6443",
]
}
}
resource "google_compute_firewall" "all-source" {
name = "all-source"
network = "${google_compute_network.calico-net.name}"
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = [ "22", "3389" ]
}
}
resource "google_compute_firewall" "internal-rules" {
name = "internal-rules"
network = "${google_compute_network.calico-net.name}"
source_ranges = ["${google_compute_subnetwork.calico-subnet.ip_cidr_range}"]
allow {
protocol = "4"
}
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = [ "0-65535" ]
}
allow {
protocol = "udp"
ports = [ "0-65535" ]
}
}