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.

128 lines
3.9 KiB

  1. # -*- mode: ruby -*-
  2. # # vi: set ft=ruby :
  3. require 'fileutils'
  4. Vagrant.require_version ">= 1.8.0"
  5. CONFIG = File.join(File.dirname(__FILE__), "vagrant/config.rb")
  6. # Defaults for config options defined in CONFIG
  7. $num_instances = 3
  8. $instance_name_prefix = "k8s"
  9. $vm_gui = false
  10. $vm_memory = 1536
  11. $vm_cpus = 1
  12. $shared_folders = {}
  13. $forwarded_ports = {}
  14. $subnet = "172.17.8"
  15. $box = "bento/ubuntu-16.04"
  16. host_vars = {}
  17. if File.exist?(CONFIG)
  18. require CONFIG
  19. end
  20. # if $inventory is not set, try to use example
  21. $inventory = File.join(File.dirname(__FILE__), "inventory") if ! $inventory
  22. # if $inventory has a hosts file use it, otherwise copy over vars etc
  23. # to where vagrant expects dynamic inventory to be.
  24. if ! File.exist?(File.join(File.dirname($inventory), "hosts"))
  25. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
  26. "provisioners", "ansible")
  27. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  28. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  29. FileUtils.ln_s($inventory, $vagrant_ansible)
  30. end
  31. end
  32. if Vagrant.has_plugin?("vagrant-proxyconf")
  33. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  34. (1..$num_instances).each do |i|
  35. $no_proxy += ",#{$subnet}.#{i+100}"
  36. end
  37. end
  38. Vagrant.configure("2") do |config|
  39. # always use Vagrants insecure key
  40. config.ssh.insert_key = false
  41. config.vm.box = $box
  42. # plugin conflict
  43. if Vagrant.has_plugin?("vagrant-vbguest") then
  44. config.vbguest.auto_update = false
  45. end
  46. (1..$num_instances).each do |i|
  47. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  48. config.vm.hostname = vm_name
  49. if Vagrant.has_plugin?("vagrant-proxyconf")
  50. config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  51. config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  52. config.proxy.no_proxy = $no_proxy
  53. end
  54. if $expose_docker_tcp
  55. config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  56. end
  57. $forwarded_ports.each do |guest, host|
  58. config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  59. end
  60. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  61. config.vm.provider vmware do |v|
  62. v.vmx['memsize'] = $vm_memory
  63. v.vmx['numvcpus'] = $vm_cpus
  64. end
  65. end
  66. config.vm.provider :virtualbox do |vb|
  67. vb.gui = $vm_gui
  68. vb.memory = $vm_memory
  69. vb.cpus = $vm_cpus
  70. end
  71. ip = "#{$subnet}.#{i+100}"
  72. host_vars[vm_name] = {
  73. "ip" => ip,
  74. #"access_ip" => ip,
  75. "flannel_interface" => ip,
  76. "flannel_backend_type" => "host-gw",
  77. "local_release_dir" => "/vagrant/temp",
  78. "download_run_once" => "False"
  79. }
  80. config.vm.network :private_network, ip: ip
  81. # Only execute once the Ansible provisioner,
  82. # when all the machines are up and ready.
  83. if i == $num_instances
  84. config.vm.provision "ansible" do |ansible|
  85. ansible.playbook = "cluster.yml"
  86. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  87. ansible.inventory_path = $inventory
  88. end
  89. ansible.sudo = true
  90. ansible.limit = "all"
  91. ansible.host_key_checking = false
  92. ansible.raw_arguments = ["--forks=#{$num_instances}"]
  93. ansible.host_vars = host_vars
  94. #ansible.tags = ['download']
  95. ansible.groups = {
  96. # The first three nodes should be etcd servers
  97. "etcd" => ["#{$instance_name_prefix}-0[1:3]"],
  98. # The first two nodes should be masters
  99. "kube-master" => ["#{$instance_name_prefix}-0[1:2]"],
  100. # all nodes should be kube nodes
  101. "kube-node" => ["#{$instance_name_prefix}-0[1:#{$num_instances}]"],
  102. "k8s-cluster:children" => ["kube-master", "kube-node"],
  103. }
  104. end
  105. end
  106. end
  107. end
  108. end