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.

183 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. $playbook = "cluster.yml"
  40. $local_release_dir = "/vagrant/temp"
  41. host_vars = {}
  42. if File.exist?(CONFIG)
  43. require CONFIG
  44. end
  45. $box = SUPPORTED_OS[$os][:box]
  46. # if $inventory is not set, try to use example
  47. $inventory = File.join(File.dirname(__FILE__), "inventory", "sample") if ! $inventory
  48. # if $inventory has a hosts file use it, otherwise copy over vars etc
  49. # to where vagrant expects dynamic inventory to be.
  50. if ! File.exist?(File.join(File.dirname($inventory), "hosts"))
  51. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
  52. "provisioners", "ansible")
  53. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  54. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  55. FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
  56. end
  57. end
  58. if Vagrant.has_plugin?("vagrant-proxyconf")
  59. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  60. (1..$num_instances).each do |i|
  61. $no_proxy += ",#{$subnet}.#{i+100}"
  62. end
  63. end
  64. Vagrant.configure("2") do |config|
  65. # always use Vagrants insecure key
  66. config.ssh.insert_key = false
  67. config.vm.box = $box
  68. if SUPPORTED_OS[$os].has_key? :box_url
  69. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  70. end
  71. config.ssh.username = SUPPORTED_OS[$os][:user]
  72. # plugin conflict
  73. if Vagrant.has_plugin?("vagrant-vbguest") then
  74. config.vbguest.auto_update = false
  75. end
  76. (1..$num_instances).each do |i|
  77. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  78. config.vm.hostname = vm_name
  79. if Vagrant.has_plugin?("vagrant-proxyconf")
  80. config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  81. config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  82. config.proxy.no_proxy = $no_proxy
  83. end
  84. if $expose_docker_tcp
  85. config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  86. end
  87. $forwarded_ports.each do |guest, host|
  88. config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  89. end
  90. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  91. config.vm.provider vmware do |v|
  92. v.vmx['memsize'] = $vm_memory
  93. v.vmx['numvcpus'] = $vm_cpus
  94. end
  95. end
  96. config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
  97. $shared_folders.each do |src, dst|
  98. config.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
  99. end
  100. config.vm.provider :virtualbox do |vb|
  101. vb.gui = $vm_gui
  102. vb.memory = $vm_memory
  103. vb.cpus = $vm_cpus
  104. end
  105. config.vm.provider :libvirt do |lv|
  106. lv.memory = $vm_memory
  107. end
  108. ip = "#{$subnet}.#{i+100}"
  109. host_vars[vm_name] = {
  110. "ip": ip,
  111. "bootstrap_os": SUPPORTED_OS[$os][:bootstrap_os],
  112. "local_release_dir" => $local_release_dir,
  113. "download_run_once": "False",
  114. "kube_network_plugin": $network_plugin
  115. }
  116. config.vm.network :private_network, ip: ip
  117. # Disable swap for each vm
  118. config.vm.provision "shell", inline: "swapoff -a"
  119. if $kube_node_instances_with_disks
  120. # Libvirt
  121. driverletters = ('a'..'z').to_a
  122. config.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. # Only execute once the Ansible provisioner,
  131. # when all the machines are up and ready.
  132. if i == $num_instances
  133. config.vm.provision "ansible" do |ansible|
  134. ansible.playbook = $playbook
  135. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  136. ansible.inventory_path = $inventory
  137. end
  138. ansible.become = true
  139. ansible.limit = "all"
  140. ansible.host_key_checking = false
  141. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache"]
  142. ansible.host_vars = host_vars
  143. #ansible.tags = ['download']
  144. ansible.groups = {
  145. "etcd" => ["#{$instance_name_prefix}-0[1:#{$etcd_instances}]"],
  146. "kube-master" => ["#{$instance_name_prefix}-0[1:#{$kube_master_instances}]"],
  147. "kube-node" => ["#{$instance_name_prefix}-0[1:#{$kube_node_instances}]"],
  148. "k8s-cluster:children" => ["kube-master", "kube-node"],
  149. }
  150. end
  151. end
  152. end
  153. end
  154. end