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.

178 lines
6.1 KiB

  1. # -*- mode: ruby -*-
  2. # # vi: set ft=ruby :
  3. require 'fileutils'
  4. Vagrant.require_version ">= 2.0.0"
  5. CONFIG = File.join(File.dirname(__FILE__), "vagrant/config.rb")
  6. COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json"
  7. # Uniq disk UUID for libvirt
  8. DISK_UUID = Time.now.utc.to_i
  9. SUPPORTED_OS = {
  10. "coreos-stable" => {box: "coreos-stable", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
  11. "coreos-alpha" => {box: "coreos-alpha", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
  12. "coreos-beta" => {box: "coreos-beta", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
  13. "ubuntu" => {box: "bento/ubuntu-16.04", bootstrap_os: "ubuntu", user: "vagrant"},
  14. "centos" => {box: "centos/7", bootstrap_os: "centos", user: "vagrant"},
  15. }
  16. # Defaults for config options defined in CONFIG
  17. $num_instances = 3
  18. $instance_name_prefix = "k8s"
  19. $vm_gui = false
  20. $vm_memory = 2048
  21. $vm_cpus = 1
  22. $shared_folders = {}
  23. $forwarded_ports = {}
  24. $subnet = "172.17.8"
  25. $os = "ubuntu"
  26. $network_plugin = "flannel"
  27. # The first three nodes are etcd servers
  28. $etcd_instances = $num_instances
  29. # The first two nodes are kube masters
  30. $kube_master_instances = $num_instances == 1 ? $num_instances : ($num_instances - 1)
  31. # All nodes are kube nodes
  32. $kube_node_instances = $num_instances
  33. # The following only works when using the libvirt provider
  34. $kube_node_instances_with_disks = false
  35. $kube_node_instances_with_disks_size = "20G"
  36. $kube_node_instances_with_disks_number = 2
  37. $local_release_dir = "/vagrant/temp"
  38. host_vars = {}
  39. if File.exist?(CONFIG)
  40. require CONFIG
  41. end
  42. $box = SUPPORTED_OS[$os][:box]
  43. # if $inventory is not set, try to use example
  44. $inventory = File.join(File.dirname(__FILE__), "inventory") if ! $inventory
  45. # if $inventory has a hosts file use it, otherwise copy over vars etc
  46. # to where vagrant expects dynamic inventory to be.
  47. if ! File.exist?(File.join(File.dirname($inventory), "hosts"))
  48. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
  49. "provisioners", "ansible")
  50. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  51. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  52. FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
  53. end
  54. end
  55. if Vagrant.has_plugin?("vagrant-proxyconf")
  56. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  57. (1..$num_instances).each do |i|
  58. $no_proxy += ",#{$subnet}.#{i+100}"
  59. end
  60. end
  61. Vagrant.configure("2") do |config|
  62. # always use Vagrants insecure key
  63. config.ssh.insert_key = false
  64. config.vm.box = $box
  65. if SUPPORTED_OS[$os].has_key? :box_url
  66. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  67. end
  68. config.ssh.username = SUPPORTED_OS[$os][:user]
  69. # plugin conflict
  70. if Vagrant.has_plugin?("vagrant-vbguest") then
  71. config.vbguest.auto_update = false
  72. end
  73. (1..$num_instances).each do |i|
  74. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  75. config.vm.hostname = vm_name
  76. if Vagrant.has_plugin?("vagrant-proxyconf")
  77. config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  78. config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  79. config.proxy.no_proxy = $no_proxy
  80. end
  81. if $expose_docker_tcp
  82. config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  83. end
  84. $forwarded_ports.each do |guest, host|
  85. config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  86. end
  87. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  88. config.vm.provider vmware do |v|
  89. v.vmx['memsize'] = $vm_memory
  90. v.vmx['numvcpus'] = $vm_cpus
  91. end
  92. end
  93. $shared_folders.each do |src, dst|
  94. config.vm.synced_folder src, dst
  95. end
  96. config.vm.provider :virtualbox do |vb|
  97. vb.gui = $vm_gui
  98. vb.memory = $vm_memory
  99. vb.cpus = $vm_cpus
  100. end
  101. config.vm.provider :libvirt do |lv|
  102. lv.memory = $vm_memory
  103. end
  104. ip = "#{$subnet}.#{i+100}"
  105. host_vars[vm_name] = {
  106. "ip": ip,
  107. "bootstrap_os": SUPPORTED_OS[$os][:bootstrap_os],
  108. "local_release_dir" => $local_release_dir,
  109. "download_run_once": "False",
  110. "kube_network_plugin": $network_plugin
  111. }
  112. config.vm.network :private_network, ip: ip
  113. # Disable swap for each vm
  114. config.vm.provision "shell", inline: "swapoff -a"
  115. if $kube_node_instances_with_disks
  116. # Libvirt
  117. driverletters = ('a'..'z').to_a
  118. config.vm.provider :libvirt do |lv|
  119. # always make /dev/sd{a/b/c} so that CI can ensure that
  120. # virtualbox and libvirt will have the same devices to use for OSDs
  121. (1..$kube_node_instances_with_disks_number).each do |d|
  122. lv.storage :file, :device => "hd#{driverletters[d]}", :path => "disk-#{i}-#{d}-#{DISK_UUID}.disk", :size => $kube_node_instances_with_disks_size, :bus => "ide"
  123. end
  124. end
  125. end
  126. # Only execute once the Ansible provisioner,
  127. # when all the machines are up and ready.
  128. if i == $num_instances
  129. config.vm.provision "ansible" do |ansible|
  130. ansible.playbook = "cluster.yml"
  131. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  132. ansible.inventory_path = $inventory
  133. end
  134. ansible.become = true
  135. ansible.limit = "all"
  136. ansible.host_key_checking = false
  137. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache"]
  138. ansible.host_vars = host_vars
  139. #ansible.tags = ['download']
  140. ansible.groups = {
  141. "etcd" => ["#{$instance_name_prefix}-0[1:#{$etcd_instances}]"],
  142. "kube-master" => ["#{$instance_name_prefix}-0[1:#{$kube_master_instances}]"],
  143. "kube-node" => ["#{$instance_name_prefix}-0[1:#{$kube_node_instances}]"],
  144. "k8s-cluster:children" => ["kube-master", "kube-node"],
  145. }
  146. end
  147. end
  148. end
  149. end
  150. end