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.

328 lines
9.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. [![Build Status](https://travis-ci.org/ansibl8s/setup-kubernetes.svg)](https://travis-ci.org/ansibl8s/setup-kubernetes)
  2. kubernetes-ansible
  3. ========
  4. This project allows to
  5. - Install and configure a **Multi-Master/HA kubernetes** cluster.
  6. - Choose the **network plugin** to be used within the cluster
  7. - A **set of roles** in order to install applications over the k8s cluster
  8. - A **flexible method** which helps to create new roles for apps.
  9. Linux distributions tested:
  10. * **Debian** Wheezy, Jessie
  11. * **Ubuntu** 14.10, 15.04, 15.10
  12. * **Fedora** 23
  13. * **CentOS** 7 (Currently with flannel only)
  14. ### Requirements
  15. * The target servers must have **access to the Internet** in order to pull docker imaqes.
  16. * The firewalls are not managed, you'll need to implement your own rules the way you used to.
  17. in order to avoid any issue during deployment you should **disable your firewall**
  18. * **Copy your ssh keys** to all the servers part of your inventory.
  19. * **Ansible v1.9.x/v2.x and python-netaddr**
  20. * Base knowledge on Ansible. Please refer to [Ansible documentation](http://www.ansible.com/how-ansible-works)
  21. ### Components
  22. * [kubernetes](https://github.com/kubernetes/kubernetes/releases) v1.1.4
  23. * [etcd](https://github.com/coreos/etcd/releases) v2.2.4
  24. * [calicoctl](https://github.com/projectcalico/calico-docker/releases) v0.14.0
  25. * [flanneld](https://github.com/coreos/flannel/releases) v0.5.5
  26. * [docker](https://www.docker.com/) v1.9.1
  27. Quickstart
  28. -------------------------
  29. The following steps will quickly setup a kubernetes cluster with default configuration.
  30. These defaults are good for tests purposes.
  31. Edit the inventory according to the number of servers
  32. ```
  33. [downloader]
  34. localhost ansible_connection=local ansible_python_interpreter=python2
  35. [kube-master]
  36. 10.115.99.31
  37. [etcd]
  38. 10.115.99.31
  39. 10.115.99.32
  40. 10.115.99.33
  41. [kube-node]
  42. 10.115.99.32
  43. 10.115.99.33
  44. [k8s-cluster:children]
  45. kube-node
  46. kube-master
  47. ```
  48. Run the playbook
  49. ```
  50. ansible-playbook -i inventory/inventory.cfg cluster.yml -u root
  51. ```
  52. You can jump directly to "*Available apps, installation procedure*"
  53. Ansible
  54. -------------------------
  55. ### Variables
  56. The main variables to change are located in the directory ```inventory/group_vars/all.yml```.
  57. ### Inventory
  58. Below is an example of an inventory.
  59. Note : The bgp vars local_as and peers are not mandatory if the var **'peer_with_router'** is set to false
  60. By default this variable is set to false and therefore all the nodes are configure in **'node-mesh'** mode.
  61. In node-mesh mode the nodes peers with all the nodes in order to exchange routes.
  62. ```
  63. [downloader]
  64. localhost ansible_connection=local ansible_python_interpreter=python2
  65. [kube-master]
  66. node1 ansible_ssh_host=10.99.0.26
  67. node2 ansible_ssh_host=10.99.0.27
  68. [etcd]
  69. node1 ansible_ssh_host=10.99.0.26
  70. node2 ansible_ssh_host=10.99.0.27
  71. node3 ansible_ssh_host=10.99.0.4
  72. [kube-node]
  73. node2 ansible_ssh_host=10.99.0.27
  74. node3 ansible_ssh_host=10.99.0.4
  75. node4 ansible_ssh_host=10.99.0.5
  76. node5 ansible_ssh_host=10.99.0.36
  77. node6 ansible_ssh_host=10.99.0.37
  78. [paris]
  79. node1 ansible_ssh_host=10.99.0.26
  80. node3 ansible_ssh_host=10.99.0.4 local_as=xxxxxxxx
  81. node4 ansible_ssh_host=10.99.0.5 local_as=xxxxxxxx
  82. [new-york]
  83. node2 ansible_ssh_host=10.99.0.27
  84. node5 ansible_ssh_host=10.99.0.36 local_as=xxxxxxxx
  85. node6 ansible_ssh_host=10.99.0.37 local_as=xxxxxxxx
  86. [k8s-cluster:children]
  87. kube-node
  88. kube-master
  89. ```
  90. ### Playbook
  91. ```
  92. ---
  93. - hosts: downloader
  94. sudo: no
  95. roles:
  96. - { role: download, tags: download }
  97. - hosts: k8s-cluster
  98. roles:
  99. - { role: kubernetes/preinstall, tags: preinstall }
  100. - { role: docker, tags: docker }
  101. - { role: kubernetes/node, tags: node }
  102. - { role: etcd, tags: etcd }
  103. - { role: dnsmasq, tags: dnsmasq }
  104. - { role: network_plugin, tags: ['calico', 'flannel', 'network'] }
  105. - hosts: kube-master
  106. roles:
  107. - { role: kubernetes/master, tags: master }
  108. ```
  109. ### Run
  110. It is possible to define variables for different environments.
  111. For instance, in order to deploy the cluster on 'dev' environment run the following command.
  112. ```
  113. ansible-playbook -i inventory/dev/inventory.cfg cluster.yml -u root
  114. ```
  115. Kubernetes
  116. -------------------------
  117. ### Multi master notes
  118. * You can choose where to install the master components. If you want your master node to act both as master (api,scheduler,controller) and node (e.g. accept workloads, create pods ...),
  119. the server address has to be present on both groups 'kube-master' and 'kube-node'.
  120. * Almost all kubernetes components are running into pods except *kubelet*. These pods are managed by kubelet which ensure they're always running
  121. * For safety reasons, you should have at least two master nodes and 3 etcd servers
  122. * Kube-proxy doesn't support multiple apiservers on startup ([Issue 18174](https://github.com/kubernetes/kubernetes/issues/18174)). An external loadbalancer needs to be configured.
  123. In order to do so, some variables have to be used '**loadbalancer_apiserver**' and '**apiserver_loadbalancer_domain_name**'
  124. ### Network Overlay
  125. You can choose between 2 network plugins. Only one must be chosen.
  126. * **flannel**: gre/vxlan (layer 2) networking. ([official docs](https://github.com/coreos/flannel))
  127. * **calico**: bgp (layer 3) networking. ([official docs](http://docs.projectcalico.org/en/0.13/))
  128. The choice is defined with the variable '**kube_network_plugin**'
  129. ### Expose a service
  130. There are several loadbalancing solutions.
  131. The one i found suitable for kubernetes are [Vulcand](http://vulcand.io/) and [Haproxy](http://www.haproxy.org/)
  132. My cluster is working with haproxy and kubernetes services are configured with the loadbalancing type '**nodePort**'.
  133. eg: each node opens the same tcp port and forwards the traffic to the target pod wherever it is located.
  134. Then Haproxy can be configured to request kubernetes's api in order to loadbalance on the proper tcp port on the nodes.
  135. Please refer to the proper kubernetes documentation on [Services](https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/user-guide/services.md)
  136. ### Check cluster status
  137. #### Kubernetes components
  138. * Check the status of the processes
  139. ```
  140. systemctl status kubelet
  141. ```
  142. * Check the logs
  143. ```
  144. journalctl -ae -u kubelet
  145. ```
  146. * Check the NAT rules
  147. ```
  148. iptables -nLv -t nat
  149. ```
  150. For the master nodes you'll have to see the docker logs for the apiserver
  151. ```
  152. docker logs [apiserver docker id]
  153. ```
  154. ### Available apps, installation procedure
  155. There are two ways of installing new apps
  156. #### Ansible galaxy
  157. Additionnal apps can be installed with ```ansible-galaxy```.
  158. ou'll need to edit the file '*requirements.yml*' in order to chose needed apps.
  159. The list of available apps are available [there](https://github.com/ansibl8s)
  160. For instance it is **strongly recommanded** to install a dns server which resolves kubernetes service names.
  161. In order to use this role you'll need the following entries in the file '*requirements.yml*'
  162. Please refer to the [k8s-kubedns readme](https://github.com/ansibl8s/k8s-kubedns) for additionnal info.
  163. ```
  164. - src: https://github.com/ansibl8s/k8s-common.git
  165. path: roles/apps
  166. # version: v1.0
  167. - src: https://github.com/ansibl8s/k8s-kubedns.git
  168. path: roles/apps
  169. # version: v1.0
  170. ```
  171. **Note**: the role common is required by all the apps and provides the tasks and libraries needed.
  172. And empty the apps directory
  173. ```
  174. rm -rf roles/apps/*
  175. ```
  176. Then download the roles with ansible-galaxy
  177. ```
  178. ansible-galaxy install -r requirements.yml
  179. ```
  180. Finally update the playbook ```apps.yml``` with the chosen roles, and run it
  181. ```
  182. ...
  183. - hosts: kube-master
  184. roles:
  185. - { role: apps/k8s-kubedns, tags: ['kubedns', 'apps'] }
  186. ...
  187. ```
  188. ```
  189. ansible-playbook -i inventory/inventory.cfg apps.yml -u root
  190. ```
  191. #### Git submodules
  192. Alternatively the roles can be installed as git submodules.
  193. That way is easier if you want to do some changes and commit them.
  194. ### Networking
  195. #### Calico
  196. Check if the calico-node container is running
  197. ```
  198. docker ps | grep calico
  199. ```
  200. The **calicoctl** command allows to check the status of the network workloads.
  201. * Check the status of Calico nodes
  202. ```
  203. calicoctl status
  204. ```
  205. * Show the configured network subnet for containers
  206. ```
  207. calicoctl pool show
  208. ```
  209. * Show the workloads (ip addresses of containers and their located)
  210. ```
  211. calicoctl endpoint show --detail
  212. ```
  213. #### Flannel
  214. * Flannel configuration file should have been created there
  215. ```
  216. cat /run/flannel/subnet.env
  217. FLANNEL_NETWORK=10.233.0.0/18
  218. FLANNEL_SUBNET=10.233.16.1/24
  219. FLANNEL_MTU=1450
  220. FLANNEL_IPMASQ=false
  221. ```
  222. * Check if the network interface has been created
  223. ```
  224. ip a show dev flannel.1
  225. 4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
  226. link/ether e2:f3:a7:0f:bf:cb brd ff:ff:ff:ff:ff:ff
  227. inet 10.233.16.0/18 scope global flannel.1
  228. valid_lft forever preferred_lft forever
  229. inet6 fe80::e0f3:a7ff:fe0f:bfcb/64 scope link
  230. valid_lft forever preferred_lft forever
  231. ```
  232. * Docker must be configured with a bridge ip in the flannel subnet.
  233. ```
  234. ps aux | grep docker
  235. root 20196 1.7 2.7 1260616 56840 ? Ssl 10:18 0:07 /usr/bin/docker daemon --bip=10.233.16.1/24 --mtu=1450
  236. ```
  237. * Try to run a container and check its ip address
  238. ```
  239. kubectl run test --image=busybox --command -- tail -f /dev/null
  240. replicationcontroller "test" created
  241. kubectl describe po test-34ozs | grep ^IP
  242. IP: 10.233.16.2
  243. ```
  244. ```
  245. kubectl exec test-34ozs -- ip a show dev eth0
  246. 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
  247. link/ether 02:42:0a:e9:2b:03 brd ff:ff:ff:ff:ff:ff
  248. inet 10.233.16.2/24 scope global eth0
  249. valid_lft forever preferred_lft forever
  250. inet6 fe80::42:aff:fee9:2b03/64 scope link tentative flags 08
  251. valid_lft forever preferred_lft forever
  252. ```
  253. Congrats ! now you can walk through [kubernetes basics](http://kubernetes.io/v1.1/basicstutorials.html)