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.

215 lines
7.8 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-15.0-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. $local_path_provisioner_enabled = false
  48. $local_path_provisioner_claim_root = "/opt/local-path-provisioner/"
  49. $playbook = "cluster.yml"
  50. host_vars = {}
  51. if File.exist?(CONFIG)
  52. require CONFIG
  53. end
  54. $box = SUPPORTED_OS[$os][:box]
  55. # if $inventory is not set, try to use example
  56. $inventory = "inventory/sample" if ! $inventory
  57. $inventory = File.absolute_path($inventory, File.dirname(__FILE__))
  58. # if $inventory has a hosts.ini file use it, otherwise copy over
  59. # vars etc to where vagrant expects dynamic inventory to be
  60. if ! File.exist?(File.join(File.dirname($inventory), "hosts.ini"))
  61. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", "provisioners", "ansible")
  62. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  63. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  64. FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
  65. end
  66. end
  67. if Vagrant.has_plugin?("vagrant-proxyconf")
  68. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  69. (1..$num_instances).each do |i|
  70. $no_proxy += ",#{$subnet}.#{i+100}"
  71. end
  72. end
  73. Vagrant.configure("2") do |config|
  74. config.vm.box = $box
  75. if SUPPORTED_OS[$os].has_key? :box_url
  76. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  77. end
  78. config.ssh.username = SUPPORTED_OS[$os][:user]
  79. # plugin conflict
  80. if Vagrant.has_plugin?("vagrant-vbguest") then
  81. config.vbguest.auto_update = false
  82. end
  83. # always use Vagrants insecure key
  84. config.ssh.insert_key = false
  85. if ($override_disk_size)
  86. unless Vagrant.has_plugin?("vagrant-disksize")
  87. system "vagrant plugin install vagrant-disksize"
  88. end
  89. config.disksize.size = $disk_size
  90. end
  91. (1..$num_instances).each do |i|
  92. config.vm.define vm_name = "%s-%01d" % [$instance_name_prefix, i] do |node|
  93. node.vm.hostname = vm_name
  94. if Vagrant.has_plugin?("vagrant-proxyconf")
  95. node.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  96. node.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  97. node.proxy.no_proxy = $no_proxy
  98. end
  99. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  100. node.vm.provider vmware do |v|
  101. v.vmx['memsize'] = $vm_memory
  102. v.vmx['numvcpus'] = $vm_cpus
  103. end
  104. end
  105. node.vm.provider :virtualbox do |vb|
  106. vb.memory = $vm_memory
  107. vb.cpus = $vm_cpus
  108. vb.gui = $vm_gui
  109. vb.linked_clone = true
  110. vb.customize ["modifyvm", :id, "--vram", "8"] # ubuntu defaults to 256 MB which is a waste of precious RAM
  111. end
  112. node.vm.provider :libvirt do |lv|
  113. lv.memory = $vm_memory
  114. lv.cpus = $vm_cpus
  115. lv.default_prefix = 'kubespray'
  116. # Fix kernel panic on fedora 28
  117. if $os == "fedora"
  118. lv.cpu_mode = "host-passthrough"
  119. end
  120. end
  121. if $kube_node_instances_with_disks
  122. # Libvirt
  123. driverletters = ('a'..'z').to_a
  124. node.vm.provider :libvirt do |lv|
  125. # always make /dev/sd{a/b/c} so that CI can ensure that
  126. # virtualbox and libvirt will have the same devices to use for OSDs
  127. (1..$kube_node_instances_with_disks_number).each do |d|
  128. lv.storage :file, :device => "hd#{driverletters[d]}", :path => "disk-#{i}-#{d}-#{DISK_UUID}.disk", :size => $kube_node_instances_with_disks_size, :bus => "ide"
  129. end
  130. end
  131. end
  132. if $expose_docker_tcp
  133. node.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  134. end
  135. $forwarded_ports.each do |guest, host|
  136. node.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  137. end
  138. node.vm.synced_folder ".", "/vagrant", disabled: false, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] , rsync__exclude: ['.git','venv']
  139. $shared_folders.each do |src, dst|
  140. node.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
  141. end
  142. ip = "#{$subnet}.#{i+100}"
  143. node.vm.network :private_network, ip: ip
  144. # Disable swap for each vm
  145. node.vm.provision "shell", inline: "swapoff -a"
  146. host_vars[vm_name] = {
  147. "ip": ip,
  148. "flannel_interface": "eth1",
  149. "kube_network_plugin": $network_plugin,
  150. "kube_network_plugin_multus": $multi_networking,
  151. "docker_keepcache": "1",
  152. "download_run_once": "False",
  153. "download_localhost": "False",
  154. "local_path_provisioner_enabled": "#{$local_path_provisioner_enabled}",
  155. "local_path_provisioner_claim_root": "#{$local_path_provisioner_claim_root}"
  156. }
  157. # Only execute the Ansible provisioner once, when all the machines are up and ready.
  158. if i == $num_instances
  159. node.vm.provision "ansible" do |ansible|
  160. ansible.playbook = $playbook
  161. $ansible_inventory_path = File.join( $inventory, "hosts.ini")
  162. if File.exist?($ansible_inventory_path)
  163. ansible.inventory_path = $ansible_inventory_path
  164. end
  165. ansible.become = true
  166. ansible.limit = "all"
  167. ansible.host_key_checking = false
  168. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache", "-e ansible_become_pass=vagrant"]
  169. ansible.host_vars = host_vars
  170. #ansible.tags = ['download']
  171. ansible.groups = {
  172. "etcd" => ["#{$instance_name_prefix}-[1:#{$etcd_instances}]"],
  173. "kube-master" => ["#{$instance_name_prefix}-[1:#{$kube_master_instances}]"],
  174. "kube-node" => ["#{$instance_name_prefix}-[1:#{$kube_node_instances}]"],
  175. "k8s-cluster:children" => ["kube-master", "kube-node"],
  176. }
  177. end
  178. end
  179. end
  180. end
  181. end