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.

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