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.

785 lines
31 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/)
  16. - [OVH](https://www.ovh.com/)
  17. - [Rackspace](https://www.rackspace.com/)
  18. - [Safespring](https://www.safespring.com)
  19. - [Ultimum](https://ultimum.io/)
  20. - [VexxHost](https://vexxhost.com/)
  21. - [Zetta](https://www.zetta.io/)
  22. ## Approach
  23. The terraform configuration inspects variables found in
  24. [variables.tf](variables.tf) to create resources in your OpenStack cluster.
  25. There is a [python script](../terraform.py) that reads the generated`.tfstate`
  26. file to generate a dynamic inventory that is consumed by the main ansible script
  27. to actually install kubernetes and stand up the cluster.
  28. ### Networking
  29. The configuration includes creating a private subnet with a router to the
  30. external net. It will allocate floating IPs from a pool and assign them to the
  31. hosts where that makes sense. You have the option of creating bastion hosts
  32. inside the private subnet to access the nodes there. Alternatively, a node with
  33. a floating IP can be used as a jump host to nodes without.
  34. #### Using an existing router
  35. It is possible to use an existing router instead of creating one. To use an
  36. existing router set the router\_id variable to the uuid of the router you wish
  37. to use.
  38. For example:
  39. ```ShellSession
  40. router_id = "00c542e7-6f46-4535-ae95-984c7f0391a3"
  41. ```
  42. ### Kubernetes Nodes
  43. You can create many different kubernetes topologies by setting the number of
  44. different classes of hosts. For each class there are options for allocating
  45. floating IP addresses or not.
  46. - Master nodes with etcd
  47. - Master nodes without etcd
  48. - Standalone etcd hosts
  49. - Kubernetes worker nodes
  50. Note that the Ansible script will report an invalid configuration if you wind up
  51. with an even number of etcd instances since that is not a valid configuration. This
  52. restriction includes standalone etcd nodes that are deployed in a cluster along with
  53. master nodes with etcd replicas. As an example, if you have three master nodes with
  54. etcd replicas and three standalone etcd nodes, the script will fail since there are
  55. now six total etcd replicas.
  56. ### GlusterFS shared file system
  57. The Terraform configuration supports provisioning of an optional GlusterFS
  58. shared file system based on a separate set of VMs. To enable this, you need to
  59. specify:
  60. - the number of Gluster hosts (minimum 2)
  61. - Size of the non-ephemeral volumes to be attached to store the GlusterFS bricks
  62. - Other properties related to provisioning the hosts
  63. Even if you are using Flatcar Container Linux by Kinvolk for your cluster, you will still
  64. need the GlusterFS VMs to be based on either Debian or RedHat based images.
  65. Flatcar Container Linux by Kinvolk cannot serve GlusterFS, but can connect to it through
  66. binaries available on hyperkube v1.4.3_coreos.0 or higher.
  67. ## Requirements
  68. - [Install Terraform](https://www.terraform.io/intro/getting-started/install.html) 0.14 or later
  69. - [Install Ansible](http://docs.ansible.com/ansible/latest/intro_installation.html)
  70. - you already have a suitable OS image in Glance
  71. - you already have a floating IP pool created
  72. - you have security groups enabled
  73. - you have a pair of keys generated that can be used to secure the new hosts
  74. ## Module Architecture
  75. The configuration is divided into three modules:
  76. - Network
  77. - IPs
  78. - Compute
  79. The main reason for splitting the configuration up in this way is to easily
  80. accommodate situations where floating IPs are limited by a quota or if you have
  81. any external references to the floating IP (e.g. DNS) that would otherwise have
  82. to be updated.
  83. You can force your existing IPs by modifying the compute variables in
  84. `kubespray.tf` as follows:
  85. ```ini
  86. k8s_master_fips = ["151.101.129.67"]
  87. k8s_node_fips = ["151.101.129.68"]
  88. ```
  89. ## Terraform
  90. Terraform will be used to provision all of the OpenStack resources with base software as appropriate.
  91. ### Configuration
  92. #### Inventory files
  93. 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):
  94. ```ShellSession
  95. cp -LRp contrib/terraform/openstack/sample-inventory inventory/$CLUSTER
  96. cd inventory/$CLUSTER
  97. ln -s ../../contrib/terraform/openstack/hosts
  98. ln -s ../../contrib
  99. ```
  100. This will be the base for subsequent Terraform commands.
  101. #### OpenStack access and credentials
  102. No provider variables are hardcoded inside `variables.tf` because Terraform
  103. supports various authentication methods for OpenStack: the older script and
  104. environment method (using `openrc`) as well as a newer declarative method, and
  105. different OpenStack environments may support Identity API version 2 or 3.
  106. These are examples and may vary depending on your OpenStack cloud provider,
  107. for an exhaustive list on how to authenticate on OpenStack with Terraform
  108. please read the [OpenStack provider documentation](https://www.terraform.io/docs/providers/openstack/).
  109. ##### Declarative method (recommended)
  110. The recommended authentication method is to describe credentials in a YAML file `clouds.yaml` that can be stored in:
  111. - the current directory
  112. - `~/.config/openstack`
  113. - `/etc/openstack`
  114. `clouds.yaml`:
  115. ```yaml
  116. clouds:
  117. mycloud:
  118. auth:
  119. auth_url: https://openstack:5000/v3
  120. username: "username"
  121. project_name: "projectname"
  122. project_id: projectid
  123. user_domain_name: "Default"
  124. password: "password"
  125. region_name: "RegionOne"
  126. interface: "public"
  127. identity_api_version: 3
  128. ```
  129. If you have multiple clouds defined in your `clouds.yaml` file you can choose
  130. the one you want to use with the environment variable `OS_CLOUD`:
  131. ```ShellSession
  132. export OS_CLOUD=mycloud
  133. ```
  134. ##### Openrc method
  135. When using classic environment variables, Terraform uses default `OS_*`
  136. environment variables. A script suitable for your environment may be available
  137. from Horizon under *Project* -> *Compute* -> *Access & Security* -> *API Access*.
  138. With identity v2:
  139. ```ShellSession
  140. source openrc
  141. env | grep OS
  142. OS_AUTH_URL=https://openstack:5000/v2.0
  143. OS_PROJECT_ID=projectid
  144. OS_PROJECT_NAME=projectname
  145. OS_USERNAME=username
  146. OS_PASSWORD=password
  147. OS_REGION_NAME=RegionOne
  148. OS_INTERFACE=public
  149. OS_IDENTITY_API_VERSION=2
  150. ```
  151. With identity v3:
  152. ```ShellSession
  153. source openrc
  154. env | grep OS
  155. OS_AUTH_URL=https://openstack:5000/v3
  156. OS_PROJECT_ID=projectid
  157. OS_PROJECT_NAME=username
  158. OS_PROJECT_DOMAIN_ID=default
  159. OS_USERNAME=username
  160. OS_PASSWORD=password
  161. OS_REGION_NAME=RegionOne
  162. OS_INTERFACE=public
  163. OS_IDENTITY_API_VERSION=3
  164. OS_USER_DOMAIN_NAME=Default
  165. ```
  166. Terraform does not support a mix of DomainName and DomainID, choose one or the other:
  167. - provider.openstack: You must provide exactly one of DomainID or DomainName to authenticate by Username
  168. ```ShellSession
  169. unset OS_USER_DOMAIN_NAME
  170. export OS_USER_DOMAIN_ID=default
  171. ```
  172. or
  173. ```ShellSession
  174. unset OS_PROJECT_DOMAIN_ID
  175. set OS_PROJECT_DOMAIN_NAME=Default
  176. ```
  177. #### Cluster variables
  178. The construction of the cluster is driven by values found in
  179. [variables.tf](variables.tf).
  180. For your cluster, edit `inventory/$CLUSTER/cluster.tfvars`.
  181. |Variable | Description |
  182. |---------|-------------|
  183. |`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`. |
  184. |`az_list` | List of Availability Zones available in your OpenStack cluster. |
  185. |`network_name` | The name to be given to the internal network that will be generated |
  186. |`use_existing_network`| Use an existing network with the name of `network_name`. `false` by default |
  187. |`network_dns_domain` | (Optional) The dns_domain for the internal network that will be generated |
  188. |`dns_nameservers`| An array of DNS name server names to be used by hosts in the internal subnet. |
  189. |`floatingip_pool` | Name of the pool from which floating IPs will be allocated |
  190. |`k8s_master_fips` | A list of floating IPs that you have already pre-allocated; they will be attached to master nodes instead of creating new random floating IPs. |
  191. |`bastion_fips` | A list of floating IPs that you have already pre-allocated; they will be attached to bastion node instead of creating new random floating IPs. |
  192. |`external_net` | UUID of the external network that will be routed to |
  193. |`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` |
  194. |`image`,`image_gfs` | Name of the image to use in provisioning the compute resources. Should already be loaded into glance. |
  195. |`ssh_user`,`ssh_user_gfs` | The username to ssh into the image with. This usually depends on the image you have selected |
  196. |`public_key_path` | Path on your local workstation to the public key file you wish to use in creating the key pairs |
  197. |`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|
  198. |`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 |
  199. |`number_of_etcd` | Number of pure etcd nodes |
  200. |`number_of_k8s_nodes`, `number_of_k8s_nodes_no_floating_ip` | Kubernetes worker nodes. These can be provisioned with or without floating ip addresses. |
  201. |`number_of_bastions` | Number of bastion hosts to create. Scripts assume this is really just zero or one |
  202. |`number_of_gfs_nodes_no_floating_ip` | Number of gluster servers to provision. |
  203. | `gfs_volume_size_in_gb` | Size of the non-ephemeral volumes to be attached to store the GlusterFS bricks |
  204. |`supplementary_master_groups` | To add ansible groups to the masters, such as `kube_node` for tainting them as nodes, empty by default. |
  205. |`supplementary_node_groups` | To add ansible groups to the nodes, such as `kube_ingress` for running ingress controller pods, empty by default. |
  206. |`bastion_allowed_remote_ips` | List of CIDR allowed to initiate a SSH connection, `["0.0.0.0/0"]` by default |
  207. |`master_allowed_remote_ips` | List of CIDR blocks allowed to initiate an API connection, `["0.0.0.0/0"]` by default |
  208. |`bastion_allowed_ports` | List of ports to open on bastion node, `[]` by default |
  209. |`k8s_allowed_remote_ips` | List of CIDR allowed to initiate a SSH connection, empty by default |
  210. |`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 |
  211. |`master_allowed_ports` | List of ports to open on master nodes, expected format is `[{ "protocol" = "tcp", "port_range_min" = 443, "port_range_max" = 443, "remote_ip_prefix" = "0.0.0.0/0"}]`, empty by default |
  212. |`node_root_volume_size_in_gb` | Size of the root volume for nodes, 0 to use ephemeral storage |
  213. |`master_root_volume_size_in_gb` | Size of the root volume for masters, 0 to use ephemeral storage |
  214. |`master_volume_type` | Volume type of the root volume for control_plane, 'Default' by default |
  215. |`node_volume_type` | Volume type of the root volume for nodes, 'Default' by default |
  216. |`gfs_root_volume_size_in_gb` | Size of the root volume for gluster, 0 to use ephemeral storage |
  217. |`etcd_root_volume_size_in_gb` | Size of the root volume for etcd nodes, 0 to use ephemeral storage |
  218. |`bastion_root_volume_size_in_gb` | Size of the root volume for bastions, 0 to use ephemeral storage |
  219. |`master_server_group_policy` | Enable and use openstack nova servergroups for masters with set policy, default: "" (disabled) |
  220. |`node_server_group_policy` | Enable and use openstack nova servergroups for nodes with set policy, default: "" (disabled) |
  221. |`etcd_server_group_policy` | Enable and use openstack nova servergroups for etcd with set policy, default: "" (disabled) |
  222. |`additional_server_groups` | Extra server groups to create. Set "policy" to the policy for the group, expected format is `{"new-server-group" = {"policy" = "anti-affinity"}}`, default: {} (to not create any extra groups) |
  223. |`use_access_ip` | If 1, nodes with floating IPs will transmit internal cluster traffic via floating IPs; if 0 private IPs will be used instead. Default value is 1. |
  224. |`port_security_enabled` | Allow to disable port security by setting this to `false`. `true` by default |
  225. |`force_null_port_security` | Set `null` instead of `true` or `false` for `port_security`. `false` by default |
  226. |`k8s_nodes` | Map containing worker node definition, see explanation below |
  227. |`k8s_masters` | Map containing master node definition, see explanation for k8s_nodes and `sample-inventory/cluster.tfvars` |
  228. ##### k8s_nodes
  229. Allows a custom definition of worker nodes giving the operator full control over individual node flavor and availability zone placement.
  230. To enable the use of this mode set the `number_of_k8s_nodes` and `number_of_k8s_nodes_no_floating_ip` variables to 0.
  231. Then define your desired worker node configuration using the `k8s_nodes` variable.
  232. The `az`, `flavor` and `floating_ip` parameters are mandatory.
  233. The optional parameter `extra_groups` (a comma-delimited string) can be used to define extra inventory group memberships for specific nodes.
  234. ```yaml
  235. k8s_nodes:
  236. node-name:
  237. az: string # Name of the AZ
  238. flavor: string # Flavor ID to use
  239. floating_ip: bool # If floating IPs should be created or not
  240. extra_groups: string # (optional) Additional groups to add for kubespray, defaults to no groups
  241. image_id: string # (optional) Image ID to use, defaults to var.image_id or var.image
  242. root_volume_size_in_gb: number # (optional) Size of the block storage to use as root disk, defaults to var.node_root_volume_size_in_gb or to use volume from flavor otherwise
  243. volume_type: string # (optional) Volume type to use, defaults to var.node_volume_type
  244. network_id: string # (optional) Use this network_id for the node, defaults to either var.network_id or ID of var.network_name
  245. server_group: string # (optional) Server group to add this node to. If set, this has to be one specified in additional_server_groups, defaults to use the server group specified in node_server_group_policy
  246. cloudinit: # (optional) Options for cloud-init
  247. extra_partitions: # List of extra partitions (other than the root partition) to setup during creation
  248. volume_path: string # Path to the volume to create partition for (e.g. /dev/vda )
  249. partition_path: string # Path to the partition (e.g. /dev/vda2 )
  250. mount_path: string # Path to where the partition should be mounted
  251. partition_start: string # Where the partition should start (e.g. 10GB ). Note, if you set the partition_start to 0 there will be no space left for the root partition
  252. partition_end: string # Where the partition should end (e.g. 10GB or -1 for end of volume)
  253. ```
  254. For example:
  255. ```ini
  256. k8s_nodes = {
  257. "1" = {
  258. "az" = "sto1"
  259. "flavor" = "83d8b44a-26a0-4f02-a981-079446926445"
  260. "floating_ip" = true
  261. },
  262. "2" = {
  263. "az" = "sto2"
  264. "flavor" = "83d8b44a-26a0-4f02-a981-079446926445"
  265. "floating_ip" = true
  266. },
  267. "3" = {
  268. "az" = "sto3"
  269. "flavor" = "83d8b44a-26a0-4f02-a981-079446926445"
  270. "floating_ip" = true
  271. "extra_groups" = "calico_rr"
  272. }
  273. }
  274. ```
  275. Would result in the same configuration as:
  276. ```ini
  277. number_of_k8s_nodes = 3
  278. flavor_k8s_node = "83d8b44a-26a0-4f02-a981-079446926445"
  279. az_list = ["sto1", "sto2", "sto3"]
  280. ```
  281. And:
  282. ```ini
  283. k8s_nodes = {
  284. "ing-1" = {
  285. "az" = "sto1"
  286. "flavor" = "83d8b44a-26a0-4f02-a981-079446926445"
  287. "floating_ip" = true
  288. },
  289. "ing-2" = {
  290. "az" = "sto2"
  291. "flavor" = "83d8b44a-26a0-4f02-a981-079446926445"
  292. "floating_ip" = true
  293. },
  294. "ing-3" = {
  295. "az" = "sto3"
  296. "flavor" = "83d8b44a-26a0-4f02-a981-079446926445"
  297. "floating_ip" = true
  298. },
  299. "big-1" = {
  300. "az" = "sto1"
  301. "flavor" = "3f73fc93-ec61-4808-88df-2580d94c1a9b"
  302. "floating_ip" = false
  303. },
  304. "big-2" = {
  305. "az" = "sto2"
  306. "flavor" = "3f73fc93-ec61-4808-88df-2580d94c1a9b"
  307. "floating_ip" = false
  308. },
  309. "big-3" = {
  310. "az" = "sto3"
  311. "flavor" = "3f73fc93-ec61-4808-88df-2580d94c1a9b"
  312. "floating_ip" = false
  313. },
  314. "small-1" = {
  315. "az" = "sto1"
  316. "flavor" = "7a6a998f-ac7f-4fb8-a534-2175b254f75e"
  317. "floating_ip" = false
  318. },
  319. "small-2" = {
  320. "az" = "sto2"
  321. "flavor" = "7a6a998f-ac7f-4fb8-a534-2175b254f75e"
  322. "floating_ip" = false
  323. },
  324. "small-3" = {
  325. "az" = "sto3"
  326. "flavor" = "7a6a998f-ac7f-4fb8-a534-2175b254f75e"
  327. "floating_ip" = false
  328. }
  329. }
  330. ```
  331. Would result in three nodes in each availability zone each with their own separate naming,
  332. flavor and floating ip configuration.
  333. The "schema":
  334. ```ini
  335. k8s_nodes = {
  336. "key | node name suffix, must be unique" = {
  337. "az" = string
  338. "flavor" = string
  339. "floating_ip" = bool
  340. },
  341. }
  342. ```
  343. All values are required.
  344. #### Terraform state files
  345. In the cluster's inventory folder, the following files might be created (either by Terraform
  346. or manually), to prevent you from pushing them accidentally they are in a
  347. `.gitignore` file in the `terraform/openstack` directory :
  348. - `.terraform`
  349. - `.tfvars`
  350. - `.tfstate`
  351. - `.tfstate.backup`
  352. You can still add them manually if you want to.
  353. ### Initialization
  354. Before Terraform can operate on your cluster you need to install the required
  355. plugins. This is accomplished as follows:
  356. ```ShellSession
  357. cd inventory/$CLUSTER
  358. terraform -chdir="../../contrib/terraform/openstack" init
  359. ```
  360. This should finish fairly quickly telling you Terraform has successfully initialized and loaded necessary modules.
  361. ### Customizing with cloud-init
  362. You can apply cloud-init based customization for the openstack instances before provisioning your cluster.
  363. One common template is used for all instances. Adjust the file shown below:
  364. `contrib/terraform/openstack/modules/compute/templates/cloudinit.yaml.tmpl`
  365. For example, to enable openstack novnc access and ansible_user=root SSH access:
  366. ```ShellSession
  367. #cloud-config
  368. ## in some cases novnc console access is required
  369. ## it requires ssh password to be set
  370. ssh_pwauth: yes
  371. chpasswd:
  372. list: |
  373. root:secret
  374. expire: False
  375. ## in some cases direct root ssh access via ssh key is required
  376. disable_root: false
  377. ```
  378. ### Provisioning cluster
  379. You can apply the Terraform configuration to your cluster with the following command
  380. issued from your cluster's inventory directory (`inventory/$CLUSTER`):
  381. ```ShellSession
  382. terraform -chdir="../../contrib/terraform/openstack" apply -var-file=cluster.tfvars
  383. ```
  384. if you chose to create a bastion host, this script will create
  385. `contrib/terraform/openstack/k8s_cluster.yml` with an ssh command for Ansible to
  386. be able to access your machines tunneling through the bastion's IP address. If
  387. you want to manually handle the ssh tunneling to these machines, please delete
  388. or move that file. If you want to use this, just leave it there, as ansible will
  389. pick it up automatically.
  390. ### Destroying cluster
  391. You can destroy your new cluster with the following command issued from the cluster's inventory directory:
  392. ```ShellSession
  393. terraform -chdir="../../contrib/terraform/openstack" destroy -var-file=cluster.tfvars
  394. ```
  395. If you've started the Ansible run, it may also be a good idea to do some manual cleanup:
  396. - remove SSH keys from the destroyed cluster from your `~/.ssh/known_hosts` file
  397. - clean up any temporary cache files: `rm /tmp/$CLUSTER-*`
  398. ### Debugging
  399. You can enable debugging output from Terraform by setting
  400. `OS_DEBUG` to 1 and`TF_LOG` to`DEBUG` before running the Terraform command.
  401. ### Terraform output
  402. Terraform can output values that are useful for configure Neutron/Octavia LBaaS or Cinder persistent volume provisioning as part of your Kubernetes deployment:
  403. - `private_subnet_id`: the subnet where your instances are running is used for `openstack_lbaas_subnet_id`
  404. - `floating_network_id`: the network_id where the floating IP are provisioned is used for `openstack_lbaas_floating_network_id`
  405. ## Ansible
  406. ### Node access
  407. #### SSH
  408. Ensure your local ssh-agent is running and your ssh key has been added. This
  409. step is required by the terraform provisioner:
  410. ```ShellSession
  411. eval $(ssh-agent -s)
  412. ssh-add ~/.ssh/id_rsa
  413. ```
  414. 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`).
  415. #### Metadata variables
  416. The [python script](../terraform.py) that reads the
  417. generated`.tfstate` file to generate a dynamic inventory recognizes
  418. some variables within a "metadata" block, defined in a "resource"
  419. block (example):
  420. ```ini
  421. resource "openstack_compute_instance_v2" "example" {
  422. ...
  423. metadata {
  424. ssh_user = "ubuntu"
  425. prefer_ipv6 = true
  426. python_bin = "/usr/bin/python3"
  427. }
  428. ...
  429. }
  430. ```
  431. As the example shows, these let you define the SSH username for
  432. Ansible, a Python binary which is needed by Ansible if
  433. `/usr/bin/python` doesn't exist, and whether the IPv6 address of the
  434. instance should be preferred over IPv4.
  435. #### Bastion host
  436. Bastion access will be determined by:
  437. - Your choice on the amount of bastion hosts (set by `number_of_bastions` terraform variable).
  438. - 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).
  439. 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.
  440. 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.
  441. So, either a bastion host, or at least master/node with a floating IP are required.
  442. #### Test access
  443. 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`.
  444. ```ShellSession
  445. $ ansible -i inventory/$CLUSTER/hosts -m ping all
  446. example-k8s_node-1 | SUCCESS => {
  447. "changed": false,
  448. "ping": "pong"
  449. }
  450. example-etcd-1 | SUCCESS => {
  451. "changed": false,
  452. "ping": "pong"
  453. }
  454. example-k8s-master-1 | SUCCESS => {
  455. "changed": false,
  456. "ping": "pong"
  457. }
  458. ```
  459. If it fails try to connect manually via SSH. It could be something as simple as a stale host key.
  460. ### Configure cluster variables
  461. Edit `inventory/$CLUSTER/group_vars/all/all.yml`:
  462. - **bin_dir**:
  463. ```yml
  464. # Directory where the binaries will be installed
  465. # Default:
  466. # bin_dir: /usr/local/bin
  467. # For Flatcar Container Linux by Kinvolk:
  468. bin_dir: /opt/bin
  469. ```
  470. - and **cloud_provider**:
  471. ```yml
  472. cloud_provider: openstack
  473. ```
  474. Edit `inventory/$CLUSTER/group_vars/k8s_cluster/k8s_cluster.yml`:
  475. - Set variable **kube_network_plugin** to your desired networking plugin.
  476. - **flannel** works out-of-the-box
  477. - **calico** requires [configuring OpenStack Neutron ports](/docs/openstack.md) to allow service and pod subnets
  478. ```yml
  479. # Choose network plugin (calico, weave or flannel)
  480. # Can also be set to 'cloud', which lets the cloud provider setup appropriate routing
  481. kube_network_plugin: flannel
  482. ```
  483. - Set variable **resolvconf_mode**
  484. ```yml
  485. # Can be docker_dns, host_resolvconf or none
  486. # Default:
  487. # resolvconf_mode: docker_dns
  488. # For Flatcar Container Linux by Kinvolk:
  489. resolvconf_mode: host_resolvconf
  490. ```
  491. - Set max amount of attached cinder volume per host (default 256)
  492. ```yml
  493. node_volume_attach_limit: 26
  494. ```
  495. ### Deploy Kubernetes
  496. ```ShellSession
  497. ansible-playbook --become -i inventory/$CLUSTER/hosts cluster.yml
  498. ```
  499. This will take some time as there are many tasks to run.
  500. ## Kubernetes
  501. ### Set up kubectl
  502. 1. [Install kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) on your workstation
  503. 2. Add a route to the internal IP of a master node (if needed):
  504. ```ShellSession
  505. sudo route add [master-internal-ip] gw [router-ip]
  506. ```
  507. or
  508. ```ShellSession
  509. sudo route add -net [internal-subnet]/24 gw [router-ip]
  510. ```
  511. 1. List Kubernetes certificates & keys:
  512. ```ShellSession
  513. ssh [os-user]@[master-ip] sudo ls /etc/kubernetes/ssl/
  514. ```
  515. 1. Get `admin`'s certificates and keys:
  516. ```ShellSession
  517. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/admin-kube-master-1-key.pem > admin-key.pem
  518. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/admin-kube-master-1.pem > admin.pem
  519. ssh [os-user]@[master-ip] sudo cat /etc/kubernetes/ssl/ca.pem > ca.pem
  520. ```
  521. 1. Configure kubectl:
  522. ```ShellSession
  523. $ kubectl config set-cluster default-cluster --server=https://[master-internal-ip]:6443 \
  524. --certificate-authority=ca.pem
  525. $ kubectl config set-credentials default-admin \
  526. --certificate-authority=ca.pem \
  527. --client-key=admin-key.pem \
  528. --client-certificate=admin.pem
  529. $ kubectl config set-context default-system --cluster=default-cluster --user=default-admin
  530. $ kubectl config use-context default-system
  531. ```
  532. 1. Check it:
  533. ```ShellSession
  534. kubectl version
  535. ```
  536. ## GlusterFS
  537. GlusterFS is not deployed by the standard `cluster.yml` playbook, see the
  538. [GlusterFS playbook documentation](../../network-storage/glusterfs/README.md)
  539. for instructions.
  540. Basically you will install Gluster as
  541. ```ShellSession
  542. ansible-playbook --become -i inventory/$CLUSTER/hosts ./contrib/network-storage/glusterfs/glusterfs.yml
  543. ```
  544. ## What's next
  545. Try out your new Kubernetes cluster with the [Hello Kubernetes service](https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/).
  546. ## Appendix
  547. ### Migration from `number_of_k8s_nodes*` to `k8s_nodes`
  548. If you currently have a cluster defined using the `number_of_k8s_nodes*` variables and wish
  549. to migrate to the `k8s_nodes` style you can do it like so:
  550. ```ShellSession
  551. $ terraform state list
  552. module.compute.data.openstack_images_image_v2.gfs_image
  553. module.compute.data.openstack_images_image_v2.vm_image
  554. module.compute.openstack_compute_floatingip_associate_v2.k8s_master[0]
  555. module.compute.openstack_compute_floatingip_associate_v2.k8s_node[0]
  556. module.compute.openstack_compute_floatingip_associate_v2.k8s_node[1]
  557. module.compute.openstack_compute_floatingip_associate_v2.k8s_node[2]
  558. module.compute.openstack_compute_instance_v2.k8s_master[0]
  559. module.compute.openstack_compute_instance_v2.k8s_node[0]
  560. module.compute.openstack_compute_instance_v2.k8s_node[1]
  561. module.compute.openstack_compute_instance_v2.k8s_node[2]
  562. module.compute.openstack_compute_keypair_v2.k8s
  563. module.compute.openstack_compute_servergroup_v2.k8s_etcd[0]
  564. module.compute.openstack_compute_servergroup_v2.k8s_master[0]
  565. module.compute.openstack_compute_servergroup_v2.k8s_node[0]
  566. module.compute.openstack_networking_secgroup_rule_v2.bastion[0]
  567. module.compute.openstack_networking_secgroup_rule_v2.egress[0]
  568. module.compute.openstack_networking_secgroup_rule_v2.k8s
  569. module.compute.openstack_networking_secgroup_rule_v2.k8s_allowed_remote_ips[0]
  570. module.compute.openstack_networking_secgroup_rule_v2.k8s_allowed_remote_ips[1]
  571. module.compute.openstack_networking_secgroup_rule_v2.k8s_allowed_remote_ips[2]
  572. module.compute.openstack_networking_secgroup_rule_v2.k8s_master[0]
  573. module.compute.openstack_networking_secgroup_rule_v2.worker[0]
  574. module.compute.openstack_networking_secgroup_rule_v2.worker[1]
  575. module.compute.openstack_networking_secgroup_rule_v2.worker[2]
  576. module.compute.openstack_networking_secgroup_rule_v2.worker[3]
  577. module.compute.openstack_networking_secgroup_rule_v2.worker[4]
  578. module.compute.openstack_networking_secgroup_v2.bastion[0]
  579. module.compute.openstack_networking_secgroup_v2.k8s
  580. module.compute.openstack_networking_secgroup_v2.k8s_master
  581. module.compute.openstack_networking_secgroup_v2.worker
  582. module.ips.null_resource.dummy_dependency
  583. module.ips.openstack_networking_floatingip_v2.k8s_master[0]
  584. module.ips.openstack_networking_floatingip_v2.k8s_node[0]
  585. module.ips.openstack_networking_floatingip_v2.k8s_node[1]
  586. module.ips.openstack_networking_floatingip_v2.k8s_node[2]
  587. module.network.openstack_networking_network_v2.k8s[0]
  588. module.network.openstack_networking_router_interface_v2.k8s[0]
  589. module.network.openstack_networking_router_v2.k8s[0]
  590. module.network.openstack_networking_subnet_v2.k8s[0]
  591. $ terraform state mv 'module.compute.openstack_compute_floatingip_associate_v2.k8s_node[0]' 'module.compute.openstack_compute_floatingip_associate_v2.k8s_nodes["1"]'
  592. Move "module.compute.openstack_compute_floatingip_associate_v2.k8s_node[0]" to "module.compute.openstack_compute_floatingip_associate_v2.k8s_nodes[\"1\"]"
  593. Successfully moved 1 object(s).
  594. $ terraform state mv 'module.compute.openstack_compute_floatingip_associate_v2.k8s_node[1]' 'module.compute.openstack_compute_floatingip_associate_v2.k8s_nodes["2"]'
  595. Move "module.compute.openstack_compute_floatingip_associate_v2.k8s_node[1]" to "module.compute.openstack_compute_floatingip_associate_v2.k8s_nodes[\"2\"]"
  596. Successfully moved 1 object(s).
  597. $ terraform state mv 'module.compute.openstack_compute_floatingip_associate_v2.k8s_node[2]' 'module.compute.openstack_compute_floatingip_associate_v2.k8s_nodes["3"]'
  598. Move "module.compute.openstack_compute_floatingip_associate_v2.k8s_node[2]" to "module.compute.openstack_compute_floatingip_associate_v2.k8s_nodes[\"3\"]"
  599. Successfully moved 1 object(s).
  600. $ terraform state mv 'module.compute.openstack_compute_instance_v2.k8s_node[0]' 'module.compute.openstack_compute_instance_v2.k8s_node["1"]'
  601. Move "module.compute.openstack_compute_instance_v2.k8s_node[0]" to "module.compute.openstack_compute_instance_v2.k8s_node[\"1\"]"
  602. Successfully moved 1 object(s).
  603. $ terraform state mv 'module.compute.openstack_compute_instance_v2.k8s_node[1]' 'module.compute.openstack_compute_instance_v2.k8s_node["2"]'
  604. Move "module.compute.openstack_compute_instance_v2.k8s_node[1]" to "module.compute.openstack_compute_instance_v2.k8s_node[\"2\"]"
  605. Successfully moved 1 object(s).
  606. $ terraform state mv 'module.compute.openstack_compute_instance_v2.k8s_node[2]' 'module.compute.openstack_compute_instance_v2.k8s_node["3"]'
  607. Move "module.compute.openstack_compute_instance_v2.k8s_node[2]" to "module.compute.openstack_compute_instance_v2.k8s_node[\"3\"]"
  608. Successfully moved 1 object(s).
  609. $ terraform state mv 'module.ips.openstack_networking_floatingip_v2.k8s_node[0]' 'module.ips.openstack_networking_floatingip_v2.k8s_node["1"]'
  610. Move "module.ips.openstack_networking_floatingip_v2.k8s_node[0]" to "module.ips.openstack_networking_floatingip_v2.k8s_node[\"1\"]"
  611. Successfully moved 1 object(s).
  612. $ terraform state mv 'module.ips.openstack_networking_floatingip_v2.k8s_node[1]' 'module.ips.openstack_networking_floatingip_v2.k8s_node["2"]'
  613. Move "module.ips.openstack_networking_floatingip_v2.k8s_node[1]" to "module.ips.openstack_networking_floatingip_v2.k8s_node[\"2\"]"
  614. Successfully moved 1 object(s).
  615. $ terraform state mv 'module.ips.openstack_networking_floatingip_v2.k8s_node[2]' 'module.ips.openstack_networking_floatingip_v2.k8s_node["3"]'
  616. Move "module.ips.openstack_networking_floatingip_v2.k8s_node[2]" to "module.ips.openstack_networking_floatingip_v2.k8s_node[\"3\"]"
  617. Successfully moved 1 object(s).
  618. ```
  619. Of course for nodes without floating ips those steps can be omitted.