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.

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