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.

495 lines
18 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 should work on
  6. most modern installs of OpenStack that support the basic services.
  7. ### Known compatible public clouds
  8. - [Auro](https://auro.io/)
  9. - [Betacloud](https://www.betacloud.io/)
  10. - [CityCloud](https://www.citycloud.com/)
  11. - [DreamHost](https://www.dreamhost.com/cloud/computing/)
  12. - [ELASTX](https://elastx.se/)
  13. - [EnterCloudSuite](https://www.entercloudsuite.com/)
  14. - [FugaCloud](https://fuga.cloud/)
  15. - [Open Telekom Cloud](https://cloud.telekom.de/) : requires to set the variable `wait_for_floatingip = "true"` in your cluster.tfvars
  16. - [OVH](https://www.ovh.com/)
  17. - [Rackspace](https://www.rackspace.com/)
  18. - [Ultimum](https://ultimum.io/)
  19. - [VexxHost](https://vexxhost.com/)
  20. - [Zetta](https://www.zetta.io/)
  21. ## Approach
  22. The terraform configuration inspects variables found in
  23. [variables.tf](variables.tf) to create resources in your OpenStack cluster.
  24. There is a [python script](../terraform.py) that reads the generated`.tfstate`
  25. file to generate a dynamic inventory that is consumed by the main ansible script
  26. to actually install kubernetes and stand up the cluster.
  27. ### Networking
  28. The configuration includes creating a private subnet with a router to the
  29. external net. It will allocate floating IPs from a pool and assign them to the
  30. hosts where that makes sense. You have the option of creating bastion hosts
  31. inside the private subnet to access the nodes there. Alternatively, a node with
  32. a floating IP can be used as a jump host to nodes without.
  33. ### Kubernetes Nodes
  34. You can create many different kubernetes topologies by setting the number of
  35. different classes of hosts. For each class there are options for allocating
  36. floating IP addresses or not.
  37. - Master nodes with etcd
  38. - Master nodes without etcd
  39. - Standalone etcd hosts
  40. - Kubernetes worker nodes
  41. Note that the Ansible script will report an invalid configuration if you wind up
  42. with an even number of etcd instances since that is not a valid configuration. This
  43. restriction includes standalone etcd nodes that are deployed in a cluster along with
  44. master nodes with etcd replicas. As an example, if you have three master nodes with
  45. etcd replicas and three standalone etcd nodes, the script will fail since there are
  46. now six total etcd replicas.
  47. ### GlusterFS
  48. The Terraform configuration supports provisioning of an optional GlusterFS
  49. shared file system based on a separate set of VMs. To enable this, you need to
  50. specify:
  51. - the number of Gluster hosts (minimum 2)
  52. - Size of the non-ephemeral volumes to be attached to store the GlusterFS bricks
  53. - Other properties related to provisioning the hosts
  54. Even if you are using Container Linux by CoreOS for your cluster, you will still
  55. need the GlusterFS VMs to be based on either Debian or RedHat based images.
  56. Container Linux by CoreOS cannot serve GlusterFS, but can connect to it through
  57. binaries available on hyperkube v1.4.3_coreos.0 or higher.
  58. ## Requirements
  59. - [Install Terraform](https://www.terraform.io/intro/getting-started/install.html) 0.12 or later
  60. - [Install Ansible](http://docs.ansible.com/ansible/latest/intro_installation.html)
  61. - you already have a suitable OS image in Glance
  62. - you already have a floating IP pool created
  63. - you have security groups enabled
  64. - you have a pair of keys generated that can be used to secure the new hosts
  65. ## Module Architecture
  66. The configuration is divided into three modules:
  67. - Network
  68. - IPs
  69. - Compute
  70. The main reason for splitting the configuration up in this way is to easily
  71. accommodate situations where floating IPs are limited by a quota or if you have
  72. any external references to the floating IP (e.g. DNS) that would otherwise have
  73. to be updated.
  74. You can force your existing IPs by modifying the compute variables in
  75. `kubespray.tf` as follows:
  76. ```
  77. k8s_master_fips = ["151.101.129.67"]
  78. k8s_node_fips = ["151.101.129.68"]
  79. ```
  80. ## Terraform
  81. Terraform will be used to provision all of the OpenStack resources with base software as appropriate.
  82. ### Configuration
  83. #### Inventory files
  84. 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):
  85. ```ShellSession
  86. $ cp -LRp contrib/terraform/openstack/sample-inventory inventory/$CLUSTER
  87. $ cd inventory/$CLUSTER
  88. $ ln -s ../../contrib/terraform/openstack/hosts
  89. $ ln -s ../../contrib
  90. ```
  91. This will be the base for subsequent Terraform commands.
  92. #### OpenStack access and credentials
  93. No provider variables are hardcoded inside `variables.tf` because Terraform
  94. supports various authentication methods for OpenStack: the older script and
  95. environment method (using `openrc`) as well as a newer declarative method, and
  96. different OpenStack environments may support Identity API version 2 or 3.
  97. These are examples and may vary depending on your OpenStack cloud provider,
  98. for an exhaustive list on how to authenticate on OpenStack with Terraform
  99. please read the [OpenStack provider documentation](https://www.terraform.io/docs/providers/openstack/).
  100. ##### Declarative method (recommended)
  101. The recommended authentication method is to describe credentials in a YAML file `clouds.yaml` that can be stored in:
  102. * the current directory
  103. * `~/.config/openstack`
  104. * `/etc/openstack`
  105. `clouds.yaml`:
  106. ```
  107. clouds:
  108. mycloud:
  109. auth:
  110. auth_url: https://openstack:5000/v3
  111. username: "username"
  112. project_name: "projectname"
  113. project_id: projectid
  114. user_domain_name: "Default"
  115. password: "password"
  116. region_name: "RegionOne"
  117. interface: "public"
  118. identity_api_version: 3
  119. ```
  120. If you have multiple clouds defined in your `clouds.yaml` file you can choose
  121. the one you want to use with the environment variable `OS_CLOUD`:
  122. ```
  123. export OS_CLOUD=mycloud
  124. ```
  125. ##### Openrc method
  126. When using classic environment variables, Terraform uses default `OS_*`
  127. environment variables. A script suitable for your environment may be available
  128. from Horizon under *Project* -> *Compute* -> *Access & Security* -> *API Access*.
  129. With identity v2:
  130. ```
  131. source openrc
  132. env | grep OS
  133. OS_AUTH_URL=https://openstack:5000/v2.0
  134. OS_PROJECT_ID=projectid
  135. OS_PROJECT_NAME=projectname
  136. OS_USERNAME=username
  137. OS_PASSWORD=password
  138. OS_REGION_NAME=RegionOne
  139. OS_INTERFACE=public
  140. OS_IDENTITY_API_VERSION=2
  141. ```
  142. With identity v3:
  143. ```
  144. source openrc
  145. env | grep OS
  146. OS_AUTH_URL=https://openstack:5000/v3
  147. OS_PROJECT_ID=projectid
  148. OS_PROJECT_NAME=username
  149. OS_PROJECT_DOMAIN_ID=default
  150. OS_USERNAME=username
  151. OS_PASSWORD=password
  152. OS_REGION_NAME=RegionOne
  153. OS_INTERFACE=public
  154. OS_IDENTITY_API_VERSION=3
  155. OS_USER_DOMAIN_NAME=Default
  156. ```
  157. Terraform does not support a mix of DomainName and DomainID, choose one or the
  158. other:
  159. ```
  160. * provider.openstack: You must provide exactly one of DomainID or DomainName to authenticate by Username
  161. ```
  162. ```
  163. unset OS_USER_DOMAIN_NAME
  164. export OS_USER_DOMAIN_ID=default
  165. or
  166. unset OS_PROJECT_DOMAIN_ID
  167. set OS_PROJECT_DOMAIN_NAME=Default
  168. ```
  169. #### Cluster variables
  170. The construction of the cluster is driven by values found in
  171. [variables.tf](variables.tf).
  172. For your cluster, edit `inventory/$CLUSTER/cluster.tfvars`.
  173. |Variable | Description |
  174. |---------|-------------|
  175. |`cluster_name` | All OpenStack resources will use the Terraform variable`cluster_name` (default`example`) in their name to make it easier to track. For example the first compute resource will be named`example-kubernetes-1`. |
  176. |`az_list` | List of Availability Zones available in your OpenStack cluster. |
  177. |`network_name` | The name to be given to the internal network that will be generated |
  178. |`network_dns_domain` | (Optional) The dns_domain for the internal network that will be generated |
  179. |`dns_nameservers`| An array of DNS name server names to be used by hosts in the internal subnet. |
  180. |`floatingip_pool` | Name of the pool from which floating IPs will be allocated |
  181. |`external_net` | UUID of the external network that will be routed to |
  182. |`flavor_k8s_master`,`flavor_k8s_node`,`flavor_etcd`, `flavor_bastion`,`flavor_gfs_node` | Flavor depends on your openstack installation, you can get available flavor IDs through `openstack flavor list` |
  183. |`image`,`image_gfs` | Name of the image to use in provisioning the compute resources. Should already be loaded into glance. |
  184. |`ssh_user`,`ssh_user_gfs` | The username to ssh into the image with. This usually depends on the image you have selected |
  185. |`public_key_path` | Path on your local workstation to the public key file you wish to use in creating the key pairs |
  186. |`number_of_k8s_masters`, `number_of_k8s_masters_no_floating_ip` | Number of nodes that serve as both master and etcd. These can be provisioned with or without floating IP addresses|
  187. |`number_of_k8s_masters_no_etcd`, `number_of_k8s_masters_no_floating_ip_no_etcd` | Number of nodes that serve as just master with no etcd. These can be provisioned with or without floating IP addresses |
  188. |`number_of_etcd` | Number of pure etcd nodes |
  189. |`number_of_k8s_nodes`, `number_of_k8s_nodes_no_floating_ip` | Kubernetes worker nodes. These can be provisioned with or without floating ip addresses. |
  190. |`number_of_bastions` | Number of bastion hosts to create. Scripts assume this is really just zero or one |
  191. |`number_of_gfs_nodes_no_floating_ip` | Number of gluster servers to provision. |
  192. | `gfs_volume_size_in_gb` | Size of the non-ephemeral volumes to be attached to store the GlusterFS bricks |
  193. |`supplementary_master_groups` | To add ansible groups to the masters, such as `kube-node` for tainting them as nodes, empty by default. |
  194. |`supplementary_node_groups` | To add ansible groups to the nodes, such as `kube-ingress` for running ingress controller pods, empty by default. |
  195. |`bastion_allowed_remote_ips` | List of CIDR allowed to initiate a SSH connection, `["0.0.0.0/0"]` by default |
  196. |`master_allowed_remote_ips` | List of CIDR blocks allowed to initiate an API connection, `["0.0.0.0/0"]` by default |
  197. |`k8s_allowed_remote_ips` | List of CIDR allowed to initiate a SSH connection, empty by default |
  198. |`worker_allowed_ports` | List of ports to open on worker nodes, `[{ "protocol" = "tcp", "port_range_min" = 30000, "port_range_max" = 32767, "remote_ip_prefix" = "0.0.0.0/0"}]` by default |
  199. |`wait_for_floatingip` | Let Terraform poll the instance until the floating IP has been associated, `false` by default. |
  200. |`node_root_volume_size_in_gb` | Size of the root volume for nodes, 0 to use ephemeral storage |
  201. |`master_root_volume_size_in_gb` | Size of the root volume for masters, 0 to use ephemeral storage |
  202. |`gfs_root_volume_size_in_gb` | Size of the root volume for gluster, 0 to use ephemeral storage |
  203. |`etcd_root_volume_size_in_gb` | Size of the root volume for etcd nodes, 0 to use ephemeral storage |
  204. |`bastion_root_volume_size_in_gb` | Size of the root volume for bastions, 0 to use ephemeral storage |
  205. #### Terraform state files
  206. In the cluster's inventory folder, the following files might be created (either by Terraform
  207. or manually), to prevent you from pushing them accidentally they are in a
  208. `.gitignore` file in the `terraform/openstack` directory :
  209. * `.terraform`
  210. * `.tfvars`
  211. * `.tfstate`
  212. * `.tfstate.backup`
  213. You can still add them manually if you want to.
  214. ### Initialization
  215. Before Terraform can operate on your cluster you need to install the required
  216. plugins. This is accomplished as follows:
  217. ```ShellSession
  218. $ cd inventory/$CLUSTER
  219. $ terraform init ../../contrib/terraform/openstack
  220. ```
  221. This should finish fairly quickly telling you Terraform has successfully initialized and loaded necessary modules.
  222. ### Provisioning cluster
  223. You can apply the Terraform configuration to your cluster with the following command
  224. issued from your cluster's inventory directory (`inventory/$CLUSTER`):
  225. ```ShellSession
  226. $ terraform apply -var-file=cluster.tfvars ../../contrib/terraform/openstack
  227. ```
  228. if you chose to create a bastion host, this script will create
  229. `contrib/terraform/openstack/k8s-cluster.yml` with an ssh command for Ansible to
  230. be able to access your machines tunneling through the bastion's IP address. If
  231. you want to manually handle the ssh tunneling to these machines, please delete
  232. or move that file. If you want to use this, just leave it there, as ansible will
  233. pick it up automatically.
  234. ### Destroying cluster
  235. You can destroy your new cluster with the following command issued from the cluster's inventory directory:
  236. ```ShellSession
  237. $ terraform destroy -var-file=cluster.tfvars ../../contrib/terraform/openstack
  238. ```
  239. If you've started the Ansible run, it may also be a good idea to do some manual cleanup:
  240. * remove SSH keys from the destroyed cluster from your `~/.ssh/known_hosts` file
  241. * clean up any temporary cache files: `rm /tmp/$CLUSTER-*`
  242. ### Debugging
  243. You can enable debugging output from Terraform by setting
  244. `OS_DEBUG` to 1 and`TF_LOG` to`DEBUG` before running the Terraform command.
  245. ### Terraform output
  246. Terraform can output values that are useful for configure Neutron/Octavia LBaaS or Cinder persistent volume provisioning as part of your Kubernetes deployment:
  247. - `private_subnet_id`: the subnet where your instances are running is used for `openstack_lbaas_subnet_id`
  248. - `floating_network_id`: the network_id where the floating IP are provisioned is used for `openstack_lbaas_floating_network_id`
  249. ## Ansible
  250. ### Node access
  251. #### SSH
  252. Ensure your local ssh-agent is running and your ssh key has been added. This
  253. step is required by the terraform provisioner:
  254. ```
  255. $ eval $(ssh-agent -s)
  256. $ ssh-add ~/.ssh/id_rsa
  257. ```
  258. 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`).
  259. #### Metadata variables
  260. The [python script](../terraform.py) that reads the
  261. generated`.tfstate` file to generate a dynamic inventory recognizes
  262. some variables within a "metadata" block, defined in a "resource"
  263. block (example):
  264. ```
  265. resource "openstack_compute_instance_v2" "example" {
  266. ...
  267. metadata {
  268. ssh_user = "ubuntu"
  269. prefer_ipv6 = true
  270. python_bin = "/usr/bin/python3"
  271. }
  272. ...
  273. }
  274. ```
  275. As the example shows, these let you define the SSH username for
  276. Ansible, a Python binary which is needed by Ansible if
  277. `/usr/bin/python` doesn't exist, and whether the IPv6 address of the
  278. instance should be preferred over IPv4.
  279. #### Bastion host
  280. Bastion access will be determined by:
  281. - Your choice on the amount of bastion hosts (set by `number_of_bastions` terraform variable).
  282. - The existence of nodes/masters with floating IPs (set by `number_of_k8s_masters`, `number_of_k8s_nodes`, `number_of_k8s_masters_no_etcd` terraform variables).
  283. If you have a bastion host, your ssh traffic will be directly routed through it. This is regardless of whether you have masters/nodes with a floating IP assigned.
  284. If you don't have a bastion host, but at least one of your masters/nodes have a floating IP, then ssh traffic will be tunneled by one of these machines.
  285. So, either a bastion host, or at least master/node with a floating IP are required.
  286. #### Test access
  287. Make sure you can connect to the hosts. Note that Container Linux by CoreOS 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`.
  288. ```
  289. $ ansible -i inventory/$CLUSTER/hosts -m ping all
  290. example-k8s_node-1 | SUCCESS => {
  291. "changed": false,
  292. "ping": "pong"
  293. }
  294. example-etcd-1 | SUCCESS => {
  295. "changed": false,
  296. "ping": "pong"
  297. }
  298. example-k8s-master-1 | SUCCESS => {
  299. "changed": false,
  300. "ping": "pong"
  301. }
  302. ```
  303. If it fails try to connect manually via SSH. It could be something as simple as a stale host key.
  304. ### Configure cluster variables
  305. Edit `inventory/$CLUSTER/group_vars/all/all.yml`:
  306. - **bin_dir**:
  307. ```
  308. # Directory where the binaries will be installed
  309. # Default:
  310. # bin_dir: /usr/local/bin
  311. # For Container Linux by CoreOS:
  312. bin_dir: /opt/bin
  313. ```
  314. - and **cloud_provider**:
  315. ```
  316. cloud_provider: openstack
  317. ```
  318. Edit `inventory/$CLUSTER/group_vars/k8s-cluster/k8s-cluster.yml`:
  319. - Set variable **kube_network_plugin** to your desired networking plugin.
  320. - **flannel** works out-of-the-box
  321. - **calico** requires [configuring OpenStack Neutron ports](/docs/openstack.md) to allow service and pod subnets
  322. ```
  323. # Choose network plugin (calico, weave or flannel)
  324. # Can also be set to 'cloud', which lets the cloud provider setup appropriate routing
  325. kube_network_plugin: flannel
  326. ```
  327. - Set variable **resolvconf_mode**
  328. ```
  329. # Can be docker_dns, host_resolvconf or none
  330. # Default:
  331. # resolvconf_mode: docker_dns
  332. # For Container Linux by CoreOS:
  333. resolvconf_mode: host_resolvconf
  334. ```
  335. - Set max amount of attached cinder volume per host (default 256)
  336. ```
  337. node_volume_attach_limit: 26
  338. ```
  339. - Disable access_ip, this will make all innternal cluster traffic to be sent over local network when a floating IP is attached (default this value is set to 1)
  340. ```
  341. use_access_ip: 0
  342. ```
  343. ### Deploy Kubernetes
  344. ```
  345. $ ansible-playbook --become -i inventory/$CLUSTER/hosts cluster.yml
  346. ```
  347. This will take some time as there are many tasks to run.
  348. ## Kubernetes
  349. ### Set up kubectl
  350. 1. [Install kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) on your workstation
  351. 2. Add a route to the internal IP of a master node (if needed):
  352. ```
  353. sudo route add [master-internal-ip] gw [router-ip]
  354. ```
  355. or
  356. ```
  357. sudo route add -net [internal-subnet]/24 gw [router-ip]
  358. ```
  359. 3. List Kubernetes certificates & keys:
  360. ```
  361. ssh [os-user]@[master-ip] sudo ls /etc/kubernetes/ssl/
  362. ```
  363. 4. Get `admin`'s certificates and keys:
  364. ```
  365. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/admin-kube-master-1-key.pem > admin-key.pem
  366. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/admin-kube-master-1.pem > admin.pem
  367. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/ca.pem > ca.pem
  368. ```
  369. 5. Configure kubectl:
  370. ```ShellSession
  371. $ kubectl config set-cluster default-cluster --server=https://[master-internal-ip]:6443 \
  372. --certificate-authority=ca.pem
  373. $ kubectl config set-credentials default-admin \
  374. --certificate-authority=ca.pem \
  375. --client-key=admin-key.pem \
  376. --client-certificate=admin.pem
  377. $ kubectl config set-context default-system --cluster=default-cluster --user=default-admin
  378. $ kubectl config use-context default-system
  379. ```
  380. 7. Check it:
  381. ```
  382. kubectl version
  383. ```
  384. ## GlusterFS
  385. GlusterFS is not deployed by the standard`cluster.yml` playbook, see the
  386. [GlusterFS playbook documentation](../../network-storage/glusterfs/README.md)
  387. for instructions.
  388. Basically you will install Gluster as
  389. ```ShellSession
  390. $ ansible-playbook --become -i inventory/$CLUSTER/hosts ./contrib/network-storage/glusterfs/glusterfs.yml
  391. ```
  392. ## What's next
  393. Try out your new Kubernetes cluster with the [Hello Kubernetes service](https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/).