main.tf 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. provider "libvirt" {
  2. uri = "${var.libvirt_uri}"
  3. }
  4. module "volume" {
  5. source = "./volume"
  6. cluster_name = "${var.cluster_name}"
  7. image = "${var.os_image}"
  8. }
  9. module "bootstrap" {
  10. source = "./bootstrap"
  11. addresses = ["${var.libvirt_bootstrap_ip}"]
  12. base_volume_id = "${module.volume.coreos_base_volume_id}"
  13. cluster_name = "${var.cluster_name}"
  14. network_id = "${libvirt_network.net.id}"
  15. ssh_key = "${var.ssh_key}"
  16. }
  17. resource "libvirt_volume" "master" {
  18. count = "${var.master_count}"
  19. name = "${var.cluster_name}-master-${count.index}"
  20. base_volume_id = "${module.volume.coreos_base_volume_id}"
  21. }
  22. resource "libvirt_volume" "worker" {
  23. count = "${var.worker_count}"
  24. name = "${var.cluster_name}-worker-${count.index}"
  25. base_volume_id = "${module.volume.coreos_base_volume_id}"
  26. }
  27. resource "libvirt_network" "net" {
  28. name = "${var.cluster_name}"
  29. mode = "nat"
  30. bridge = "${var.libvirt_network_if}"
  31. domain = "${var.base_domain}"
  32. addresses = [
  33. "${var.libvirt_ip_range}",
  34. ]
  35. dns = [{
  36. local_only = true
  37. srvs = ["${flatten(list(
  38. data.libvirt_network_dns_srv_template.etcd_cluster.*.rendered,
  39. ))}"]
  40. hosts = ["${flatten(list(
  41. data.libvirt_network_dns_host_template.bootstrap.*.rendered,
  42. data.libvirt_network_dns_host_template.masters.*.rendered,
  43. data.libvirt_network_dns_host_template.etcds.*.rendered,
  44. ))}"]
  45. }]
  46. autostart = true
  47. }
  48. data "template_file" "user_data" {
  49. template = "${file("${path.module}/user-data.tpl")}"
  50. vars {
  51. ssh_authorized_keys = "${var.ssh_key}"
  52. }
  53. }
  54. resource "libvirt_cloudinit_disk" "commoninit" {
  55. name = "${var.cluster_name}-master-init.iso"
  56. user_data = "${data.template_file.user_data.rendered}"
  57. }
  58. resource "libvirt_domain" "master" {
  59. count = "${var.master_count}"
  60. name = "${var.cluster_name}-master-${count.index}"
  61. memory = "${var.libvirt_master_memory}"
  62. vcpu = "${var.libvirt_master_vcpu}"
  63. cloudinit = "${libvirt_cloudinit_disk.commoninit.id}"
  64. disk {
  65. volume_id = "${element(libvirt_volume.master.*.id, count.index)}"
  66. }
  67. console {
  68. type = "pty"
  69. target_port = 0
  70. }
  71. network_interface {
  72. network_id = "${libvirt_network.net.id}"
  73. hostname = "${var.cluster_name}-master-${count.index}"
  74. addresses = ["${var.libvirt_master_ips[count.index]}"]
  75. }
  76. }
  77. resource "libvirt_domain" "worker" {
  78. count = "${var.worker_count}"
  79. name = "${var.cluster_name}-worker-${count.index}"
  80. memory = "${var.libvirt_worker_memory}"
  81. vcpu = "${var.libvirt_worker_vcpu}"
  82. cloudinit = "${libvirt_cloudinit_disk.commoninit.id}"
  83. disk {
  84. volume_id = "${element(libvirt_volume.worker.*.id, count.index)}"
  85. }
  86. console {
  87. type = "pty"
  88. target_port = 0
  89. }
  90. network_interface {
  91. network_id = "${libvirt_network.net.id}"
  92. hostname = "${var.cluster_name}-worker-${count.index}"
  93. addresses = ["${var.libvirt_worker_ips[count.index]}"]
  94. }
  95. }
  96. data "libvirt_network_dns_host_template" "bootstrap" {
  97. count = "${var.bootstrap_dns ? 1 : 0}"
  98. ip = "${var.libvirt_bootstrap_ip}"
  99. hostname = "${var.cluster_name}-api"
  100. }
  101. data "libvirt_network_dns_host_template" "masters" {
  102. count = "${var.master_count}"
  103. ip = "${var.libvirt_master_ips[count.index]}"
  104. hostname = "${var.cluster_name}-api"
  105. }
  106. data "libvirt_network_dns_host_template" "etcds" {
  107. count = "${var.master_count}"
  108. ip = "${var.libvirt_master_ips[count.index]}"
  109. hostname = "${var.cluster_name}-etcd-${count.index}"
  110. }
  111. data "libvirt_network_dns_srv_template" "etcd_cluster" {
  112. count = "${var.master_count}"
  113. service = "etcd-server-ssl"
  114. protocol = "tcp"
  115. domain = "${var.cluster_name}.${var.base_domain}"
  116. port = 2380
  117. weight = 10
  118. target = "${var.cluster_name}-etcd-${count.index}.${var.base_domain}"
  119. }