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.

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