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.

115 lines
3.5 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. Vagrant.configure("2") do |config|
  33. # always use Vagrants insecure key
  34. config.ssh.insert_key = false
  35. config.vm.box = $box
  36. # plugin conflict
  37. if Vagrant.has_plugin?("vagrant-vbguest") then
  38. config.vbguest.auto_update = false
  39. end
  40. (1..$num_instances).each do |i|
  41. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  42. config.vm.hostname = vm_name
  43. if $expose_docker_tcp
  44. config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  45. end
  46. $forwarded_ports.each do |guest, host|
  47. config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  48. end
  49. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  50. config.vm.provider vmware do |v|
  51. v.vmx['memsize'] = $vm_memory
  52. v.vmx['numvcpus'] = $vm_cpus
  53. end
  54. end
  55. config.vm.provider :virtualbox do |vb|
  56. vb.gui = $vm_gui
  57. vb.memory = $vm_memory
  58. vb.cpus = $vm_cpus
  59. end
  60. ip = "#{$subnet}.#{i+100}"
  61. host_vars[vm_name] = {
  62. "ip" => ip,
  63. #"access_ip" => ip,
  64. "flannel_interface" => ip,
  65. "flannel_backend_type" => "host-gw",
  66. "local_release_dir" => "/vagrant/temp",
  67. "download_run_once" => "True"
  68. }
  69. config.vm.network :private_network, ip: ip
  70. # Only execute once the Ansible provisioner,
  71. # when all the machines are up and ready.
  72. if i == $num_instances
  73. config.vm.provision "ansible" do |ansible|
  74. ansible.playbook = "cluster.yml"
  75. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  76. ansible.inventory_path = $inventory
  77. end
  78. ansible.sudo = true
  79. ansible.limit = "all"
  80. ansible.host_key_checking = false
  81. ansible.raw_arguments = ["--forks=#{$num_instances}"]
  82. ansible.host_vars = host_vars
  83. #ansible.tags = ['download']
  84. ansible.groups = {
  85. # The first three nodes should be etcd servers
  86. "etcd" => ["#{$instance_name_prefix}-0[1:3]"],
  87. # The first two nodes should be masters
  88. "kube-master" => ["#{$instance_name_prefix}-0[1:2]"],
  89. # all nodes should be kube nodes
  90. "kube-node" => ["#{$instance_name_prefix}-0[1:#{$num_instances}]"],
  91. "k8s-cluster:children" => ["kube-master", "kube-node"],
  92. }
  93. end
  94. end
  95. end
  96. end
  97. end