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.

319 lines
9.4 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 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. [kube-master]
  34. 10.115.99.31
  35. [etcd]
  36. 10.115.99.31
  37. 10.115.99.32
  38. 10.115.99.33
  39. [kube-node]
  40. 10.115.99.32
  41. 10.115.99.33
  42. [k8s-cluster:children]
  43. kube-node
  44. kube-master
  45. ```
  46. Run the playbook
  47. ```
  48. ansible-playbook -i inventory/inventory.cfg cluster.yml -u root
  49. ```
  50. You can jump directly to "*Available apps, installation procedure*"
  51. Ansible
  52. -------------------------
  53. ### Variables
  54. The main variables to change are located in the directory ```inventory/group_vars/all.yml```.
  55. ### Inventory
  56. Below is an example of an inventory.
  57. Note : The bgp vars local_as and peers are not mandatory if the var **'peer_with_router'** is set to false
  58. By default this variable is set to false and therefore all the nodes are configure in **'node-mesh'** mode.
  59. In node-mesh mode the nodes peers with all the nodes in order to exchange routes.
  60. ```
  61. [kube-master]
  62. node1 ansible_ssh_host=10.99.0.26
  63. node2 ansible_ssh_host=10.99.0.27
  64. [etcd]
  65. node1 ansible_ssh_host=10.99.0.26
  66. node2 ansible_ssh_host=10.99.0.27
  67. node3 ansible_ssh_host=10.99.0.4
  68. [kube-node]
  69. node2 ansible_ssh_host=10.99.0.27
  70. node3 ansible_ssh_host=10.99.0.4
  71. node4 ansible_ssh_host=10.99.0.5
  72. node5 ansible_ssh_host=10.99.0.36
  73. node6 ansible_ssh_host=10.99.0.37
  74. [paris]
  75. node1 ansible_ssh_host=10.99.0.26
  76. node3 ansible_ssh_host=10.99.0.4 local_as=xxxxxxxx
  77. node4 ansible_ssh_host=10.99.0.5 local_as=xxxxxxxx
  78. [new-york]
  79. node2 ansible_ssh_host=10.99.0.27
  80. node5 ansible_ssh_host=10.99.0.36 local_as=xxxxxxxx
  81. node6 ansible_ssh_host=10.99.0.37 local_as=xxxxxxxx
  82. [k8s-cluster:children]
  83. kube-node
  84. kube-master
  85. ```
  86. ### Playbook
  87. ```
  88. ---
  89. - hosts: k8s-cluster
  90. roles:
  91. - { role: download, tags: download }
  92. - { role: kubernetes/preinstall, tags: preinstall }
  93. - { role: docker, tags: docker }
  94. - { role: kubernetes/node, tags: node }
  95. - { role: etcd, tags: etcd }
  96. - { role: dnsmasq, tags: dnsmasq }
  97. - { role: network_plugin, tags: ['calico', 'flannel', 'network'] }
  98. - hosts: kube-master
  99. roles:
  100. - { role: kubernetes/master, tags: master }
  101. ```
  102. ### Run
  103. It is possible to define variables for different environments.
  104. For instance, in order to deploy the cluster on 'dev' environment run the following command.
  105. ```
  106. ansible-playbook -i inventory/dev/inventory.cfg cluster.yml -u root
  107. ```
  108. Kubernetes
  109. -------------------------
  110. ### Multi master notes
  111. * 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 ...),
  112. the server address has to be present on both groups 'kube-master' and 'kube-node'.
  113. * Almost all kubernetes components are running into pods except *kubelet*. These pods are managed by kubelet which ensure they're always running
  114. * For safety reasons, you should have at least two master nodes and 3 etcd servers
  115. * 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.
  116. In order to do so, some variables have to be used '**loadbalancer_apiserver**' and '**apiserver_loadbalancer_domain_name**'
  117. ### Network Overlay
  118. You can choose between 2 network plugins. Only one must be chosen.
  119. * **flannel**: gre/vxlan (layer 2) networking. ([official docs](https://github.com/coreos/flannel))
  120. * **calico**: bgp (layer 3) networking. ([official docs](http://docs.projectcalico.org/en/0.13/))
  121. The choice is defined with the variable '**kube_network_plugin**'
  122. ### Expose a service
  123. There are several loadbalancing solutions.
  124. The one i found suitable for kubernetes are [Vulcand](http://vulcand.io/) and [Haproxy](http://www.haproxy.org/)
  125. My cluster is working with haproxy and kubernetes services are configured with the loadbalancing type '**nodePort**'.
  126. eg: each node opens the same tcp port and forwards the traffic to the target pod wherever it is located.
  127. Then Haproxy can be configured to request kubernetes's api in order to loadbalance on the proper tcp port on the nodes.
  128. Please refer to the proper kubernetes documentation on [Services](https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/user-guide/services.md)
  129. ### Check cluster status
  130. #### Kubernetes components
  131. * Check the status of the processes
  132. ```
  133. systemctl status kubelet
  134. ```
  135. * Check the logs
  136. ```
  137. journalctl -ae -u kubelet
  138. ```
  139. * Check the NAT rules
  140. ```
  141. iptables -nLv -t nat
  142. ```
  143. For the master nodes you'll have to see the docker logs for the apiserver
  144. ```
  145. docker logs [apiserver docker id]
  146. ```
  147. ### Available apps, installation procedure
  148. There are two ways of installing new apps
  149. #### Ansible galaxy
  150. Additionnal apps can be installed with ```ansible-galaxy```.
  151. ou'll need to edit the file '*requirements.yml*' in order to chose needed apps.
  152. The list of available apps are available [there](https://github.com/ansibl8s)
  153. For instance it is **strongly recommanded** to install a dns server which resolves kubernetes service names.
  154. In order to use this role you'll need the following entries in the file '*requirements.yml*'
  155. Please refer to the [k8s-kubedns readme](https://github.com/ansibl8s/k8s-kubedns) for additionnal info.
  156. ```
  157. - src: https://github.com/ansibl8s/k8s-common.git
  158. path: roles/apps
  159. # version: v1.0
  160. - src: https://github.com/ansibl8s/k8s-kubedns.git
  161. path: roles/apps
  162. # version: v1.0
  163. ```
  164. **Note**: the role common is required by all the apps and provides the tasks and libraries needed.
  165. And empty the apps directory
  166. ```
  167. rm -rf roles/apps/*
  168. ```
  169. Then download the roles with ansible-galaxy
  170. ```
  171. ansible-galaxy install -r requirements.yml
  172. ```
  173. Finally update the playbook ```apps.yml``` with the chosen roles, and run it
  174. ```
  175. ...
  176. - hosts: kube-master
  177. roles:
  178. - { role: apps/k8s-kubedns, tags: ['kubedns', 'apps'] }
  179. ...
  180. ```
  181. ```
  182. ansible-playbook -i inventory/inventory.cfg apps.yml -u root
  183. ```
  184. #### Git submodules
  185. Alternatively the roles can be installed as git submodules.
  186. That way is easier if you want to do some changes and commit them.
  187. ### Networking
  188. #### Calico
  189. Check if the calico-node container is running
  190. ```
  191. docker ps | grep calico
  192. ```
  193. The **calicoctl** command allows to check the status of the network workloads.
  194. * Check the status of Calico nodes
  195. ```
  196. calicoctl status
  197. ```
  198. * Show the configured network subnet for containers
  199. ```
  200. calicoctl pool show
  201. ```
  202. * Show the workloads (ip addresses of containers and their located)
  203. ```
  204. calicoctl endpoint show --detail
  205. ```
  206. #### Flannel
  207. * Flannel configuration file should have been created there
  208. ```
  209. cat /run/flannel/subnet.env
  210. FLANNEL_NETWORK=10.233.0.0/18
  211. FLANNEL_SUBNET=10.233.16.1/24
  212. FLANNEL_MTU=1450
  213. FLANNEL_IPMASQ=false
  214. ```
  215. * Check if the network interface has been created
  216. ```
  217. ip a show dev flannel.1
  218. 4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
  219. link/ether e2:f3:a7:0f:bf:cb brd ff:ff:ff:ff:ff:ff
  220. inet 10.233.16.0/18 scope global flannel.1
  221. valid_lft forever preferred_lft forever
  222. inet6 fe80::e0f3:a7ff:fe0f:bfcb/64 scope link
  223. valid_lft forever preferred_lft forever
  224. ```
  225. * Docker must be configured with a bridge ip in the flannel subnet.
  226. ```
  227. ps aux | grep docker
  228. 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
  229. ```
  230. * Try to run a container and check its ip address
  231. ```
  232. kubectl run test --image=busybox --command -- tail -f /dev/null
  233. replicationcontroller "test" created
  234. kubectl describe po test-34ozs | grep ^IP
  235. IP: 10.233.16.2
  236. ```
  237. ```
  238. kubectl exec test-34ozs -- ip a show dev eth0
  239. 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
  240. link/ether 02:42:0a:e9:2b:03 brd ff:ff:ff:ff:ff:ff
  241. inet 10.233.16.2/24 scope global eth0
  242. valid_lft forever preferred_lft forever
  243. inet6 fe80::42:aff:fee9:2b03/64 scope link tentative flags 08
  244. valid_lft forever preferred_lft forever
  245. ```
  246. Congrats ! now you can walk through [kubernetes basics](http://kubernetes.io/v1.1/basicstutorials.html)