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.

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