You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
9.4 KiB

  1. variable user_data {
  2. type = "string"
  3. default = <<EOF
  4. #cloud-config
  5. manage_etc_hosts: localhost
  6. package_update: true
  7. package_upgrade: true
  8. EOF
  9. }
  10. resource "openstack_compute_keypair_v2" "k8s" {
  11. name = "kubernetes-${var.cluster_name}"
  12. public_key = "${chomp(file(var.public_key_path))}"
  13. }
  14. resource "openstack_compute_secgroup_v2" "k8s_master" {
  15. name = "${var.cluster_name}-k8s-master"
  16. description = "${var.cluster_name} - Kubernetes Master"
  17. rule {
  18. ip_protocol = "tcp"
  19. from_port = "6443"
  20. to_port = "6443"
  21. cidr = "0.0.0.0/0"
  22. }
  23. }
  24. resource "openstack_compute_secgroup_v2" "bastion" {
  25. name = "${var.cluster_name}-bastion"
  26. description = "${var.cluster_name} - Bastion Server"
  27. rule {
  28. ip_protocol = "tcp"
  29. from_port = "22"
  30. to_port = "22"
  31. cidr = "0.0.0.0/0"
  32. }
  33. }
  34. resource "openstack_compute_secgroup_v2" "k8s" {
  35. name = "${var.cluster_name}-k8s"
  36. description = "${var.cluster_name} - Kubernetes"
  37. rule {
  38. ip_protocol = "icmp"
  39. from_port = "-1"
  40. to_port = "-1"
  41. cidr = "0.0.0.0/0"
  42. }
  43. rule {
  44. ip_protocol = "tcp"
  45. from_port = "1"
  46. to_port = "65535"
  47. self = true
  48. }
  49. rule {
  50. ip_protocol = "udp"
  51. from_port = "1"
  52. to_port = "65535"
  53. self = true
  54. }
  55. rule {
  56. ip_protocol = "icmp"
  57. from_port = "-1"
  58. to_port = "-1"
  59. self = true
  60. }
  61. }
  62. resource "openstack_compute_instance_v2" "bastion" {
  63. name = "${var.cluster_name}-bastion-${count.index+1}"
  64. count = "${var.number_of_bastions}"
  65. image_name = "${var.image}"
  66. flavor_id = "${var.flavor_bastion}"
  67. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  68. network {
  69. name = "${var.network_name}"
  70. }
  71. security_groups = [ "${openstack_compute_secgroup_v2.k8s.name}",
  72. "${openstack_compute_secgroup_v2.bastion.name}",
  73. "default" ]
  74. metadata = {
  75. ssh_user = "${var.ssh_user}"
  76. kubespray_groups = "bastion"
  77. depends_on = "${var.network_id}"
  78. }
  79. provisioner "local-exec" {
  80. command = "sed s/USER/${var.ssh_user}/ contrib/terraform/openstack/ansible_bastion_template.txt | sed s/BASTION_ADDRESS/${var.bastion_fips[0]}/ > contrib/terraform/openstack/group_vars/no-floating.yml"
  81. }
  82. user_data = "${var.user_data}"
  83. }
  84. resource "openstack_compute_instance_v2" "k8s_master" {
  85. name = "${var.cluster_name}-k8s-master-${count.index+1}"
  86. count = "${var.number_of_k8s_masters}"
  87. image_name = "${var.image}"
  88. flavor_id = "${var.flavor_k8s_master}"
  89. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  90. network {
  91. name = "${var.network_name}"
  92. }
  93. security_groups = [ "${openstack_compute_secgroup_v2.k8s_master.name}",
  94. "${openstack_compute_secgroup_v2.bastion.name}",
  95. "${openstack_compute_secgroup_v2.k8s.name}",
  96. "default" ]
  97. metadata = {
  98. ssh_user = "${var.ssh_user}"
  99. kubespray_groups = "etcd,kube-master,kube-node,k8s-cluster,vault"
  100. depends_on = "${var.network_id}"
  101. }
  102. user_data = "${var.user_data}"
  103. }
  104. resource "openstack_compute_instance_v2" "k8s_master_no_etcd" {
  105. name = "${var.cluster_name}-k8s-master-ne-${count.index+1}"
  106. count = "${var.number_of_k8s_masters_no_etcd}"
  107. image_name = "${var.image}"
  108. flavor_id = "${var.flavor_k8s_master}"
  109. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  110. network {
  111. name = "${var.network_name}"
  112. }
  113. security_groups = [ "${openstack_compute_secgroup_v2.k8s_master.name}",
  114. "${openstack_compute_secgroup_v2.k8s.name}" ]
  115. metadata = {
  116. ssh_user = "${var.ssh_user}"
  117. kubespray_groups = "kube-master,kube-node,k8s-cluster,vault"
  118. depends_on = "${var.network_id}"
  119. }
  120. user_data = "${var.user_data}"
  121. }
  122. resource "openstack_compute_instance_v2" "etcd" {
  123. name = "${var.cluster_name}-etcd-${count.index+1}"
  124. count = "${var.number_of_etcd}"
  125. image_name = "${var.image}"
  126. flavor_id = "${var.flavor_etcd}"
  127. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  128. network {
  129. name = "${var.network_name}"
  130. }
  131. security_groups = [ "${openstack_compute_secgroup_v2.k8s.name}" ]
  132. metadata = {
  133. ssh_user = "${var.ssh_user}"
  134. kubespray_groups = "etcd,vault,no-floating"
  135. depends_on = "${var.network_id}"
  136. }
  137. user_data = "${var.user_data}"
  138. }
  139. resource "openstack_compute_instance_v2" "k8s_master_no_floating_ip" {
  140. name = "${var.cluster_name}-k8s-master-nf-${count.index+1}"
  141. count = "${var.number_of_k8s_masters_no_floating_ip}"
  142. image_name = "${var.image}"
  143. flavor_id = "${var.flavor_k8s_master}"
  144. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  145. network {
  146. name = "${var.network_name}"
  147. }
  148. security_groups = [ "${openstack_compute_secgroup_v2.k8s_master.name}",
  149. "${openstack_compute_secgroup_v2.k8s.name}",
  150. "default" ]
  151. metadata = {
  152. ssh_user = "${var.ssh_user}"
  153. kubespray_groups = "etcd,kube-master,kube-node,k8s-cluster,vault,no-floating"
  154. depends_on = "${var.network_id}"
  155. }
  156. user_data = "${var.user_data}"
  157. }
  158. resource "openstack_compute_instance_v2" "k8s_master_no_floating_ip_no_etcd" {
  159. name = "${var.cluster_name}-k8s-master-ne-nf-${count.index+1}"
  160. count = "${var.number_of_k8s_masters_no_floating_ip_no_etcd}"
  161. image_name = "${var.image}"
  162. flavor_id = "${var.flavor_k8s_master}"
  163. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  164. network {
  165. name = "${var.network_name}"
  166. }
  167. security_groups = [ "${openstack_compute_secgroup_v2.k8s_master.name}",
  168. "${openstack_compute_secgroup_v2.k8s.name}" ]
  169. metadata = {
  170. ssh_user = "${var.ssh_user}"
  171. kubespray_groups = "kube-master,kube-node,k8s-cluster,vault,no-floating"
  172. depends_on = "${var.network_id}"
  173. }
  174. user_data = "${var.user_data}"
  175. }
  176. resource "openstack_compute_instance_v2" "k8s_node" {
  177. name = "${var.cluster_name}-k8s-node-${count.index+1}"
  178. count = "${var.number_of_k8s_nodes}"
  179. image_name = "${var.image}"
  180. flavor_id = "${var.flavor_k8s_node}"
  181. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  182. network {
  183. name = "${var.network_name}"
  184. }
  185. security_groups = [ "${openstack_compute_secgroup_v2.k8s.name}",
  186. "${openstack_compute_secgroup_v2.bastion.name}",
  187. "default" ]
  188. metadata = {
  189. ssh_user = "${var.ssh_user}"
  190. kubespray_groups = "kube-node,k8s-cluster"
  191. depends_on = "${var.network_id}"
  192. }
  193. user_data = "${var.user_data}"
  194. }
  195. resource "openstack_compute_instance_v2" "k8s_node_no_floating_ip" {
  196. name = "${var.cluster_name}-k8s-node-nf-${count.index+1}"
  197. count = "${var.number_of_k8s_nodes_no_floating_ip}"
  198. image_name = "${var.image}"
  199. flavor_id = "${var.flavor_k8s_node}"
  200. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  201. network {
  202. name = "${var.network_name}"
  203. }
  204. security_groups = [ "${openstack_compute_secgroup_v2.k8s.name}",
  205. "default" ]
  206. metadata = {
  207. ssh_user = "${var.ssh_user}"
  208. kubespray_groups = "kube-node,k8s-cluster,no-floating"
  209. depends_on = "${var.network_id}"
  210. }
  211. user_data = "${var.user_data}"
  212. }
  213. resource "openstack_compute_floatingip_associate_v2" "bastion" {
  214. count = "${var.number_of_bastions}"
  215. floating_ip = "${var.bastion_fips[count.index]}"
  216. instance_id = "${element(openstack_compute_instance_v2.bastion.*.id, count.index)}"
  217. }
  218. resource "openstack_compute_floatingip_associate_v2" "k8s_master" {
  219. count = "${var.number_of_k8s_masters}"
  220. instance_id = "${element(openstack_compute_instance_v2.k8s_master.*.id, count.index)}"
  221. floating_ip = "${var.k8s_master_fips[count.index]}"
  222. }
  223. resource "openstack_compute_floatingip_associate_v2" "k8s_node" {
  224. count = "${var.number_of_k8s_nodes}"
  225. floating_ip = "${var.k8s_node_fips[count.index]}"
  226. instance_id = "${element(openstack_compute_instance_v2.k8s_node.*.id, count.index)}"
  227. }
  228. resource "openstack_blockstorage_volume_v2" "glusterfs_volume" {
  229. name = "${var.cluster_name}-glusterfs_volume-${count.index+1}"
  230. count = "${var.number_of_gfs_nodes_no_floating_ip}"
  231. description = "Non-ephemeral volume for GlusterFS"
  232. size = "${var.gfs_volume_size_in_gb}"
  233. }
  234. resource "openstack_compute_instance_v2" "glusterfs_node_no_floating_ip" {
  235. name = "${var.cluster_name}-gfs-node-nf-${count.index+1}"
  236. count = "${var.number_of_gfs_nodes_no_floating_ip}"
  237. image_name = "${var.image_gfs}"
  238. flavor_id = "${var.flavor_gfs_node}"
  239. key_pair = "${openstack_compute_keypair_v2.k8s.name}"
  240. network {
  241. name = "${var.network_name}"
  242. }
  243. security_groups = ["${openstack_compute_secgroup_v2.k8s.name}",
  244. "default" ]
  245. metadata = {
  246. ssh_user = "${var.ssh_user_gfs}"
  247. kubespray_groups = "gfs-cluster,network-storage,no-floating"
  248. depends_on = "${var.network_id}"
  249. }
  250. user_data = "#cloud-config\nmanage_etc_hosts: localhost\npackage_update: true\npackage_upgrade: true"
  251. }
  252. resource "openstack_compute_volume_attach_v2" "glusterfs_volume" {
  253. count = "${var.number_of_gfs_nodes_no_floating_ip}"
  254. instance_id = "${element(openstack_compute_instance_v2.glusterfs_node_no_floating_ip.*.id, count.index)}"
  255. volume_id = "${element(openstack_blockstorage_volume_v2.glusterfs_volume.*.id, count.index)}"
  256. }