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.

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