main.tf 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_network" "net" {
  23. name = "${var.cluster_name}"
  24. mode = "nat"
  25. bridge = "${var.libvirt_network_if}"
  26. domain = "${var.base_domain}"
  27. addresses = [
  28. "${var.libvirt_ip_range}",
  29. ]
  30. dns = [{
  31. local_only = true
  32. srvs = ["${flatten(list(
  33. data.libvirt_network_dns_srv_template.etcd_cluster.*.rendered,
  34. ))}"]
  35. hosts = ["${flatten(list(
  36. data.libvirt_network_dns_host_template.bootstrap.*.rendered,
  37. data.libvirt_network_dns_host_template.masters.*.rendered,
  38. data.libvirt_network_dns_host_template.etcds.*.rendered,
  39. ))}"]
  40. }]
  41. autostart = true
  42. }
  43. data "template_file" "user_data" {
  44. template = "${file("${path.module}/user-data.tpl")}"
  45. vars {
  46. ssh_authorized_keys = "${var.ssh_key}"
  47. }
  48. }
  49. resource "libvirt_cloudinit_disk" "commoninit" {
  50. name = "${var.cluster_name}-master-init.iso"
  51. user_data = "${data.template_file.user_data.rendered}"
  52. }
  53. resource "libvirt_domain" "master" {
  54. count = "${var.master_count}"
  55. name = "${var.cluster_name}-master-${count.index}"
  56. memory = "${var.libvirt_master_memory}"
  57. vcpu = "${var.libvirt_master_vcpu}"
  58. cloudinit = "${libvirt_cloudinit_disk.commoninit.id}"
  59. disk {
  60. volume_id = "${element(libvirt_volume.master.*.id, count.index)}"
  61. }
  62. console {
  63. type = "pty"
  64. target_port = 0
  65. }
  66. network_interface {
  67. network_id = "${libvirt_network.net.id}"
  68. hostname = "${var.cluster_name}-master-${count.index}"
  69. addresses = ["${var.libvirt_master_ips[count.index]}"]
  70. }
  71. }
  72. data "libvirt_network_dns_host_template" "bootstrap" {
  73. count = "${var.bootstrap_dns ? 1 : 0}"
  74. ip = "${var.libvirt_bootstrap_ip}"
  75. hostname = "${var.cluster_name}-api"
  76. }
  77. data "libvirt_network_dns_host_template" "masters" {
  78. count = "${var.master_count}"
  79. ip = "${var.libvirt_master_ips[count.index]}"
  80. hostname = "${var.cluster_name}-api"
  81. }
  82. data "libvirt_network_dns_host_template" "etcds" {
  83. count = "${var.master_count}"
  84. ip = "${var.libvirt_master_ips[count.index]}"
  85. hostname = "${var.cluster_name}-etcd-${count.index}"
  86. }
  87. data "libvirt_network_dns_srv_template" "etcd_cluster" {
  88. count = "${var.master_count}"
  89. service = "etcd-server-ssl"
  90. protocol = "tcp"
  91. domain = "${var.cluster_name}.${var.base_domain}"
  92. port = 2380
  93. weight = 10
  94. target = "${var.cluster_name}-etcd-${count.index}.${var.base_domain}"
  95. }