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.

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