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.

153 lines
5.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. COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json"
  7. SUPPORTED_OS = {
  8. "coreos-stable" => {box: "coreos-stable", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
  9. "coreos-alpha" => {box: "coreos-alpha", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
  10. "coreos-beta" => {box: "coreos-beta", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
  11. "ubuntu" => {box: "bento/ubuntu-16.04", bootstrap_os: "ubuntu", user: "vagrant"},
  12. }
  13. # Defaults for config options defined in CONFIG
  14. $num_instances = 3
  15. $instance_name_prefix = "k8s"
  16. $vm_gui = false
  17. $vm_memory = 1536
  18. $vm_cpus = 1
  19. $shared_folders = {}
  20. $forwarded_ports = {}
  21. $subnet = "172.17.8"
  22. $os = "ubuntu"
  23. # The first three nodes are etcd servers
  24. $etcd_instances = $num_instances
  25. # The first two nodes are masters
  26. $kube_master_instances = $num_instances == 1 ? $num_instances : ($num_instances - 1)
  27. $local_release_dir = "/vagrant/temp"
  28. host_vars = {}
  29. if File.exist?(CONFIG)
  30. require CONFIG
  31. end
  32. # All nodes are kube nodes
  33. $kube_node_instances = $num_instances
  34. $box = SUPPORTED_OS[$os][:box]
  35. # if $inventory is not set, try to use example
  36. $inventory = File.join(File.dirname(__FILE__), "inventory") if ! $inventory
  37. # if $inventory has a hosts file use it, otherwise copy over vars etc
  38. # to where vagrant expects dynamic inventory to be.
  39. if ! File.exist?(File.join(File.dirname($inventory), "hosts"))
  40. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
  41. "provisioners", "ansible")
  42. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  43. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  44. FileUtils.ln_s($inventory, $vagrant_ansible)
  45. end
  46. end
  47. if Vagrant.has_plugin?("vagrant-proxyconf")
  48. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  49. (1..$num_instances).each do |i|
  50. $no_proxy += ",#{$subnet}.#{i+100}"
  51. end
  52. end
  53. Vagrant.configure("2") do |config|
  54. # always use Vagrants insecure key
  55. config.ssh.insert_key = false
  56. config.vm.box = $box
  57. if SUPPORTED_OS[$os].has_key? :box_url
  58. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  59. end
  60. config.ssh.username = SUPPORTED_OS[$os][:user]
  61. # plugin conflict
  62. if Vagrant.has_plugin?("vagrant-vbguest") then
  63. config.vbguest.auto_update = false
  64. end
  65. (1..$num_instances).each do |i|
  66. config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
  67. config.vm.hostname = vm_name
  68. if Vagrant.has_plugin?("vagrant-proxyconf")
  69. config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  70. config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  71. config.proxy.no_proxy = $no_proxy
  72. end
  73. if $expose_docker_tcp
  74. config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  75. end
  76. $forwarded_ports.each do |guest, host|
  77. config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  78. end
  79. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  80. config.vm.provider vmware do |v|
  81. v.vmx['memsize'] = $vm_memory
  82. v.vmx['numvcpus'] = $vm_cpus
  83. end
  84. end
  85. $shared_folders.each do |src, dst|
  86. config.vm.synced_folder src, dst
  87. end
  88. config.vm.provider :virtualbox do |vb|
  89. vb.gui = $vm_gui
  90. vb.memory = $vm_memory
  91. vb.cpus = $vm_cpus
  92. end
  93. ip = "#{$subnet}.#{i+100}"
  94. host_vars[vm_name] = {
  95. "ip": ip,
  96. "flannel_interface": ip,
  97. "flannel_backend_type": "host-gw",
  98. "local_release_dir" => $local_release_dir,
  99. "download_run_once": "False",
  100. # Override the default 'calico' with flannel.
  101. # inventory/group_vars/k8s-cluster.yml
  102. "kube_network_plugin": "flannel",
  103. "bootstrap_os": SUPPORTED_OS[$os][:bootstrap_os]
  104. }
  105. config.vm.network :private_network, ip: ip
  106. # Only execute once the Ansible provisioner,
  107. # when all the machines are up and ready.
  108. if i == $num_instances
  109. config.vm.provision "ansible" do |ansible|
  110. ansible.playbook = "cluster.yml"
  111. if File.exist?(File.join(File.dirname($inventory), "hosts"))
  112. ansible.inventory_path = $inventory
  113. end
  114. ansible.sudo = true
  115. ansible.limit = "all"
  116. ansible.host_key_checking = false
  117. ansible.raw_arguments = ["--forks=#{$num_instances}"]
  118. ansible.host_vars = host_vars
  119. #ansible.tags = ['download']
  120. ansible.groups = {
  121. "etcd" => ["#{$instance_name_prefix}-0[1:#{$etcd_instances}]"],
  122. "kube-master" => ["#{$instance_name_prefix}-0[1:#{$kube_master_instances}]"],
  123. "kube-node" => ["#{$instance_name_prefix}-0[1:#{$kube_node_instances}]"],
  124. "k8s-cluster:children" => ["kube-master", "kube-node"],
  125. }
  126. end
  127. end
  128. end
  129. end
  130. end