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.

211 lines
7.5 KiB

  1. # -*- mode: ruby -*-
  2. # # vi: set ft=ruby :
  3. # For help on using kubespray with vagrant, check out docs/vagrant.md
  4. require 'fileutils'
  5. Vagrant.require_version ">= 2.0.0"
  6. CONFIG = File.join(File.dirname(__FILE__), "vagrant/config.rb")
  7. COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json"
  8. # Uniq disk UUID for libvirt
  9. DISK_UUID = Time.now.utc.to_i
  10. SUPPORTED_OS = {
  11. "coreos-stable" => {box: "coreos-stable", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
  12. "coreos-alpha" => {box: "coreos-alpha", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
  13. "coreos-beta" => {box: "coreos-beta", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
  14. "ubuntu1604" => {box: "generic/ubuntu1604", user: "vagrant"},
  15. "ubuntu1804" => {box: "generic/ubuntu1804", user: "vagrant"},
  16. "centos" => {box: "centos/7", user: "vagrant"},
  17. "centos-bento" => {box: "bento/centos-7.5", user: "vagrant"},
  18. "fedora" => {box: "fedora/28-cloud-base", user: "vagrant"},
  19. "opensuse" => {box: "opensuse/openSUSE-42.3-x86_64", user: "vagrant"},
  20. "opensuse-tumbleweed" => {box: "opensuse/openSUSE-Tumbleweed-x86_64", user: "vagrant"},
  21. }
  22. # Defaults for config options defined in CONFIG
  23. $num_instances = 3
  24. $instance_name_prefix = "k8s"
  25. $vm_gui = false
  26. $vm_memory = 2048
  27. $vm_cpus = 1
  28. $shared_folders = {}
  29. $forwarded_ports = {}
  30. $subnet = "172.17.8"
  31. $os = "ubuntu1804"
  32. $network_plugin = "flannel"
  33. # Setting multi_networking to true will install Multus: https://github.com/intel/multus-cni
  34. $multi_networking = false
  35. # The first three nodes are etcd servers
  36. $etcd_instances = $num_instances
  37. # The first two nodes are kube masters
  38. $kube_master_instances = $num_instances == 1 ? $num_instances : ($num_instances - 1)
  39. # All nodes are kube nodes
  40. $kube_node_instances = $num_instances
  41. # The following only works when using the libvirt provider
  42. $kube_node_instances_with_disks = false
  43. $kube_node_instances_with_disks_size = "20G"
  44. $kube_node_instances_with_disks_number = 2
  45. $override_disk_size = false
  46. $disk_size = "20GB"
  47. $playbook = "cluster.yml"
  48. host_vars = {}
  49. if File.exist?(CONFIG)
  50. require CONFIG
  51. end
  52. $box = SUPPORTED_OS[$os][:box]
  53. # if $inventory is not set, try to use example
  54. $inventory = "inventory/sample" if ! $inventory
  55. $inventory = File.absolute_path($inventory, File.dirname(__FILE__))
  56. # if $inventory has a hosts.ini file use it, otherwise copy over
  57. # vars etc to where vagrant expects dynamic inventory to be
  58. if ! File.exist?(File.join(File.dirname($inventory), "hosts.ini"))
  59. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", "provisioners", "ansible")
  60. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  61. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  62. FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
  63. end
  64. end
  65. if Vagrant.has_plugin?("vagrant-proxyconf")
  66. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  67. (1..$num_instances).each do |i|
  68. $no_proxy += ",#{$subnet}.#{i+100}"
  69. end
  70. end
  71. Vagrant.configure("2") do |config|
  72. config.vm.box = $box
  73. if SUPPORTED_OS[$os].has_key? :box_url
  74. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  75. end
  76. config.ssh.username = SUPPORTED_OS[$os][:user]
  77. # plugin conflict
  78. if Vagrant.has_plugin?("vagrant-vbguest") then
  79. config.vbguest.auto_update = false
  80. end
  81. # always use Vagrants insecure key
  82. config.ssh.insert_key = false
  83. if ($override_disk_size)
  84. unless Vagrant.has_plugin?("vagrant-disksize")
  85. system "vagrant plugin install vagrant-disksize"
  86. end
  87. config.disksize.size = $disk_size
  88. end
  89. (1..$num_instances).each do |i|
  90. config.vm.define vm_name = "%s-%01d" % [$instance_name_prefix, i] do |node|
  91. node.vm.hostname = vm_name
  92. if Vagrant.has_plugin?("vagrant-proxyconf")
  93. node.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  94. node.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  95. node.proxy.no_proxy = $no_proxy
  96. end
  97. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  98. node.vm.provider vmware do |v|
  99. v.vmx['memsize'] = $vm_memory
  100. v.vmx['numvcpus'] = $vm_cpus
  101. end
  102. end
  103. node.vm.provider :virtualbox do |vb|
  104. vb.memory = $vm_memory
  105. vb.cpus = $vm_cpus
  106. vb.gui = $vm_gui
  107. vb.linked_clone = true
  108. vb.customize ["modifyvm", :id, "--vram", "8"] # ubuntu defaults to 256 MB which is a waste of precious RAM
  109. end
  110. node.vm.provider :libvirt do |lv|
  111. lv.memory = $vm_memory
  112. lv.cpus = $vm_cpus
  113. lv.default_prefix = 'kubespray'
  114. # Fix kernel panic on fedora 28
  115. if $os == "fedora"
  116. lv.cpu_mode = "host-passthrough"
  117. end
  118. end
  119. if $kube_node_instances_with_disks
  120. # Libvirt
  121. driverletters = ('a'..'z').to_a
  122. node.vm.provider :libvirt do |lv|
  123. # always make /dev/sd{a/b/c} so that CI can ensure that
  124. # virtualbox and libvirt will have the same devices to use for OSDs
  125. (1..$kube_node_instances_with_disks_number).each do |d|
  126. lv.storage :file, :device => "hd#{driverletters[d]}", :path => "disk-#{i}-#{d}-#{DISK_UUID}.disk", :size => $kube_node_instances_with_disks_size, :bus => "ide"
  127. end
  128. end
  129. end
  130. if $expose_docker_tcp
  131. node.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  132. end
  133. $forwarded_ports.each do |guest, host|
  134. node.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  135. end
  136. node.vm.synced_folder ".", "/vagrant", disabled: false, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] , rsync__exclude: ['.git','venv']
  137. $shared_folders.each do |src, dst|
  138. node.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
  139. end
  140. ip = "#{$subnet}.#{i+100}"
  141. node.vm.network :private_network, ip: ip
  142. # Disable swap for each vm
  143. node.vm.provision "shell", inline: "swapoff -a"
  144. host_vars[vm_name] = {
  145. "ip": ip,
  146. "flannel_interface": "eth1",
  147. "kube_network_plugin": $network_plugin,
  148. "kube_network_plugin_multus": $multi_networking,
  149. "docker_keepcache": "1",
  150. "download_run_once": "True",
  151. "download_localhost": "False"
  152. }
  153. # Only execute the Ansible provisioner once, when all the machines are up and ready.
  154. if i == $num_instances
  155. node.vm.provision "ansible" do |ansible|
  156. ansible.playbook = $playbook
  157. $ansible_inventory_path = File.join( $inventory, "hosts.ini")
  158. if File.exist?($ansible_inventory_path)
  159. ansible.inventory_path = $ansible_inventory_path
  160. end
  161. ansible.become = true
  162. ansible.limit = "all"
  163. ansible.host_key_checking = false
  164. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache", "--ask-become-pass"]
  165. ansible.host_vars = host_vars
  166. #ansible.tags = ['download']
  167. ansible.groups = {
  168. "etcd" => ["#{$instance_name_prefix}-[1:#{$etcd_instances}]"],
  169. "kube-master" => ["#{$instance_name_prefix}-[1:#{$kube_master_instances}]"],
  170. "kube-node" => ["#{$instance_name_prefix}-[1:#{$kube_node_instances}]"],
  171. "k8s-cluster:children" => ["kube-master", "kube-node"],
  172. }
  173. end
  174. end
  175. end
  176. end
  177. end