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.

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