-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
152 lines (134 loc) · 4.9 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
provider "openstack" {
user_name = "${var.user_name}"
tenant_name = "${var.tenant_name}"
password = "${var.password}"
auth_url = "${var.auth_url}"
}
resource "openstack_networking_network_v2" "kube" {
name = "kube"
region = "${var.region}"
admin_state_up = "true"
}
resource "openstack_compute_keypair_v2" "kube" {
name = "SSH keypair for kube instances"
region = "${var.region}"
public_key = "${file("${var.ssh_key_file}.pub")}"
}
resource "openstack_networking_subnet_v2" "kube" {
name = "kube"
region = "${var.region}"
network_id = "${openstack_networking_network_v2.kube.id}"
cidr = "${var.tenant_net_cidr}"
ip_version = 4
enable_dhcp = "true"
/*dns_nameservers = "${var.dns_nameservers}"*/
}
resource "openstack_networking_router_v2" "kube" {
name = "kube"
region = "${var.region}"
admin_state_up = "true"
external_gateway = "${var.external_gateway}"
}
resource "openstack_networking_router_interface_v2" "kube" {
region = "${var.region}"
router_id = "${openstack_networking_router_v2.kube.id}"
subnet_id = "${openstack_networking_subnet_v2.kube.id}"
}
resource "openstack_compute_floatingip_v2" "kube" {
depends_on = ["openstack_networking_router_interface_v2.kube"]
region = "${var.region}"
pool = "${var.pool}"
}
resource "openstack_compute_secgroup_v2" "kube" {
name = "kube"
region = "${var.region}"
description = "Security group for the kube instances"
rule {
from_port = 1
to_port = 65535
ip_protocol = "tcp"
cidr = "0.0.0.0/0"
}
rule {
from_port = 1
to_port = 65535
ip_protocol = "udp"
cidr = "0.0.0.0/0"
}
rule {
ip_protocol = "icmp"
from_port = "-1"
to_port = "-1"
cidr = "0.0.0.0/0"
}
}
resource "openstack_compute_instance_v2" "kube-master" {
name = "kube-master"
region = "${var.region}"
image_id = "${var.image_id}"
flavor_name = "${var.master_flavor}"
key_pair = "${openstack_compute_keypair_v2.kube.name}"
security_groups = ["${openstack_compute_secgroup_v2.kube.name}"]
floating_ip = "${openstack_compute_floatingip_v2.kube.address}"
network {
uuid = "${openstack_networking_network_v2.kube.id}"
}
provisioner "remote-exec" {
inline = [
"sudo sed -i -r 's/127.0.0.1 localhost/127.0.0.1 localhost kube-master/' /etc/hosts",
"curl -s -O https://packages.cloud.google.com/apt/doc/apt-key.gpg",
"sudo apt-key add ./apt-key.gpg",
"sudo add-apt-repository -y 'deb http://apt.kubernetes.io/ kubernetes-xenial main'",
"sudo apt-get -y -qq update",
"sudo apt-get -y -qq upgrade",
"sudo apt-get install -y -q apt-transport-https docker.io",
"sudo systemctl start docker.service",
"sudo apt-get -y -q update",
"sudo apt-get install -y -q htop kubelet kubeadm kubectl kubernetes-cni",
"sudo service kubelet restart",
"sudo kubeadm init --token ${var.kube_token} --kubernetes-version ${var.kube_version}",
"sudo cp -v /etc/kubernetes/admin.conf /home/ubuntu/config",
"sudo chown ubuntu /home/ubuntu/config",
"kubectl apply -f https://git.io/weave-kube",
]
connection {
user = "${var.ssh_user_name}"
private_key = "${file("${var.ssh_key_file}")}"
}
}
}
resource "openstack_compute_instance_v2" "kube-worker" {
name = "kube-worker-${count.index}"
count = "${var.worker_count}"
region = "${var.region}"
image_id = "${var.image_id}"
flavor_name = "${var.worker_flavor}"
key_pair = "${openstack_compute_keypair_v2.kube.name}"
security_groups = ["${openstack_compute_secgroup_v2.kube.name}"]
depends_on = ["openstack_compute_instance_v2.kube-master"]
network {
uuid = "${openstack_networking_network_v2.kube.id}"
}
provisioner "remote-exec" {
inline = [
"sudo sed -i -r 's/127.0.0.1 localhost/127.0.0.1 localhost kube-master/' /etc/hosts",
"curl -s -O https://packages.cloud.google.com/apt/doc/apt-key.gpg",
"sudo apt-key add apt-key.gpg",
"sudo add-apt-repository 'deb http://apt.kubernetes.io/ kubernetes-xenial main'",
"sudo apt-get -y -q update",
"sudo apt-get -y -q upgrade",
"sudo apt-get install -y -q apt-transport-https docker.io",
"sudo systemctl start docker.service",
"sudo apt-get -y -q update",
"sudo apt-get install -y -q htop kubelet kubeadm kubectl kubernetes-cni",
"sudo service kubelet restart",
"sudo kubeadm join --token ${var.kube_token} ${openstack_compute_instance_v2.kube-master.access_ip_v4}",
]
connection {
type = "ssh"
user = "${var.ssh_user_name}"
private_key = "${file("${var.ssh_key_file}")}"
bastion_host = "${openstack_compute_instance_v2.kube-master.access_ip_v4}"
}
}
}