-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetcd.tf
172 lines (135 loc) · 4.11 KB
/
etcd.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
// IAM and Service Account
resource "google_service_account" "etcd" {
account_id = "etcd-${var.cluster_name}"
display_name = "service account for etcd instances"
}
// Volumes
resource "google_compute_disk" "etcd-data" {
count = var.etcd_instance_count
name = "etcd-data-${count.index}-${var.cluster_name}"
zone = var.available_zones[count.index]
size = var.etcd_data_volume_size
type = "pd-ssd"
// The value can only contain lowercase letters, numeric characters, underscores and dashes
labels = {
name = "etcd-${var.cluster_name}-data-vol-${count.index}"
component = "${var.cluster_name}-etcd"
cluster = var.cluster_name
}
lifecycle {
ignore_changes = [snapshot]
}
}
resource "null_resource" "etcd_data_volume_ids" {
count = var.etcd_instance_count
triggers = {
volume_id = "etcd_data_${count.index}"
}
}
resource "random_string" "r" {
count = var.etcd_instance_count
length = 4
special = false
upper = false
keepers = {
userdata = var.etcd_user_data[count.index]
}
}
// reserve Ip addresses
resource "google_compute_address" "etcd_addresses" {
count = var.etcd_instance_count
name = "etcd-address-${count.index}-${var.cluster_name}"
address_type = "INTERNAL"
subnetwork = var.subnetwork_link
address = var.etcd_addresses[count.index]
}
// Instances
resource "google_compute_instance" "etcd" {
count = var.etcd_instance_count
name = "etcd-${count.index}-${var.cluster_name}-${random_string.r[count.index].result}"
description = "etcd cluster member"
machine_type = var.etcd_machine_type
zone = var.available_zones[count.index]
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = var.container_linux_image
}
auto_delete = true
}
lifecycle {
ignore_changes = [boot_disk.0.initialize_params.0.image]
}
attached_disk {
source = google_compute_disk.etcd-data[count.index].self_link
mode = "READ_WRITE"
device_name = null_resource.etcd_data_volume_ids.*.triggers.volume_id[count.index]
}
network_interface {
subnetwork = var.subnetwork_link
network_ip = google_compute_address.etcd_addresses[count.index].address
}
tags = concat(["etcd-${var.cluster_name}"], var.cluster_instance_tags)
labels = {
cluster = var.cluster_name
name = "${var.cluster_name}-etcd"
}
metadata = {
user-data = var.etcd_user_data[count.index]
}
service_account {
email = google_service_account.etcd.email
scopes = []
}
}
// Firewall Rules
resource "google_compute_firewall" "allow-etcds-to-talk" {
name = "allow-etcds-to-talk-${var.cluster_name}"
network = var.network_link
allow {
protocol = "tcp"
ports = ["2379", "2380"]
}
source_tags = ["etcd-${var.cluster_name}"]
direction = "INGRESS"
target_tags = ["etcd-${var.cluster_name}"]
}
resource "google_compute_firewall" "allow-masters-to-etcds" {
name = "allow-masters-to-etcds-${var.cluster_name}"
network = var.network_link
allow {
protocol = "tcp"
ports = ["2379", "2380"]
}
source_tags = ["master-${var.cluster_name}"]
direction = "INGRESS"
target_tags = ["etcd-${var.cluster_name}"]
}
resource "google_compute_firewall" "allow-workerss-to-etcds" {
name = "allow-workers-to-etcds-${var.cluster_name}"
network = var.network_link
// promtail, node exporter and etcd metrics
allow {
protocol = "tcp"
ports = ["9080", "9100", "9378"]
}
source_tags = ["worker-${var.cluster_name}"]
direction = "INGRESS"
target_tags = ["etcd-${var.cluster_name}"]
}
// dns
resource "google_dns_record_set" "etcd-by-instance" {
count = var.etcd_instance_count
name = "${count.index}.etcd.${var.dns_domain}."
type = "A"
ttl = 30
managed_zone = var.dns_zone
rrdatas = [google_compute_instance.etcd.*.network_interface.0.network_ip[count.index]]
}
resource "google_dns_record_set" "etcd-all" {
name = "etcd.${var.dns_domain}."
type = "A"
ttl = 30
managed_zone = var.dns_zone
rrdatas = google_compute_instance.etcd.*.network_interface.0.network_ip
}