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.

243 lines
8.6 KiB

  1. # Kubernetes on Equinix Metal with Terraform
  2. Provision a Kubernetes cluster with [Terraform](https://www.terraform.io) on
  3. [Equinix Metal](https://metal.equinix.com) ([formerly Packet](https://blog.equinix.com/blog/2020/10/06/equinix-metal-metal-and-more/)).
  4. ## Status
  5. This will install a Kubernetes cluster on Equinix Metal. It should work in all locations and on most server types.
  6. ## Approach
  7. The terraform configuration inspects variables found in
  8. [variables.tf](variables.tf) to create resources in your Equinix Metal project.
  9. There is a [python script](../terraform.py) that reads the generated`.tfstate`
  10. file to generate a dynamic inventory that is consumed by [cluster.yml](../../..//cluster.yml)
  11. to actually install Kubernetes with Kubespray.
  12. ### Kubernetes Nodes
  13. You can create many different kubernetes topologies by setting the number of
  14. different classes of hosts.
  15. - Master nodes with etcd: `number_of_k8s_masters` variable
  16. - Master nodes without etcd: `number_of_k8s_masters_no_etcd` variable
  17. - Standalone etcd hosts: `number_of_etcd` variable
  18. - Kubernetes worker nodes: `number_of_k8s_nodes` variable
  19. Note that the Ansible script will report an invalid configuration if you wind up
  20. with an *even number* of etcd instances since that is not a valid configuration. This
  21. restriction includes standalone etcd nodes that are deployed in a cluster along with
  22. master nodes with etcd replicas. As an example, if you have three master nodes with
  23. etcd replicas and three standalone etcd nodes, the script will fail since there are
  24. now six total etcd replicas.
  25. ## Requirements
  26. - [Install Terraform](https://www.terraform.io/intro/getting-started/install.html)
  27. - Install dependencies: `sudo pip install -r requirements.txt`
  28. - Account with Equinix Metal
  29. - An SSH key pair
  30. ## SSH Key Setup
  31. An SSH keypair is required so Ansible can access the newly provisioned nodes (Equinix Metal hosts). By default, the public SSH key defined in cluster.tfvars will be installed in authorized_key on the newly provisioned nodes (~/.ssh/id_rsa.pub). Terraform will upload this public key and then it will be distributed out to all the nodes. If you have already set this public key in Equinix Metal (i.e. via the portal), then set the public keyfile name in cluster.tfvars to blank to prevent the duplicate key from being uploaded which will cause an error.
  32. If you don't already have a keypair generated (~/.ssh/id_rsa and ~/.ssh/id_rsa.pub), then a new keypair can be generated with the command:
  33. ```ShellSession
  34. ssh-keygen -f ~/.ssh/id_rsa
  35. ```
  36. ## Terraform
  37. Terraform will be used to provision all of the Equinix Metal resources with base software as appropriate.
  38. ### Configuration
  39. #### Inventory files
  40. Create an inventory directory for your cluster by copying the existing sample and linking the `hosts` script (used to build the inventory based on Terraform state):
  41. ```ShellSession
  42. cp -LRp contrib/terraform/metal/sample-inventory inventory/$CLUSTER
  43. cd inventory/$CLUSTER
  44. ln -s ../../contrib/terraform/metal/hosts
  45. ```
  46. This will be the base for subsequent Terraform commands.
  47. #### Equinix Metal API access
  48. Your Equinix Metal API key must be available in the `PACKET_AUTH_TOKEN` environment variable.
  49. This key is typically stored outside of the code repo since it is considered secret.
  50. If someone gets this key, they can startup/shutdown hosts in your project!
  51. For more information on how to generate an API key or find your project ID, please see
  52. [Accounts Index](https://metal.equinix.com/developers/docs/accounts/).
  53. The Equinix Metal Project ID associated with the key will be set later in `cluster.tfvars`.
  54. For more information about the API, please see [Equinix Metal API](https://metal.equinix.com/developers/api/).
  55. Example:
  56. ```ShellSession
  57. export PACKET_AUTH_TOKEN="Example-API-Token"
  58. ```
  59. Note that to deploy several clusters within the same project you need to use [terraform workspace](https://www.terraform.io/docs/state/workspaces.html#using-workspaces).
  60. #### Cluster variables
  61. The construction of the cluster is driven by values found in
  62. [variables.tf](variables.tf).
  63. For your cluster, edit `inventory/$CLUSTER/cluster.tfvars`.
  64. The `cluster_name` is used to set a tag on each server deployed as part of this cluster.
  65. This helps when identifying which hosts are associated with each cluster.
  66. While the defaults in variables.tf will successfully deploy a cluster, it is recommended to set the following values:
  67. - cluster_name = the name of the inventory directory created above as $CLUSTER
  68. - metal_project_id = the Equinix Metal Project ID associated with the Equinix Metal API token above
  69. #### Enable localhost access
  70. Kubespray will pull down a Kubernetes configuration file to access this cluster by enabling the
  71. `kubeconfig_localhost: true` in the Kubespray configuration.
  72. Edit `inventory/$CLUSTER/group_vars/k8s_cluster/k8s_cluster.yml` and comment back in the following line and change from `false` to `true`:
  73. `\# kubeconfig_localhost: false`
  74. becomes:
  75. `kubeconfig_localhost: true`
  76. Once the Kubespray playbooks are run, a Kubernetes configuration file will be written to the local host at `inventory/$CLUSTER/artifacts/admin.conf`
  77. #### Terraform state files
  78. In the cluster's inventory folder, the following files might be created (either by Terraform
  79. or manually), to prevent you from pushing them accidentally they are in a
  80. `.gitignore` file in the `terraform/metal` directory :
  81. - `.terraform`
  82. - `.tfvars`
  83. - `.tfstate`
  84. - `.tfstate.backup`
  85. You can still add them manually if you want to.
  86. ### Initialization
  87. Before Terraform can operate on your cluster you need to install the required
  88. plugins. This is accomplished as follows:
  89. ```ShellSession
  90. cd inventory/$CLUSTER
  91. terraform init ../../contrib/terraform/metal
  92. ```
  93. This should finish fairly quickly telling you Terraform has successfully initialized and loaded necessary modules.
  94. ### Provisioning cluster
  95. You can apply the Terraform configuration to your cluster with the following command
  96. issued from your cluster's inventory directory (`inventory/$CLUSTER`):
  97. ```ShellSession
  98. terraform apply -var-file=cluster.tfvars ../../contrib/terraform/metal
  99. export ANSIBLE_HOST_KEY_CHECKING=False
  100. ansible-playbook -i hosts ../../cluster.yml
  101. ```
  102. ### Destroying cluster
  103. You can destroy your new cluster with the following command issued from the cluster's inventory directory:
  104. ```ShellSession
  105. terraform destroy -var-file=cluster.tfvars ../../contrib/terraform/metal
  106. ```
  107. If you've started the Ansible run, it may also be a good idea to do some manual cleanup:
  108. - Remove SSH keys from the destroyed cluster from your `~/.ssh/known_hosts` file
  109. - Clean up any temporary cache files: `rm /tmp/$CLUSTER-*`
  110. ### Debugging
  111. You can enable debugging output from Terraform by setting `TF_LOG` to `DEBUG` before running the Terraform command.
  112. ## Ansible
  113. ### Node access
  114. #### SSH
  115. Ensure your local ssh-agent is running and your ssh key has been added. This
  116. step is required by the terraform provisioner:
  117. ```ShellSession
  118. eval $(ssh-agent -s)
  119. ssh-add ~/.ssh/id_rsa
  120. ```
  121. If you have deployed and destroyed a previous iteration of your cluster, you will need to clear out any stale keys from your SSH "known hosts" file ( `~/.ssh/known_hosts`).
  122. #### Test access
  123. Make sure you can connect to the hosts. Note that Flatcar Container Linux by Kinvolk will have a state `FAILED` due to Python not being present. This is okay, because Python will be installed during bootstrapping, so long as the hosts are not `UNREACHABLE`.
  124. ```ShellSession
  125. $ ansible -i inventory/$CLUSTER/hosts -m ping all
  126. example-k8s_node-1 | SUCCESS => {
  127. "changed": false,
  128. "ping": "pong"
  129. }
  130. example-etcd-1 | SUCCESS => {
  131. "changed": false,
  132. "ping": "pong"
  133. }
  134. example-k8s-master-1 | SUCCESS => {
  135. "changed": false,
  136. "ping": "pong"
  137. }
  138. ```
  139. If it fails try to connect manually via SSH. It could be something as simple as a stale host key.
  140. ### Deploy Kubernetes
  141. ```ShellSession
  142. ansible-playbook --become -i inventory/$CLUSTER/hosts cluster.yml
  143. ```
  144. This will take some time as there are many tasks to run.
  145. ## Kubernetes
  146. ### Set up kubectl
  147. - [Install kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) on the localhost.
  148. - Verify that Kubectl runs correctly
  149. ```ShellSession
  150. kubectl version
  151. ```
  152. - Verify that the Kubernetes configuration file has been copied over
  153. ```ShellSession
  154. cat inventory/alpha/$CLUSTER/admin.conf
  155. ```
  156. - Verify that all the nodes are running correctly.
  157. ```ShellSession
  158. kubectl version
  159. kubectl --kubeconfig=inventory/$CLUSTER/artifacts/admin.conf get nodes
  160. ```
  161. ## What's next
  162. Try out your new Kubernetes cluster with the [Hello Kubernetes service](https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/).