Vagrantfile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
  4. VAGRANTFILE_API_VERSION = "2"
  5. # Require a recent version of vagrant otherwise some have reported errors setting host names on boxes
  6. Vagrant.require_version ">= 1.7.4"
  7. if ARGV.first == "up" && ENV['USING_KUBE_SCRIPTS'] != 'true'
  8. raise Vagrant::Errors::VagrantError.new, <<END
  9. Calling 'vagrant up' directly is not supported. Instead, please run the following:
  10. export KUBERNETES_PROVIDER=vagrant
  11. export VAGRANT_DEFAULT_PROVIDER=providername
  12. ./cluster/kube-up.sh
  13. END
  14. end
  15. # The number of nodes to provision
  16. $num_node = (ENV['NUM_NODES'] || 1).to_i
  17. # ip configuration
  18. $master_ip = ENV['MASTER_IP']
  19. $node_ip_base = ENV['NODE_IP_BASE'] || ""
  20. $node_ips = $num_node.times.collect { |n| $node_ip_base + "#{n+3}" }
  21. # Determine the OS platform to use
  22. $kube_os = ENV['KUBERNETES_OS'] || "fedora"
  23. # Determine whether vagrant should use nfs to sync folders
  24. $use_nfs = ENV['KUBERNETES_VAGRANT_USE_NFS'] == 'true'
  25. # To override the vagrant provider, use (e.g.):
  26. # KUBERNETES_PROVIDER=vagrant VAGRANT_DEFAULT_PROVIDER=... .../cluster/kube-up.sh
  27. # To override the box, use (e.g.):
  28. # KUBERNETES_PROVIDER=vagrant KUBERNETES_BOX_NAME=... .../cluster/kube-up.sh
  29. # You can specify a box version:
  30. # KUBERNETES_PROVIDER=vagrant KUBERNETES_BOX_NAME=... KUBERNETES_BOX_VERSION=... .../cluster/kube-up.sh
  31. # You can specify a box location:
  32. # KUBERNETES_PROVIDER=vagrant KUBERNETES_BOX_NAME=... KUBERNETES_BOX_URL=... .../cluster/kube-up.sh
  33. # KUBERNETES_BOX_URL and KUBERNETES_BOX_VERSION will be ignored unless
  34. # KUBERNETES_BOX_NAME is set
  35. # Default OS platform to provider/box information
  36. $kube_provider_boxes = {
  37. :parallels => {
  38. 'fedora' => {
  39. # :box_url and :box_version are optional (and mutually exclusive);
  40. # if :box_url is omitted the box will be retrieved by :box_name (and
  41. # :box_version if provided) from
  42. # http://atlas.hashicorp.com/boxes/search (formerly
  43. # http://vagrantcloud.com/); this allows you override :box_name with
  44. # your own value so long as you provide :box_url; for example, the
  45. # "official" name of this box is "rickard-von-essen/
  46. # opscode_fedora-20", but by providing the URL and our own name, we
  47. # make it appear as yet another provider under the "kube-fedora22"
  48. # box
  49. :box_name => 'kube-fedora23',
  50. :box_url => 'https://opscode-vm-bento.s3.amazonaws.com/vagrant/parallels/opscode_fedora-23_chef-provisionerless.box'
  51. }
  52. },
  53. :virtualbox => {
  54. 'fedora' => {
  55. :box_name => 'kube-fedora23',
  56. :box_url => 'https://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_fedora-23_chef-provisionerless.box'
  57. }
  58. },
  59. :libvirt => {
  60. 'fedora' => {
  61. :box_name => 'kube-fedora23',
  62. :box_url => 'https://dl.fedoraproject.org/pub/fedora/linux/releases/23/Cloud/x86_64/Images/Fedora-Cloud-Base-Vagrant-23-20151030.x86_64.vagrant-libvirt.box'
  63. }
  64. },
  65. :vmware_desktop => {
  66. 'fedora' => {
  67. :box_name => 'kube-fedora23',
  68. :box_url => 'https://opscode-vm-bento.s3.amazonaws.com/vagrant/vmware/opscode_fedora-23_chef-provisionerless.box'
  69. }
  70. },
  71. :vsphere => {
  72. 'fedora' => {
  73. :box_name => 'vsphere-dummy',
  74. :box_url => 'https://github.com/deromka/vagrant-vsphere/blob/master/vsphere-dummy.box?raw=true'
  75. }
  76. }
  77. }
  78. # Give access to all physical cpu cores
  79. # Previously cargo-culted from here:
  80. # http://www.stefanwrobel.com/how-to-make-vagrant-performance-not-suck
  81. # Rewritten to actually determine the number of hardware cores instead of assuming
  82. # that the host has hyperthreading enabled.
  83. host = RbConfig::CONFIG['host_os']
  84. if host =~ /darwin/
  85. $vm_cpus = `sysctl -n hw.physicalcpu`.to_i
  86. elsif host =~ /linux/
  87. #This should work on most processors, however it will fail on ones without the core id field.
  88. #So far i have only seen this on a raspberry pi. which you probably don't want to run vagrant on anyhow...
  89. #But just in case we'll default to the result of nproc if we get 0 just to be safe.
  90. $vm_cpus = `cat /proc/cpuinfo | grep 'core id' | sort -u | wc -l`.to_i
  91. if $vm_cpus < 1
  92. $vm_cpus = `nproc`.to_i
  93. end
  94. else # sorry Windows folks, I can't help you
  95. $vm_cpus = 2
  96. end
  97. # Give VM 1024MB of RAM by default
  98. # In Fedora VM, tmpfs device is mapped to /tmp. tmpfs is given 50% of RAM allocation.
  99. # When doing Salt provisioning, we copy approximately 200MB of content in /tmp before anything else happens.
  100. # This causes problems if anything else was in /tmp or the other directories that are bound to tmpfs device (i.e /run, etc.)
  101. $vm_master_mem = (ENV['KUBERNETES_MASTER_MEMORY'] || ENV['KUBERNETES_MEMORY'] || 1280).to_i
  102. $vm_node_mem = (ENV['KUBERNETES_NODE_MEMORY'] || ENV['KUBERNETES_MEMORY'] || 1024).to_i
  103. Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  104. if Vagrant.has_plugin?("vagrant-proxyconf")
  105. $http_proxy = ENV['KUBERNETES_HTTP_PROXY'] || ""
  106. $https_proxy = ENV['KUBERNETES_HTTPS_PROXY'] || ""
  107. $no_proxy = ENV['KUBERNETES_NO_PROXY'] || "127.0.0.1"
  108. config.proxy.http = $http_proxy
  109. config.proxy.https = $https_proxy
  110. config.proxy.no_proxy = $no_proxy
  111. end
  112. def setvmboxandurl(config, provider)
  113. if ENV['KUBERNETES_BOX_NAME'] then
  114. config.vm.box = ENV['KUBERNETES_BOX_NAME']
  115. if ENV['KUBERNETES_BOX_URL'] then
  116. config.vm.box_url = ENV['KUBERNETES_BOX_URL']
  117. end
  118. if ENV['KUBERNETES_BOX_VERSION'] then
  119. config.vm.box_version = ENV['KUBERNETES_BOX_VERSION']
  120. end
  121. else
  122. config.vm.box = $kube_provider_boxes[provider][$kube_os][:box_name]
  123. if $kube_provider_boxes[provider][$kube_os][:box_url] then
  124. config.vm.box_url = $kube_provider_boxes[provider][$kube_os][:box_url]
  125. end
  126. if $kube_provider_boxes[provider][$kube_os][:box_version] then
  127. config.vm.box_version = $kube_provider_boxes[provider][$kube_os][:box_version]
  128. end
  129. end
  130. end
  131. def customize_vm(config, vm_mem)
  132. if $use_nfs then
  133. config.vm.synced_folder ".", "/vagrant", nfs: true
  134. end
  135. # Try VMWare Fusion first (see
  136. # https://docs.vagrantup.com/v2/providers/basic_usage.html)
  137. config.vm.provider :vmware_fusion do |v, override|
  138. setvmboxandurl(override, :vmware_desktop)
  139. v.vmx['memsize'] = vm_mem
  140. v.vmx['numvcpus'] = $vm_cpus
  141. end
  142. # configure libvirt provider
  143. config.vm.provider :libvirt do |v, override|
  144. setvmboxandurl(override, :libvirt)
  145. v.memory = vm_mem
  146. v.cpus = $vm_cpus
  147. v.nested = true
  148. v.volume_cache = 'none'
  149. end
  150. # Then try VMWare Workstation
  151. config.vm.provider :vmware_workstation do |v, override|
  152. setvmboxandurl(override, :vmware_desktop)
  153. v.vmx['memsize'] = vm_mem
  154. v.vmx['numvcpus'] = $vm_cpus
  155. end
  156. # Then try Parallels
  157. config.vm.provider :parallels do |v, override|
  158. setvmboxandurl(override, :parallels)
  159. v.memory = vm_mem # v.customize ['set', :id, '--memsize', vm_mem]
  160. v.cpus = $vm_cpus # v.customize ['set', :id, '--cpus', $vm_cpus]
  161. # Don't attempt to update the Parallels tools on the image (this can
  162. # be done manually if necessary)
  163. v.update_guest_tools = false # v.customize ['set', :id, '--tools-autoupdate', 'off']
  164. # Set up Parallels folder sharing to behave like VirtualBox (i.e.,
  165. # mount the current directory as /vagrant and that's it)
  166. v.customize ['set', :id, '--shf-guest', 'off']
  167. v.customize ['set', :id, '--shf-guest-automount', 'off']
  168. v.customize ['set', :id, '--shf-host', 'on']
  169. # Synchronize VM clocks to host clock (Avoid certificate invalid issue)
  170. v.customize ['set', :id, '--time-sync', 'on']
  171. # Remove all auto-mounted "shared folders"; the result seems to
  172. # persist between runs (i.e., vagrant halt && vagrant up)
  173. override.vm.provision :shell, :inline => (%q{
  174. set -ex
  175. if [ -d /media/psf ]; then
  176. for i in /media/psf/*; do
  177. if [ -d "${i}" ]; then
  178. umount "${i}" || true
  179. rmdir -v "${i}"
  180. fi
  181. done
  182. rmdir -v /media/psf
  183. fi
  184. exit
  185. }).strip
  186. end
  187. # Then try vsphere
  188. config.vm.provider :vsphere do |vsphere, override|
  189. setvmboxandurl(override, :vsphere)
  190. #config.vm.hostname = ENV['MASTER_NAME']
  191. config.ssh.username = ENV['MASTER_USER']
  192. config.ssh.password = ENV['MASTER_PASSWD']
  193. config.ssh.pty = true
  194. config.ssh.insert_key = true
  195. #config.ssh.private_key_path = '~/.ssh/id_rsa_vsphere'
  196. # Don't attempt to update the tools on the image (this can
  197. # be done manually if necessary)
  198. # vsphere.update_guest_tools = false # v.customize ['set', :id, '--tools-autoupdate', 'off']
  199. # The vSphere host we're going to connect to
  200. vsphere.host = ENV['VAGRANT_VSPHERE_URL']
  201. # The ESX host for the new VM
  202. vsphere.compute_resource_name = ENV['VAGRANT_VSPHERE_RESOURCE_POOL']
  203. # The resource pool for the new VM
  204. #vsphere.resource_pool_name = 'Comp'
  205. # path to folder where new VM should be created, if not specified template's parent folder will be used
  206. vsphere.vm_base_path = ENV['VAGRANT_VSPHERE_BASE_PATH']
  207. # The template we're going to clone
  208. vsphere.template_name = ENV['VAGRANT_VSPHERE_TEMPLATE_NAME']
  209. # The name of the new machine
  210. #vsphere.name = ENV['MASTER_NAME']
  211. # vSphere login
  212. vsphere.user = ENV['VAGRANT_VSPHERE_USERNAME']
  213. # vSphere password
  214. vsphere.password = ENV['VAGRANT_VSPHERE_PASSWORD']
  215. # cpu count
  216. vsphere.cpu_count = $vm_cpus
  217. # memory in MB
  218. vsphere.memory_mb = vm_mem
  219. # If you don't have SSL configured correctly, set this to 'true'
  220. vsphere.insecure = ENV['VAGRANT_VSPHERE_INSECURE']
  221. end
  222. # Don't attempt to update Virtualbox Guest Additions (requires gcc)
  223. if Vagrant.has_plugin?("vagrant-vbguest") then
  224. config.vbguest.auto_update = false
  225. end
  226. # Finally, fall back to VirtualBox
  227. config.vm.provider :virtualbox do |v, override|
  228. setvmboxandurl(override, :virtualbox)
  229. v.memory = vm_mem # v.customize ["modifyvm", :id, "--memory", vm_mem]
  230. v.cpus = $vm_cpus # v.customize ["modifyvm", :id, "--cpus", $vm_cpus]
  231. # Use faster paravirtualized networking
  232. v.customize ["modifyvm", :id, "--nictype1", "virtio"]
  233. v.customize ["modifyvm", :id, "--nictype2", "virtio"]
  234. end
  235. end
  236. # Kubernetes master
  237. config.vm.define "master" do |c|
  238. customize_vm c, $vm_master_mem
  239. if ENV['KUBE_TEMP'] then
  240. script = "#{ENV['KUBE_TEMP']}/master-start.sh"
  241. c.vm.provision "shell", run: "always", path: script
  242. end
  243. c.vm.network "private_network", ip: "#{$master_ip}"
  244. end
  245. # Kubernetes node
  246. $num_node.times do |n|
  247. node_vm_name = "node-#{n+1}"
  248. node_prefix = ENV['INSTANCE_PREFIX'] || 'kubernetes' # must mirror default in cluster/vagrant/config-default.sh
  249. node_hostname = "#{node_prefix}-#{node_vm_name}"
  250. config.vm.define node_vm_name do |node|
  251. customize_vm node, $vm_node_mem
  252. node_ip = $node_ips[n]
  253. if ENV['KUBE_TEMP'] then
  254. script = "#{ENV['KUBE_TEMP']}/node-start-#{n}.sh"
  255. node.vm.provision "shell", run: "always", path: script
  256. end
  257. node.vm.network "private_network", ip: "#{node_ip}"
  258. end
  259. end
  260. end