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.

156 lines
5.7 KiB

  1. # Kubernetes on Openstack with Terraform
  2. Provision a Kubernetes cluster with [Terraform](https://www.terraform.io) on
  3. Openstack.
  4. ## Status
  5. This will install a Kubernetes cluster on an Openstack Cloud. It has been tested on a
  6. OpenStack Cloud provided by [BlueBox](https://www.blueboxcloud.com/) and on OpenStack at [EMBL-EBI's](http://www.ebi.ac.uk/) [EMBASSY Cloud](http://www.embassycloud.org/). This should work on most modern installs of OpenStack that support the basic
  7. services.
  8. There are some assumptions made to try and ensure it will work on your openstack cluster.
  9. * floating-ips are used for access, but you can have masters and nodes that don't use floating-ips if needed. You need currently at least 1 floating ip, which we would suggest is used on a master.
  10. * you already have a suitable OS image in glance
  11. * you already have both an internal network and a floating-ip pool created
  12. * you have security-groups enabled
  13. ## Requirements
  14. - [Install Terraform](https://www.terraform.io/intro/getting-started/install.html)
  15. ## Terraform
  16. Terraform will be used to provision all of the OpenStack resources. It is also used to deploy and provision the software
  17. requirements.
  18. ### Prep
  19. #### OpenStack
  20. Ensure your OpenStack credentials are loaded in environment variables. This can be done by downloading a credentials .rc file from your OpenStack dashboard and sourcing it:
  21. ```
  22. $ source ~/.stackrc
  23. ```
  24. You will need two networks before installing, an internal network and
  25. an external (floating IP Pool) network. The internet network can be shared as
  26. we use security groups to provide network segregation. Due to the many
  27. differences between OpenStack installs the Terraform does not attempt to create
  28. these for you.
  29. By default Terraform will expect that your networks are called `internal` and
  30. `external`. You can change this by altering the Terraform variables `network_name` and `floatingip_pool`. This can be done on a new variables file or through environment variables.
  31. A full list of variables you can change can be found at [variables.tf](variables.tf).
  32. All OpenStack resources will use the Terraform variable `cluster_name` (
  33. default `example`) in their name to make it easier to track. For example the
  34. first compute resource will be named `example-kubernetes-1`.
  35. #### Terraform
  36. Ensure your local ssh-agent is running and your ssh key has been added. This
  37. step is required by the terraform provisioner:
  38. ```
  39. $ eval $(ssh-agent -s)
  40. $ ssh-add ~/.ssh/id_rsa
  41. ```
  42. Ensure that you have your Openstack credentials loaded into Terraform
  43. environment variables. Likely via a command similar to:
  44. ```
  45. $ echo Setting up Terraform creds && \
  46. export TF_VAR_username=${OS_USERNAME} && \
  47. export TF_VAR_password=${OS_PASSWORD} && \
  48. export TF_VAR_tenant=${OS_TENANT_NAME} && \
  49. export TF_VAR_auth_url=${OS_AUTH_URL}
  50. ```
  51. If you want to provision master or node VMs that don't use floating ips, write on a `my-terraform-vars.tfvars` file, for example:
  52. ```
  53. number_of_k8s_masters = "1"
  54. number_of_k8s_masters_no_floating_ip = "2"
  55. number_of_k8s_nodes_no_floating_ip = "1"
  56. number_of_k8s_nodes = "0"
  57. ```
  58. This will provision one VM as master using a floating ip, two additional masters using no floating ips (these will only have private ips inside your tenancy) and one VM as node, again without a floating ip.
  59. # Provision a Kubernetes Cluster on OpenStack
  60. If not using a tfvars file for your setup, then execute:
  61. ```
  62. terraform apply -state=contrib/terraform/openstack/terraform.tfstate contrib/terraform/openstack
  63. openstack_compute_secgroup_v2.k8s_master: Creating...
  64. description: "" => "example - Kubernetes Master"
  65. name: "" => "example-k8s-master"
  66. rule.#: "" => "<computed>"
  67. ...
  68. ...
  69. Apply complete! Resources: 9 added, 0 changed, 0 destroyed.
  70. The state of your infrastructure has been saved to the path
  71. below. This state is required to modify and destroy your
  72. infrastructure, so keep it safe. To inspect the complete state
  73. use the `terraform show` command.
  74. State path: contrib/terraform/openstack/terraform.tfstate
  75. ```
  76. Alternatively, if you wrote your terraform variables on a file `my-terraform-vars.tfvars`, your command would look like:
  77. ```
  78. terraform apply -state=contrib/terraform/openstack/terraform.tfstate -var-file=my-terraform-vars.tfvars contrib/terraform/openstack
  79. ```
  80. if you choose to add masters or nodes without floating ips (only internal ips on your OpenStack tenancy), this script will create as well a file `contrib/terraform/openstack/k8s-cluster.yml` with an ssh command for ansible to be able to access your machines tunneling through the first floating ip used. If you want to manually handling the ssh tunneling to these machines, please delete or move that file. If you want to use this, just leave it there, as ansible will pick it up automatically.
  81. Make sure you can connect to the hosts:
  82. ```
  83. $ ansible -i contrib/terraform/openstack/hosts -m ping all
  84. example-k8s_node-1 | SUCCESS => {
  85. "changed": false,
  86. "ping": "pong"
  87. }
  88. example-etcd-1 | SUCCESS => {
  89. "changed": false,
  90. "ping": "pong"
  91. }
  92. example-k8s-master-1 | SUCCESS => {
  93. "changed": false,
  94. "ping": "pong"
  95. }
  96. ```
  97. if you are deploying a system that needs bootstrapping, like CoreOS, these might have a state `FAILED` due to CoreOS not having python. As long as the state is not `UNREACHABLE`, this is fine.
  98. if it fails try to connect manually via SSH ... it could be somthing as simple as a stale host key.
  99. Deploy kubernetes:
  100. ```
  101. $ ansible-playbook --become -i contrib/terraform/openstack/hosts cluster.yml
  102. ```
  103. # clean up:
  104. ```
  105. $ terraform destroy
  106. Do you really want to destroy?
  107. Terraform will delete all your managed infrastructure.
  108. There is no undo. Only 'yes' will be accepted to confirm.
  109. Enter a value: yes
  110. ...
  111. ...
  112. Apply complete! Resources: 0 added, 0 changed, 12 destroyed.
  113. ```