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.

234 lines
9.4 KiB

Added file and container image caching (#4828) * File and container image downloads are now cached localy, so that repeated vagrant up/down runs do not trigger downloading of those files. This is especially useful on laptops with kubernetes runnig locally on vm's. The total size of the cache, after an ansible run, is currently around 800MB, so bandwidth (=time) savings can be quite significant. * When download_run_once is false, the default is still not to cache, but setting download_force_cache will still enable caching. * The local cache location can be set with download_cache_dir and defaults to /tmp/kubernetes_cache * A local docker instance is no longer required to cache docker images; Images are cached to file. A local docker instance is still required, though, if you wish to download images on localhost. * Fixed a FIXME, wher the argument was that delegate_to doesn't play nice with omit. That is a correct observation and the fix is to use default(inventory_host) instead of default(omit). See ansible/ansible#26009 * Removed "Register docker images info" task from download_container and set_docker_image_facts because it was faulty and unused. * Removed redundant when:download.{container,enabled,run_once} conditions from {sync,download}_container.yml * All features of commit d6fd0d2acaec9f53e75d82db30411f96a5bf2cc9 by Timoses <timosesu@gmail.com>, merged May 1st 2019, are included in this patch. Not all code was included verbatim, but each feature of that commit was checked to be working in this patch. One notable change: The actual downloading of the kubeadm images was moved to {download,sync)_container, to enable caching. Note 1: I considered splitting this patch, but most changes that are not directly related to caching, are a pleasant by-product of implementing the caching code, so splitting would be impractical. Note 2: I have my doubts about the usefulness of the upload, download and upgrade tags in the download role. Must they remain or can they be removed? If anybody knows, then please speak up.
5 years ago
Added file and container image caching (#4828) * File and container image downloads are now cached localy, so that repeated vagrant up/down runs do not trigger downloading of those files. This is especially useful on laptops with kubernetes runnig locally on vm's. The total size of the cache, after an ansible run, is currently around 800MB, so bandwidth (=time) savings can be quite significant. * When download_run_once is false, the default is still not to cache, but setting download_force_cache will still enable caching. * The local cache location can be set with download_cache_dir and defaults to /tmp/kubernetes_cache * A local docker instance is no longer required to cache docker images; Images are cached to file. A local docker instance is still required, though, if you wish to download images on localhost. * Fixed a FIXME, wher the argument was that delegate_to doesn't play nice with omit. That is a correct observation and the fix is to use default(inventory_host) instead of default(omit). See ansible/ansible#26009 * Removed "Register docker images info" task from download_container and set_docker_image_facts because it was faulty and unused. * Removed redundant when:download.{container,enabled,run_once} conditions from {sync,download}_container.yml * All features of commit d6fd0d2acaec9f53e75d82db30411f96a5bf2cc9 by Timoses <timosesu@gmail.com>, merged May 1st 2019, are included in this patch. Not all code was included verbatim, but each feature of that commit was checked to be working in this patch. One notable change: The actual downloading of the kubeadm images was moved to {download,sync)_container, to enable caching. Note 1: I considered splitting this patch, but most changes that are not directly related to caching, are a pleasant by-product of implementing the caching code, so splitting would be impractical. Note 2: I have my doubts about the usefulness of the upload, download and upgrade tags in the download role. Must they remain or can they be removed? If anybody knows, then please speak up.
5 years ago
  1. # -*- mode: ruby -*-
  2. # # vi: set ft=ruby :
  3. # For help on using kubespray with vagrant, check out docs/vagrant.md
  4. require 'fileutils'
  5. Vagrant.require_version ">= 2.0.0"
  6. CONFIG = File.join(File.dirname(__FILE__), ENV['KUBESPRAY_VAGRANT_CONFIG'] || 'vagrant/config.rb')
  7. COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json"
  8. FLATCAR_URL_TEMPLATE = "https://%s.release.flatcar-linux.net/amd64-usr/current/flatcar_production_vagrant.json"
  9. # Uniq disk UUID for libvirt
  10. DISK_UUID = Time.now.utc.to_i
  11. SUPPORTED_OS = {
  12. "coreos-stable" => {box: "coreos-stable", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
  13. "coreos-alpha" => {box: "coreos-alpha", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
  14. "coreos-beta" => {box: "coreos-beta", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
  15. "flatcar-stable" => {box: "flatcar-stable", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["stable"]},
  16. "flatcar-beta" => {box: "flatcar-beta", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["beta"]},
  17. "flatcar-alpha" => {box: "flatcar-alpha", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["alpha"]},
  18. "flatcar-edge" => {box: "flatcar-edge", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["edge"]},
  19. "ubuntu1604" => {box: "generic/ubuntu1604", user: "vagrant"},
  20. "ubuntu1804" => {box: "generic/ubuntu1804", user: "vagrant"},
  21. "ubuntu2004" => {box: "geerlingguy/ubuntu2004", user: "vagrant"},
  22. "centos" => {box: "centos/7", user: "vagrant"},
  23. "centos-bento" => {box: "bento/centos-7.6", user: "vagrant"},
  24. "centos8" => {box: "centos/8", user: "vagrant"},
  25. "centos8-bento" => {box: "bento/centos-8", user: "vagrant"},
  26. "fedora30" => {box: "fedora/30-cloud-base", user: "vagrant"},
  27. "fedora31" => {box: "fedora/31-cloud-base", user: "vagrant"},
  28. "opensuse" => {box: "bento/opensuse-leap-15.1", user: "vagrant"},
  29. "opensuse-tumbleweed" => {box: "opensuse/Tumbleweed.x86_64", user: "vagrant"},
  30. "oraclelinux" => {box: "generic/oracle7", user: "vagrant"},
  31. }
  32. if File.exist?(CONFIG)
  33. require CONFIG
  34. end
  35. # Defaults for config options defined in CONFIG
  36. $num_instances ||= 3
  37. $instance_name_prefix ||= "k8s"
  38. $vm_gui ||= false
  39. $vm_memory ||= 2048
  40. $vm_cpus ||= 1
  41. $shared_folders ||= {}
  42. $forwarded_ports ||= {}
  43. $subnet ||= "172.18.8"
  44. $os ||= "ubuntu1804"
  45. $network_plugin ||= "flannel"
  46. # Setting multi_networking to true will install Multus: https://github.com/intel/multus-cni
  47. $multi_networking ||= false
  48. # The first three nodes are etcd servers
  49. $etcd_instances ||= $num_instances
  50. # The first two nodes are kube masters
  51. $kube_master_instances ||= $num_instances == 1 ? $num_instances : ($num_instances - 1)
  52. # All nodes are kube nodes
  53. $kube_node_instances ||= $num_instances
  54. # The following only works when using the libvirt provider
  55. $kube_node_instances_with_disks ||= false
  56. $kube_node_instances_with_disks_size ||= "20G"
  57. $kube_node_instances_with_disks_number ||= 2
  58. $override_disk_size ||= false
  59. $disk_size ||= "20GB"
  60. $local_path_provisioner_enabled ||= false
  61. $local_path_provisioner_claim_root ||= "/opt/local-path-provisioner/"
  62. $playbook = "cluster.yml"
  63. host_vars = {}
  64. $box = SUPPORTED_OS[$os][:box]
  65. # if $inventory is not set, try to use example
  66. $inventory = "inventory/sample" if ! $inventory
  67. $inventory = File.absolute_path($inventory, File.dirname(__FILE__))
  68. # if $inventory has a hosts.ini file use it, otherwise copy over
  69. # vars etc to where vagrant expects dynamic inventory to be
  70. if ! File.exist?(File.join(File.dirname($inventory), "hosts.ini"))
  71. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", "provisioners", "ansible")
  72. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  73. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  74. FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
  75. end
  76. end
  77. if Vagrant.has_plugin?("vagrant-proxyconf")
  78. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  79. (1..$num_instances).each do |i|
  80. $no_proxy += ",#{$subnet}.#{i+100}"
  81. end
  82. end
  83. Vagrant.configure("2") do |config|
  84. config.vm.box = $box
  85. if SUPPORTED_OS[$os].has_key? :box_url
  86. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  87. end
  88. config.ssh.username = SUPPORTED_OS[$os][:user]
  89. # plugin conflict
  90. if Vagrant.has_plugin?("vagrant-vbguest") then
  91. config.vbguest.auto_update = false
  92. end
  93. # always use Vagrants insecure key
  94. config.ssh.insert_key = false
  95. if ($override_disk_size)
  96. unless Vagrant.has_plugin?("vagrant-disksize")
  97. system "vagrant plugin install vagrant-disksize"
  98. end
  99. config.disksize.size = $disk_size
  100. end
  101. (1..$num_instances).each do |i|
  102. config.vm.define vm_name = "%s-%01d" % [$instance_name_prefix, i] do |node|
  103. node.vm.hostname = vm_name
  104. if Vagrant.has_plugin?("vagrant-proxyconf")
  105. node.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  106. node.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  107. node.proxy.no_proxy = $no_proxy
  108. end
  109. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  110. node.vm.provider vmware do |v|
  111. v.vmx['memsize'] = $vm_memory
  112. v.vmx['numvcpus'] = $vm_cpus
  113. end
  114. end
  115. node.vm.provider :virtualbox do |vb|
  116. vb.memory = $vm_memory
  117. vb.cpus = $vm_cpus
  118. vb.gui = $vm_gui
  119. vb.linked_clone = true
  120. vb.customize ["modifyvm", :id, "--vram", "8"] # ubuntu defaults to 256 MB which is a waste of precious RAM
  121. end
  122. node.vm.provider :libvirt do |lv|
  123. lv.memory = $vm_memory
  124. lv.cpus = $vm_cpus
  125. lv.default_prefix = 'kubespray'
  126. # Fix kernel panic on fedora 28
  127. if $os == "fedora"
  128. lv.cpu_mode = "host-passthrough"
  129. end
  130. end
  131. if $kube_node_instances_with_disks
  132. # Libvirt
  133. driverletters = ('a'..'z').to_a
  134. node.vm.provider :libvirt do |lv|
  135. # always make /dev/sd{a/b/c} so that CI can ensure that
  136. # virtualbox and libvirt will have the same devices to use for OSDs
  137. (1..$kube_node_instances_with_disks_number).each do |d|
  138. lv.storage :file, :device => "hd#{driverletters[d]}", :path => "disk-#{i}-#{d}-#{DISK_UUID}.disk", :size => $kube_node_instances_with_disks_size, :bus => "ide"
  139. end
  140. end
  141. end
  142. if $expose_docker_tcp
  143. node.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  144. end
  145. $forwarded_ports.each do |guest, host|
  146. node.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  147. end
  148. node.vm.synced_folder ".", "/vagrant", disabled: false, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] , rsync__exclude: ['.git','venv']
  149. $shared_folders.each do |src, dst|
  150. node.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
  151. end
  152. ip = "#{$subnet}.#{i+100}"
  153. node.vm.network :private_network, ip: ip
  154. # Disable swap for each vm
  155. node.vm.provision "shell", inline: "swapoff -a"
  156. host_vars[vm_name] = {
  157. "ip": ip,
  158. "flannel_interface": "eth1",
  159. "kube_network_plugin": $network_plugin,
  160. "kube_network_plugin_multus": $multi_networking,
  161. "download_run_once": "True",
  162. "download_localhost": "False",
  163. "download_cache_dir": ENV['HOME'] + "/kubespray_cache",
  164. # Make kubespray cache even when download_run_once is false
  165. "download_force_cache": "True",
  166. # Keeping the cache on the nodes can improve provisioning speed while debugging kubespray
  167. "download_keep_remote_cache": "False",
  168. "docker_keepcache": "1",
  169. # These two settings will put kubectl and admin.config in $inventory/artifacts
  170. "kubeconfig_localhost": "True",
  171. "kubectl_localhost": "True",
  172. "local_path_provisioner_enabled": "#{$local_path_provisioner_enabled}",
  173. "local_path_provisioner_claim_root": "#{$local_path_provisioner_claim_root}",
  174. "ansible_ssh_user": SUPPORTED_OS[$os][:user]
  175. }
  176. # Only execute the Ansible provisioner once, when all the machines are up and ready.
  177. if i == $num_instances
  178. node.vm.provision "ansible" do |ansible|
  179. ansible.playbook = $playbook
  180. $ansible_inventory_path = File.join( $inventory, "hosts.ini")
  181. if File.exist?($ansible_inventory_path)
  182. ansible.inventory_path = $ansible_inventory_path
  183. end
  184. ansible.become = true
  185. ansible.limit = "all,localhost"
  186. ansible.host_key_checking = false
  187. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache", "-e ansible_become_pass=vagrant"]
  188. ansible.host_vars = host_vars
  189. #ansible.tags = ['download']
  190. ansible.groups = {
  191. "etcd" => ["#{$instance_name_prefix}-[1:#{$etcd_instances}]"],
  192. "kube-master" => ["#{$instance_name_prefix}-[1:#{$kube_master_instances}]"],
  193. "kube-node" => ["#{$instance_name_prefix}-[1:#{$kube_node_instances}]"],
  194. "k8s-cluster:children" => ["kube-master", "kube-node"],
  195. }
  196. end
  197. end
  198. end
  199. end
  200. end