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.

121 lines
3.8 KiB

  1. # Kubernetes on Hetzner with Terraform
  2. Provision a Kubernetes cluster on [Hetzner](https://www.hetzner.com/cloud) using Terraform and Kubespray
  3. ## Overview
  4. The setup looks like following
  5. ```text
  6. Kubernetes cluster
  7. +--------------------------+
  8. | +--------------+ |
  9. | | +--------------+ |
  10. | --> | | | |
  11. | | | Master/etcd | |
  12. | | | node(s) | |
  13. | +-+ | |
  14. | +--------------+ |
  15. | ^ |
  16. | | |
  17. | v |
  18. | +--------------+ |
  19. | | +--------------+ |
  20. | --> | | | |
  21. | | | Worker | |
  22. | | | node(s) | |
  23. | +-+ | |
  24. | +--------------+ |
  25. +--------------------------+
  26. ```
  27. The nodes uses a private network for node to node communication and a public interface for all external communication.
  28. ## Requirements
  29. * Terraform 0.14.0 or newer
  30. ## Quickstart
  31. NOTE: Assumes you are at the root of the kubespray repo.
  32. For authentication in your cluster you can use the environment variables.
  33. ```bash
  34. export HCLOUD_TOKEN=api-token
  35. ```
  36. Copy the cluster configuration file.
  37. ```bash
  38. CLUSTER=my-hetzner-cluster
  39. cp -r inventory/sample inventory/$CLUSTER
  40. cp contrib/terraform/hetzner/default.tfvars inventory/$CLUSTER/
  41. cd inventory/$CLUSTER
  42. ```
  43. Edit `default.tfvars` to match your requirement.
  44. Flatcar Container Linux instead of the basic Hetzner Images.
  45. ```bash
  46. cd ../../contrib/terraform/hetzner
  47. ```
  48. Edit `main.tf` and reactivate the module `source = "./modules/kubernetes-cluster-flatcar"`and
  49. comment out the `#source = "./modules/kubernetes-cluster"`.
  50. activate `ssh_private_key_path = var.ssh_private_key_path`. The VM boots into
  51. Rescue-Mode with the selected image of the `var.machines` but installs Flatcar instead.
  52. Run Terraform to create the infrastructure.
  53. ```bash
  54. cd ./kubespray
  55. terraform -chdir=./contrib/terraform/hetzner/ init
  56. terraform -chdir=./contrib/terraform/hetzner/ apply --var-file=../../../inventory/$CLUSTER/default.tfvars
  57. ```
  58. You should now have a inventory file named `inventory.ini` that you can use with kubespray.
  59. You can use the inventory file with kubespray to set up a cluster.
  60. It is a good idea to check that you have basic SSH connectivity to the nodes. You can do that by:
  61. ```bash
  62. ansible -i inventory.ini -m ping all
  63. ```
  64. You can setup Kubernetes with kubespray using the generated inventory:
  65. ```bash
  66. ansible-playbook -i inventory.ini ../../cluster.yml -b -v
  67. ```
  68. ## Cloud controller
  69. For better support with the cloud you can install the [hcloud cloud controller](https://github.com/hetznercloud/hcloud-cloud-controller-manager) and [CSI driver](https://github.com/hetznercloud/csi-driver).
  70. Please read the instructions in both repos on how to install it.
  71. ## Teardown
  72. You can teardown your infrastructure using the following Terraform command:
  73. ```bash
  74. terraform destroy --var-file default.tfvars ../../contrib/terraform/hetzner
  75. ```
  76. ## Variables
  77. * `prefix`: Prefix to add to all resources, if set to "" don't set any prefix
  78. * `ssh_public_keys`: List of public SSH keys to install on all machines
  79. * `zone`: The zone where to run the cluster
  80. * `network_zone`: the network zone where the cluster is running
  81. * `machines`: Machines to provision. Key of this object will be used as the name of the machine
  82. * `node_type`: The role of this node *(master|worker)*
  83. * `size`: Size of the VM
  84. * `image`: The image to use for the VM
  85. * `ssh_whitelist`: List of IP ranges (CIDR) that will be allowed to ssh to the nodes
  86. * `api_server_whitelist`: List of IP ranges (CIDR) that will be allowed to connect to the API server
  87. * `nodeport_whitelist`: List of IP ranges (CIDR) that will be allowed to connect to the kubernetes nodes on port 30000-32767 (kubernetes nodeports)
  88. * `ingress_whitelist`: List of IP ranges (CIDR) that will be allowed to connect to kubernetes workers on port 80 and 443