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.

133 lines
8.1 KiB

  1. # Vagrant
  2. Assuming you have Vagrant 2.0+ installed with virtualbox, libvirt/qemu or vmware, but is untested) you should be able to launch a 3 node Kubernetes cluster by simply running `vagrant up`. This will spin up 3 VMs and install kubernetes on them. Once they are completed you can connect to any of them by running `vagrant ssh k8s-[1..3]`.
  3. To give an estimate of the expected duration of a provisioning run: On a dual core i5-6300u laptop with an SSD, provisioning takes around 13 to 15 minutes, once the container images and other files are cached. Note that libvirt/qemu is recommended over virtualbox as it is quite a bit faster, especially during boot-up time.
  4. For proper performance a minimum of 12GB RAM is recommended. It is possible to run a 3 node cluster on a laptop with 8GB of RAM using the default Vagrantfile, provided you have 8GB zram swap configured and not much more than a browser and a mail client running. If you decide to run on such a machine, then also make sure that any tmpfs devices, that are mounted, are mostly empty and disable any swapfiles mounted on HDD/SSD or you will be in for some serious swap-madness. Things can get a bit sluggish during provisioning, but when that's done, the system will actually be able to perform quite well.
  5. ## Customize Vagrant
  6. You can override the default settings in the `Vagrantfile` either by directly modifying the `Vagrantfile` or through an override file. In the same directory as the `Vagrantfile`, create a folder called `vagrant` and create `config.rb` file in it. An example of how to configure this file is given below.
  7. ## Use alternative OS for Vagrant
  8. By default, Vagrant uses Ubuntu 18.04 box to provision a local cluster. You may use an alternative supported operating system for your local cluster.
  9. Customize `$os` variable in `Vagrantfile` or as override, e.g.,:
  10. ```ShellSession
  11. echo '$os = "flatcar-stable"' >> vagrant/config.rb
  12. ```
  13. The supported operating systems for vagrant are defined in the `SUPPORTED_OS` constant in the `Vagrantfile`.
  14. ## File and image caching
  15. Kubespray can take quite a while to start on a laptop. To improve provisioning speed, the variable 'download_run_once' is set. This will make kubespray download all files and containers just once and then redistributes them to the other nodes and as a bonus, also cache all downloads locally and re-use them on the next provisioning run. For more information on download settings see [download documentation](/docs/downloads.md).
  16. ## Example use of Vagrant
  17. The following is an example of setting up and running kubespray using `vagrant`. For repeated runs, you could save the script to a file in the root of the kubespray and run it by executing 'source <name_of_the_file>.
  18. ```ShellSession
  19. # use virtualenv to install all python requirements
  20. VENVDIR=venv
  21. virtualenv --python=/usr/bin/python3.7 $VENVDIR
  22. source $VENVDIR/bin/activate
  23. pip install -r requirements.txt
  24. # prepare an inventory to test with
  25. INV=inventory/my_lab
  26. rm -rf ${INV}.bak &> /dev/null
  27. mv ${INV} ${INV}.bak &> /dev/null
  28. cp -a inventory/sample ${INV}
  29. rm -f ${INV}/hosts.ini
  30. # customize the vagrant environment
  31. mkdir vagrant
  32. cat << EOF > vagrant/config.rb
  33. \$instance_name_prefix = "kub"
  34. \$vm_cpus = 1
  35. \$num_instances = 3
  36. \$os = "centos-bento"
  37. \$subnet = "10.0.20"
  38. \$network_plugin = "flannel"
  39. \$inventory = "$INV"
  40. \$shared_folders = { 'temp/docker_rpms' => "/var/cache/yum/x86_64/7/docker-ce/packages" }
  41. EOF
  42. # make the rpm cache
  43. mkdir -p temp/docker_rpms
  44. vagrant up
  45. # make a copy of the downloaded docker rpm, to speed up the next provisioning run
  46. scp kub-1:/var/cache/yum/x86_64/7/docker-ce/packages/* temp/docker_rpms/
  47. # copy kubectl access configuration in place
  48. mkdir $HOME/.kube/ &> /dev/null
  49. ln -s $PWD/$INV/artifacts/admin.conf $HOME/.kube/config
  50. # make the kubectl binary available
  51. sudo ln -s $PWD/$INV/artifacts/kubectl /usr/local/bin/kubectl
  52. #or
  53. export PATH=$PATH:$PWD/$INV/artifacts
  54. ```
  55. If a vagrant run failed and you've made some changes to fix the issue causing the fail, here is how you would re-run ansible:
  56. ```ShellSession
  57. ansible-playbook -vvv -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory cluster.yml
  58. ```
  59. If all went well, you check if it's all working as expected:
  60. ```ShellSession
  61. kubectl get nodes
  62. ```
  63. The output should look like this:
  64. ```ShellSession
  65. $ kubectl get nodes
  66. NAME STATUS ROLES AGE VERSION
  67. kub-1 Ready control-plane,master 4m37s v1.22.5
  68. kub-2 Ready control-plane,master 4m7s v1.22.5
  69. kub-3 Ready <none> 3m7s v1.22.5
  70. ```
  71. Another nice test is the following:
  72. ```ShellSession
  73. kubectl get pods --all-namespaces -o wide
  74. ```
  75. Which should yield something like the following:
  76. ```ShellSession
  77. $ kubectl get pods --all-namespaces -o wide
  78. NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  79. kube-system coredns-8474476ff8-m2469 1/1 Running 0 2m45s 10.233.65.2 kub-2 <none> <none>
  80. kube-system coredns-8474476ff8-v5wzj 1/1 Running 0 2m41s 10.233.64.3 kub-1 <none> <none>
  81. kube-system dns-autoscaler-5ffdc7f89d-76tnv 1/1 Running 0 2m43s 10.233.64.2 kub-1 <none> <none>
  82. kube-system kube-apiserver-kub-1 1/1 Running 1 4m54s 10.0.20.101 kub-1 <none> <none>
  83. kube-system kube-apiserver-kub-2 1/1 Running 1 4m33s 10.0.20.102 kub-2 <none> <none>
  84. kube-system kube-controller-manager-kub-1 1/1 Running 1 5m1s 10.0.20.101 kub-1 <none> <none>
  85. kube-system kube-controller-manager-kub-2 1/1 Running 1 4m33s 10.0.20.102 kub-2 <none> <none>
  86. kube-system kube-flannel-9xgf5 1/1 Running 0 3m10s 10.0.20.102 kub-2 <none> <none>
  87. kube-system kube-flannel-l8jbl 1/1 Running 0 3m10s 10.0.20.101 kub-1 <none> <none>
  88. kube-system kube-flannel-zss4t 1/1 Running 0 3m10s 10.0.20.103 kub-3 <none> <none>
  89. kube-system kube-multus-ds-amd64-bhpc9 1/1 Running 0 3m2s 10.0.20.103 kub-3 <none> <none>
  90. kube-system kube-multus-ds-amd64-n6vl8 1/1 Running 0 3m2s 10.0.20.102 kub-2 <none> <none>
  91. kube-system kube-multus-ds-amd64-qttgs 1/1 Running 0 3m2s 10.0.20.101 kub-1 <none> <none>
  92. kube-system kube-proxy-2x4jl 1/1 Running 0 3m33s 10.0.20.101 kub-1 <none> <none>
  93. kube-system kube-proxy-d48r7 1/1 Running 0 3m33s 10.0.20.103 kub-3 <none> <none>
  94. kube-system kube-proxy-f45lp 1/1 Running 0 3m33s 10.0.20.102 kub-2 <none> <none>
  95. kube-system kube-scheduler-kub-1 1/1 Running 1 4m54s 10.0.20.101 kub-1 <none> <none>
  96. kube-system kube-scheduler-kub-2 1/1 Running 1 4m33s 10.0.20.102 kub-2 <none> <none>
  97. kube-system nginx-proxy-kub-3 1/1 Running 0 3m33s 10.0.20.103 kub-3 <none> <none>
  98. kube-system nodelocaldns-cg9tz 1/1 Running 0 2m41s 10.0.20.102 kub-2 <none> <none>
  99. kube-system nodelocaldns-htswt 1/1 Running 0 2m41s 10.0.20.103 kub-3 <none> <none>
  100. kube-system nodelocaldns-nsp7s 1/1 Running 0 2m41s 10.0.20.101 kub-1 <none> <none>
  101. local-path-storage local-path-provisioner-66df45bfdd-km4zg 1/1 Running 0 2m54s 10.233.66.2 kub-3 <none> <none>
  102. ```