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.

253 lines
10 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
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. FLATCAR_URL_TEMPLATE = "https://%s.release.flatcar-linux.net/amd64-usr/current/flatcar_production_vagrant.json"
  8. # Uniq disk UUID for libvirt
  9. DISK_UUID = Time.now.utc.to_i
  10. SUPPORTED_OS = {
  11. "flatcar-stable" => {box: "flatcar-stable", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["stable"]},
  12. "flatcar-beta" => {box: "flatcar-beta", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["beta"]},
  13. "flatcar-alpha" => {box: "flatcar-alpha", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["alpha"]},
  14. "flatcar-edge" => {box: "flatcar-edge", user: "core", box_url: FLATCAR_URL_TEMPLATE % ["edge"]},
  15. "ubuntu1604" => {box: "generic/ubuntu1604", user: "vagrant"},
  16. "ubuntu1804" => {box: "generic/ubuntu1804", user: "vagrant"},
  17. "ubuntu2004" => {box: "generic/ubuntu2004", user: "vagrant"},
  18. "centos" => {box: "centos/7", user: "vagrant"},
  19. "centos-bento" => {box: "bento/centos-7.6", user: "vagrant"},
  20. "centos8" => {box: "centos/8", user: "vagrant"},
  21. "centos8-bento" => {box: "bento/centos-8", user: "vagrant"},
  22. "fedora32" => {box: "fedora/32-cloud-base", user: "vagrant"},
  23. "fedora33" => {box: "fedora/33-cloud-base", user: "vagrant"},
  24. "opensuse" => {box: "bento/opensuse-leap-15.1", user: "vagrant"},
  25. "opensuse-tumbleweed" => {box: "opensuse/Tumbleweed.x86_64", user: "vagrant"},
  26. "oraclelinux" => {box: "generic/oracle7", user: "vagrant"},
  27. "oraclelinux8" => {box: "generic/oracle8", user: "vagrant"},
  28. "rhel7" => {box: "generic/rhel7", user: "vagrant"},
  29. "rhel8" => {box: "generic/rhel8", user: "vagrant"},
  30. }
  31. if File.exist?(CONFIG)
  32. require CONFIG
  33. end
  34. # Defaults for config options defined in CONFIG
  35. $num_instances ||= 3
  36. $instance_name_prefix ||= "k8s"
  37. $vm_gui ||= false
  38. $vm_memory ||= 2048
  39. $vm_cpus ||= 2
  40. $shared_folders ||= {}
  41. $forwarded_ports ||= {}
  42. $subnet ||= "172.18.8"
  43. $os ||= "ubuntu1804"
  44. $network_plugin ||= "flannel"
  45. # Setting multi_networking to true will install Multus: https://github.com/intel/multus-cni
  46. $multi_networking ||= false
  47. $download_run_once ||= "True"
  48. $download_force_cache ||= "True"
  49. # The first three nodes are etcd servers
  50. $etcd_instances ||= $num_instances
  51. # The first two nodes are kube masters
  52. $kube_master_instances ||= $num_instances == 1 ? $num_instances : ($num_instances - 1)
  53. # All nodes are kube nodes
  54. $kube_node_instances ||= $num_instances
  55. # The following only works when using the libvirt provider
  56. $kube_node_instances_with_disks ||= false
  57. $kube_node_instances_with_disks_size ||= "20G"
  58. $kube_node_instances_with_disks_number ||= 2
  59. $override_disk_size ||= false
  60. $disk_size ||= "20GB"
  61. $local_path_provisioner_enabled ||= false
  62. $local_path_provisioner_claim_root ||= "/opt/local-path-provisioner/"
  63. $libvirt_nested ||= false
  64. $playbook ||= "cluster.yml"
  65. host_vars = {}
  66. $box = SUPPORTED_OS[$os][:box]
  67. # if $inventory is not set, try to use example
  68. $inventory = "inventory/sample" if ! $inventory
  69. $inventory = File.absolute_path($inventory, File.dirname(__FILE__))
  70. # if $inventory has a hosts.ini file use it, otherwise copy over
  71. # vars etc to where vagrant expects dynamic inventory to be
  72. if ! File.exist?(File.join(File.dirname($inventory), "hosts.ini"))
  73. $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", "provisioners", "ansible")
  74. FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
  75. if ! File.exist?(File.join($vagrant_ansible,"inventory"))
  76. FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
  77. end
  78. end
  79. if Vagrant.has_plugin?("vagrant-proxyconf")
  80. $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
  81. (1..$num_instances).each do |i|
  82. $no_proxy += ",#{$subnet}.#{i+100}"
  83. end
  84. end
  85. Vagrant.configure("2") do |config|
  86. config.vm.box = $box
  87. if SUPPORTED_OS[$os].has_key? :box_url
  88. config.vm.box_url = SUPPORTED_OS[$os][:box_url]
  89. end
  90. config.ssh.username = SUPPORTED_OS[$os][:user]
  91. # plugin conflict
  92. if Vagrant.has_plugin?("vagrant-vbguest") then
  93. config.vbguest.auto_update = false
  94. end
  95. # always use Vagrants insecure key
  96. config.ssh.insert_key = false
  97. if ($override_disk_size)
  98. unless Vagrant.has_plugin?("vagrant-disksize")
  99. system "vagrant plugin install vagrant-disksize"
  100. end
  101. config.disksize.size = $disk_size
  102. end
  103. (1..$num_instances).each do |i|
  104. config.vm.define vm_name = "%s-%01d" % [$instance_name_prefix, i] do |node|
  105. node.vm.hostname = vm_name
  106. if Vagrant.has_plugin?("vagrant-proxyconf")
  107. node.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
  108. node.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
  109. node.proxy.no_proxy = $no_proxy
  110. end
  111. ["vmware_fusion", "vmware_workstation"].each do |vmware|
  112. node.vm.provider vmware do |v|
  113. v.vmx['memsize'] = $vm_memory
  114. v.vmx['numvcpus'] = $vm_cpus
  115. end
  116. end
  117. node.vm.provider :virtualbox do |vb|
  118. vb.memory = $vm_memory
  119. vb.cpus = $vm_cpus
  120. vb.gui = $vm_gui
  121. vb.linked_clone = true
  122. vb.customize ["modifyvm", :id, "--vram", "8"] # ubuntu defaults to 256 MB which is a waste of precious RAM
  123. vb.customize ["modifyvm", :id, "--audio", "none"]
  124. end
  125. node.vm.provider :libvirt do |lv|
  126. lv.nested = $libvirt_nested
  127. lv.cpu_mode = "host-model"
  128. lv.memory = $vm_memory
  129. lv.cpus = $vm_cpus
  130. lv.default_prefix = 'kubespray'
  131. # Fix kernel panic on fedora 28
  132. if $os == "fedora"
  133. lv.cpu_mode = "host-passthrough"
  134. end
  135. end
  136. if $kube_node_instances_with_disks
  137. # Libvirt
  138. driverletters = ('a'..'z').to_a
  139. node.vm.provider :libvirt do |lv|
  140. # always make /dev/sd{a/b/c} so that CI can ensure that
  141. # virtualbox and libvirt will have the same devices to use for OSDs
  142. (1..$kube_node_instances_with_disks_number).each do |d|
  143. lv.storage :file, :device => "hd#{driverletters[d]}", :path => "disk-#{i}-#{d}-#{DISK_UUID}.disk", :size => $kube_node_instances_with_disks_size, :bus => "ide"
  144. end
  145. end
  146. end
  147. if $expose_docker_tcp
  148. node.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
  149. end
  150. $forwarded_ports.each do |guest, host|
  151. node.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
  152. end
  153. if ["rhel7","rhel8"].include? $os
  154. # Vagrant synced_folder rsync options cannot be used for RHEL boxes as Rsync package cannot
  155. # be installed until the host is registered with a valid Red Hat support subscription
  156. node.vm.synced_folder ".", "/vagrant", disabled: false
  157. $shared_folders.each do |src, dst|
  158. node.vm.synced_folder src, dst
  159. end
  160. else
  161. node.vm.synced_folder ".", "/vagrant", disabled: false, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] , rsync__exclude: ['.git','venv']
  162. $shared_folders.each do |src, dst|
  163. node.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
  164. end
  165. end
  166. ip = "#{$subnet}.#{i+100}"
  167. node.vm.network :private_network, ip: ip
  168. # Disable swap for each vm
  169. node.vm.provision "shell", inline: "swapoff -a"
  170. # Disable firewalld on oraclelinux/redhat vms
  171. if ["oraclelinux","oraclelinux8","rhel7","rhel8"].include? $os
  172. node.vm.provision "shell", inline: "systemctl stop firewalld; systemctl disable firewalld"
  173. end
  174. host_vars[vm_name] = {
  175. "ip": ip,
  176. "flannel_interface": "eth1",
  177. "kube_network_plugin": $network_plugin,
  178. "kube_network_plugin_multus": $multi_networking,
  179. "download_run_once": $download_run_once,
  180. "download_localhost": "False",
  181. "download_cache_dir": ENV['HOME'] + "/kubespray_cache",
  182. # Make kubespray cache even when download_run_once is false
  183. "download_force_cache": $download_force_cache,
  184. # Keeping the cache on the nodes can improve provisioning speed while debugging kubespray
  185. "download_keep_remote_cache": "False",
  186. "docker_rpm_keepcache": "1",
  187. # These two settings will put kubectl and admin.config in $inventory/artifacts
  188. "kubeconfig_localhost": "True",
  189. "kubectl_localhost": "True",
  190. "local_path_provisioner_enabled": "#{$local_path_provisioner_enabled}",
  191. "local_path_provisioner_claim_root": "#{$local_path_provisioner_claim_root}",
  192. "ansible_ssh_user": SUPPORTED_OS[$os][:user]
  193. }
  194. # Only execute the Ansible provisioner once, when all the machines are up and ready.
  195. if i == $num_instances
  196. node.vm.provision "ansible" do |ansible|
  197. ansible.playbook = $playbook
  198. $ansible_inventory_path = File.join( $inventory, "hosts.ini")
  199. if File.exist?($ansible_inventory_path)
  200. ansible.inventory_path = $ansible_inventory_path
  201. end
  202. ansible.become = true
  203. ansible.limit = "all,localhost"
  204. ansible.host_key_checking = false
  205. ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache", "-e ansible_become_pass=vagrant"]
  206. ansible.host_vars = host_vars
  207. #ansible.tags = ['download']
  208. ansible.groups = {
  209. "etcd" => ["#{$instance_name_prefix}-[1:#{$etcd_instances}]"],
  210. "kube-master" => ["#{$instance_name_prefix}-[1:#{$kube_master_instances}]"],
  211. "kube-node" => ["#{$instance_name_prefix}-[1:#{$kube_node_instances}]"],
  212. "k8s-cluster:children" => ["kube-master", "kube-node"],
  213. }
  214. end
  215. end
  216. end
  217. end
  218. end