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.

256 lines
9.6 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 **Identity v2** 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 must set **OS_REGION_NAME** and **OS_TENANT_ID** environment variables not required by openstack CLI
  25. You will need two networks before installing, an internal network and
  26. an external (floating IP Pool) network. The internet network can be shared as
  27. we use security groups to provide network segregation. Due to the many
  28. differences between OpenStack installs the Terraform does not attempt to create
  29. these for you.
  30. By default Terraform will expect that your networks are called `internal` and
  31. `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.
  32. A full list of variables you can change can be found at [variables.tf](variables.tf).
  33. All OpenStack resources will use the Terraform variable `cluster_name` (
  34. default `example`) in their name to make it easier to track. For example the
  35. first compute resource will be named `example-kubernetes-1`.
  36. #### Terraform
  37. Ensure your local ssh-agent is running and your ssh key has been added. This
  38. step is required by the terraform provisioner:
  39. ```
  40. $ eval $(ssh-agent -s)
  41. $ ssh-add ~/.ssh/id_rsa
  42. ```
  43. Ensure that you have your Openstack credentials loaded into Terraform
  44. environment variables. Likely via a command similar to:
  45. ```
  46. $ echo Setting up Terraform creds && \
  47. export TF_VAR_username=${OS_USERNAME} && \
  48. export TF_VAR_password=${OS_PASSWORD} && \
  49. export TF_VAR_tenant=${OS_TENANT_NAME} && \
  50. export TF_VAR_auth_url=${OS_AUTH_URL}
  51. ```
  52. 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:
  53. ```
  54. number_of_k8s_masters = "1"
  55. number_of_k8s_masters_no_floating_ip = "2"
  56. number_of_k8s_nodes_no_floating_ip = "1"
  57. number_of_k8s_nodes = "0"
  58. ```
  59. 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.
  60. Additionally, now the terraform based installation supports provisioning of a GlusterFS shared file system based on a separate set of VMs, running either a Debian or RedHat based set of VMs. To enable this, you need to add to your `my-terraform-vars.tfvars` the following variables:
  61. ```
  62. # Flavour depends on your openstack installation, you can get available flavours through `nova flavor-list`
  63. flavor_gfs_node = "af659280-5b8a-42b5-8865-a703775911da"
  64. # This is the name of an image already available in your openstack installation.
  65. image_gfs = "Ubuntu 15.10"
  66. number_of_gfs_nodes_no_floating_ip = "3"
  67. # This is the size of the non-ephemeral volumes to be attached to store the GlusterFS bricks.
  68. gfs_volume_size_in_gb = "50"
  69. # The user needed for the image choosen for GlusterFS.
  70. ssh_user_gfs = "ubuntu"
  71. ```
  72. If these variables are provided, this will give rise to a new ansible group called `gfs-cluster`, for which we have added ansible roles to execute in the ansible provisioning step. If you are using Container Linux by CoreOS, these GlusterFS VM necessarily need to be either Debian or RedHat based VMs, Container Linux by CoreOS cannot serve GlusterFS, but can connect to it through binaries available on hyperkube v1.4.3_coreos.0 or higher.
  73. # Configure Cluster variables
  74. Edit `inventory/group_vars/all.yml`:
  75. - Set variable **bootstrap_os** according selected image
  76. ```
  77. # Valid bootstrap options (required): ubuntu, coreos, centos, none
  78. bootstrap_os: coreos
  79. ```
  80. - **bin_dir**
  81. ```
  82. # Directory where the binaries will be installed
  83. # Default:
  84. # bin_dir: /usr/local/bin
  85. # For Container Linux by CoreOS:
  86. bin_dir: /opt/bin
  87. ```
  88. - and **cloud_provider**
  89. ```
  90. cloud_provider: openstack
  91. ```
  92. Edit `inventory/group_vars/k8s-cluster.yml`:
  93. - Set variable **kube_network_plugin** according selected networking
  94. ```
  95. # Choose network plugin (calico, weave or flannel)
  96. # Can also be set to 'cloud', which lets the cloud provider setup appropriate routing
  97. kube_network_plugin: flannel
  98. ```
  99. > flannel works out-of-the-box
  100. > calico requires allowing service's and pod's subnets on according OpenStack Neutron ports
  101. - Set variable **resolvconf_mode**
  102. ```
  103. # Can be docker_dns, host_resolvconf or none
  104. # Default:
  105. # resolvconf_mode: docker_dns
  106. # For Container Linux by CoreOS:
  107. resolvconf_mode: host_resolvconf
  108. ```
  109. For calico configure OpenStack Neutron ports: [OpenStack](/docs/openstack.md)
  110. # Provision a Kubernetes Cluster on OpenStack
  111. If not using a tfvars file for your setup, then execute:
  112. ```
  113. terraform apply -state=contrib/terraform/openstack/terraform.tfstate contrib/terraform/openstack
  114. openstack_compute_secgroup_v2.k8s_master: Creating...
  115. description: "" => "example - Kubernetes Master"
  116. name: "" => "example-k8s-master"
  117. rule.#: "" => "<computed>"
  118. ...
  119. ...
  120. Apply complete! Resources: 9 added, 0 changed, 0 destroyed.
  121. The state of your infrastructure has been saved to the path
  122. below. This state is required to modify and destroy your
  123. infrastructure, so keep it safe. To inspect the complete state
  124. use the `terraform show` command.
  125. State path: contrib/terraform/openstack/terraform.tfstate
  126. ```
  127. Alternatively, if you wrote your terraform variables on a file `my-terraform-vars.tfvars`, your command would look like:
  128. ```
  129. terraform apply -state=contrib/terraform/openstack/terraform.tfstate -var-file=my-terraform-vars.tfvars contrib/terraform/openstack
  130. ```
  131. 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.
  132. Make sure you can connect to the hosts:
  133. ```
  134. $ ansible -i contrib/terraform/openstack/hosts -m ping all
  135. example-k8s_node-1 | SUCCESS => {
  136. "changed": false,
  137. "ping": "pong"
  138. }
  139. example-etcd-1 | SUCCESS => {
  140. "changed": false,
  141. "ping": "pong"
  142. }
  143. example-k8s-master-1 | SUCCESS => {
  144. "changed": false,
  145. "ping": "pong"
  146. }
  147. ```
  148. if you are deploying a system that needs bootstrapping, like Container Linux by CoreOS, these might have a state `FAILED` due to Container Linux by CoreOS not having python. As long as the state is not `UNREACHABLE`, this is fine.
  149. if it fails try to connect manually via SSH ... it could be somthing as simple as a stale host key.
  150. Deploy kubernetes:
  151. ```
  152. $ ansible-playbook --become -i contrib/terraform/openstack/hosts cluster.yml
  153. ```
  154. # Set up local kubectl
  155. 1. Install kubectl on your workstation:
  156. [Install and Set Up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
  157. 2. Add route to internal IP of master node (if needed):
  158. ```
  159. sudo route add [master-internal-ip] gw [router-ip]
  160. ```
  161. or
  162. ```
  163. sudo route add -net [internal-subnet]/24 gw [router-ip]
  164. ```
  165. 3. List Kubernetes certs&keys:
  166. ```
  167. ssh [os-user]@[master-ip] sudo ls /etc/kubernetes/ssl/
  168. ```
  169. 4. Get admin's certs&key:
  170. ```
  171. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/admin-[cluster_name]-k8s-master-1-key.pem > admin-key.pem
  172. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/admin-[cluster_name]-k8s-master-1.pem > admin.pem
  173. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/ca.pem > ca.pem
  174. ```
  175. 5. Edit OpenStack Neutron master's Security Group to allow TCP connections to port 6443
  176. 6. Configure kubectl:
  177. ```
  178. kubectl config set-cluster default-cluster --server=https://[master-internal-ip]:6443 \
  179. --certificate-authority=ca.pem
  180. kubectl config set-credentials default-admin \
  181. --certificate-authority=ca.pem \
  182. --client-key=admin-key.pem \
  183. --client-certificate=admin.pem
  184. kubectl config set-context default-system --cluster=default-cluster --user=default-admin
  185. kubectl config use-context default-system
  186. ```
  187. 7. Check it:
  188. ```
  189. kubectl version
  190. ```
  191. # What's next
  192. [Start Hello Kubernetes Service](https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/)
  193. # clean up:
  194. ```
  195. $ terraform destroy
  196. Do you really want to destroy?
  197. Terraform will delete all your managed infrastructure.
  198. There is no undo. Only 'yes' will be accepted to confirm.
  199. Enter a value: yes
  200. ...
  201. ...
  202. Apply complete! Resources: 0 added, 0 changed, 12 destroyed.
  203. ```