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.

160 lines
5.5 KiB

  1. # -*- mode: ruby -*-
  2. # # vi: set ft=ruby :
  3. require 'fileutils'
  4. Vagrant.require_version ">= 1.9.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. SUPPORTED_OS = {
  8. "coreos-stable" => {box: "coreos-stable", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
  9. "coreos-alpha" => {box: "coreos-alpha", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
  10. "coreos-beta" => {box: "coreos-beta", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
  11. "ubuntu" => {box: "bento/ubuntu-16.04", bootstrap_os: "ubuntu", user: "vagrant"},
  12. "centos" => {box: "bento/centos-7.3", bootstrap_os: "centos", user: "vagrant"},
  13. }
  14. # Defaults for config options defined in CONFIG
  15. $num_instances = 3
  16. $instance_name_prefix = "k8s"
  17. $vm_gui = false
  18. $vm_memory = 2048
  19. $vm_cpus = 1
  20. $shared_folders = {}
  21. $forwarded_ports = {}
  22. $subnet = "172.17.8"
  23. $os = "ubuntu"
  24. $network_plugin = "flannel"
  25. # The first three nodes are etcd servers
  26. $etcd_instances = $num_instances
  27. # The first two nodes are kube masters
  28. $kube_master_instances = $num_instances == 1 ? $num_instances : ($num_instances - 1)
  29. # All nodes are kube nodes
  30. $kube_node_instances = $num_instances
  31. $local_release_dir = "/vagrant/temp"
  32. host_vars = {}
  33. if File.exist?(CONFIG)
  34. require CONFIG
  35. end
  36. $box = SUPPORTED_OS[$os][:box]
  37. # if $inventory is not set, try to use example
  38. $inventory = File.join(File.dirname(__FILE__), "inventory") if ! $inventory
  39. # if $inventory has a hosts file use it, otherwise copy over vars etc
  40. # to where vagrant expects dynamic inventory to be.
  41. if ! File.exist?(File.join(File.dirname($inventory), "hosts"))
  42. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
  43. "provisioners", "ansible")
  44. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  45. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  46. FileUtils.ln_s($inventory, $vagrant_ansible)
  47. end
  48. end
  49. if Vagrant.has_plugin?("vagrant-proxyconf")
  50. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  51. (1..$num_instances).each do |i|
  52. $no_proxy += ",#{$subnet}.#{i+100}"
  53. end
  54. end
  55. Vagrant.configure("2") do |config|
  56. # always use Vagrants insecure key
  57. config.ssh.insert_key = false
  58. config.vm.box = $box
  59. if SUPPORTED_OS[$os].has_key? :box_url
  60. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  61. end
  62. config.ssh.username = SUPPORTED_OS[$os][:user]
  63. # plugin conflict
  64. if Vagrant.has_plugin?("vagrant-vbguest") then
  65. config.vbguest.auto_update = false
  66. end
  67. (1..$num_instances).each do |i|
  68. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  69. config.vm.hostname = vm_name
  70. if Vagrant.has_plugin?("vagrant-proxyconf")
  71. config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  72. config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  73. config.proxy.no_proxy = $no_proxy
  74. end
  75. if $expose_docker_tcp
  76. config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  77. end
  78. $forwarded_ports.each do |guest, host|
  79. config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  80. end
  81. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  82. config.vm.provider vmware do |v|
  83. v.vmx['memsize'] = $vm_memory
  84. v.vmx['numvcpus'] = $vm_cpus
  85. end
  86. end
  87. $shared_folders.each do |src, dst|
  88. config.vm.synced_folder src, dst
  89. end
  90. config.vm.provider :virtualbox do |vb|
  91. vb.gui = $vm_gui
  92. vb.memory = $vm_memory
  93. vb.cpus = $vm_cpus
  94. end
  95. ip = "#{$subnet}.#{i+100}"
  96. host_vars[vm_name] = {
  97. "ip": ip,
  98. "bootstrap_os": SUPPORTED_OS[$os][:bootstrap_os],
  99. "local_release_dir" => $local_release_dir,
  100. "download_run_once": "False",
  101. "kube_network_plugin": $network_plugin
  102. }
  103. config.vm.network :private_network, ip: ip
  104. # workaround for Vagrant 1.9.1 and centos vm
  105. # https://github.com/hashicorp/vagrant/issues/8096
  106. if Vagrant::VERSION == "1.9.1" && $os == "centos"
  107. config.vm.provision "shell", inline: "service network restart", run: "always"
  108. end
  109. # Disable swap for each vm
  110. config.vm.provision "shell", inline: "swapoff -a"
  111. # Only execute once the Ansible provisioner,
  112. # when all the machines are up and ready.
  113. if i == $num_instances
  114. config.vm.provision "ansible" do |ansible|
  115. ansible.playbook = "cluster.yml"
  116. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  117. ansible.inventory_path = $inventory
  118. end
  119. ansible.sudo = true
  120. ansible.limit = "all"
  121. ansible.host_key_checking = false
  122. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache"]
  123. ansible.host_vars = host_vars
  124. #ansible.tags = ['download']
  125. ansible.groups = {
  126. "etcd" => ["#{$instance_name_prefix}-0[1:#{$etcd_instances}]"],
  127. "kube-master" => ["#{$instance_name_prefix}-0[1:#{$kube_master_instances}]"],
  128. "kube-node" => ["#{$instance_name_prefix}-0[1:#{$kube_node_instances}]"],
  129. "k8s-cluster:children" => ["kube-master", "kube-node"],
  130. }
  131. end
  132. end
  133. end
  134. end
  135. end