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.

126 lines
4.0 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 = 1024
  11. $vm_cpus = 1
  12. $shared_folders = {}
  13. $forwarded_ports = {}
  14. $subnet = "172.17.8"
  15. host_vars = {}
  16. if File.exist?(CONFIG)
  17. require CONFIG
  18. end
  19. # if $inventory is not set, try to use example
  20. $inventory = File.join(File.dirname(__FILE__), "inventory") if ! $inventory
  21. # if $inventory has a hosts file use it, otherwise copy over vars etc
  22. # to where vagrant expects dynamic inventory to be.
  23. if ! File.exist?(File.join(File.dirname($inventory), "hosts"))
  24. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
  25. "provisioners", "ansible")
  26. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  27. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  28. FileUtils.ln_s($inventory, $vagrant_ansible)
  29. end
  30. end
  31. Vagrant.configure("2") do |config|
  32. # always use Vagrants insecure key
  33. config.ssh.insert_key = false
  34. config.vm.box = "ubuntu-14.04"
  35. config.vm.box_url = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant.json" % [$update_channel, $image_version]
  36. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  37. config.vm.provider vmware do |v, override|
  38. override.vm.box_url = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant_vmware_fusion.json" % [$update_channel, $image_version]
  39. end
  40. end
  41. config.vm.provider :virtualbox do |v|
  42. # On VirtualBox, we don't have guest additions or a functional vboxsf
  43. # in CoreOS, so tell Vagrant that so it can be smarter.
  44. v.check_guest_additions = false
  45. v.functional_vboxsf = false
  46. end
  47. # plugin conflict
  48. if Vagrant.has_plugin?("vagrant-vbguest") then
  49. config.vbguest.auto_update = false
  50. end
  51. (1..$num_instances).each do |i|
  52. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  53. config.vm.hostname = vm_name
  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. }
  78. config.vm.network :private_network, ip: ip
  79. # Only execute once the Ansible provisioner,
  80. # when all the machines are up and ready.
  81. if i == $num_instances
  82. config.vm.provision "ansible" do |ansible|
  83. ansible.playbook = "cluster.yml"
  84. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  85. ansible.inventory_path = $inventory
  86. end
  87. ansible.sudo = true
  88. ansible.limit = "all"
  89. ansible.host_key_checking = false
  90. ansible.raw_arguments = ["--forks=#{$num_instances}"]
  91. ansible.host_vars = host_vars
  92. ansible.groups = {
  93. # The first three nodes should be etcd servers
  94. "etcd" => ["k8s-0[1:3]"],
  95. # The first two nodes should be masters
  96. "kube-master" => ["k8s-0[1:2]"],
  97. # all nodes should be kube nodes
  98. "kube-node" => ["k8s-0[1:#{$num_instances}]"],
  99. "k8s-cluster:children" => ["kube-master", "kube-node"],
  100. }
  101. end
  102. end
  103. end
  104. end
  105. end