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.

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