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.

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