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.

318 lines
8.7 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
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 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
8 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
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. node1
  35. node2
  36. [etcd]
  37. node1
  38. node2
  39. node3
  40. [kube-node]
  41. node2
  42. node3
  43. node4
  44. node5
  45. node6
  46. [k8s-cluster:children]
  47. kube-node
  48. kube-master
  49. ```
  50. Run the playbook
  51. ```
  52. ansible-playbook -i inventory/inventory.cfg cluster.yml -u root
  53. ```
  54. You can jump directly to "*Available apps, installation procedure*"
  55. Ansible
  56. -------------------------
  57. ### Variables
  58. The main variables to change are located in the directory ```inventory/group_vars/all.yml```.
  59. ### Inventory
  60. Below is an example of an inventory.
  61. ```
  62. ## Configure 'ip' variable to bind kubernetes services on a
  63. ## different ip than the default iface
  64. node1 ansible_ssh_host=95.54.0.12 # ip=10.3.0.1
  65. node2 ansible_ssh_host=95.54.0.13 # ip=10.3.0.2
  66. node3 ansible_ssh_host=95.54.0.14 # ip=10.3.0.3
  67. node4 ansible_ssh_host=95.54.0.15 # ip=10.3.0.4
  68. node5 ansible_ssh_host=95.54.0.16 # ip=10.3.0.5
  69. node6 ansible_ssh_host=95.54.0.17 # ip=10.3.0.6
  70. [kube-master]
  71. node1
  72. node2
  73. [etcd]
  74. node1
  75. node2
  76. node3
  77. [kube-node]
  78. node2
  79. node3
  80. node4
  81. node5
  82. node6
  83. [k8s-cluster:children]
  84. kube-node
  85. kube-master
  86. ```
  87. ### Playbook
  88. ```
  89. ---
  90. - hosts: k8s-cluster
  91. roles:
  92. - { role: download, tags: download }
  93. - { role: kubernetes/preinstall, tags: preinstall }
  94. - { role: docker, tags: docker }
  95. - { role: kubernetes/node, tags: node }
  96. - { role: etcd, tags: etcd }
  97. - { role: dnsmasq, tags: dnsmasq }
  98. - { role: network_plugin, tags: ['calico', 'flannel', 'network'] }
  99. - hosts: kube-master
  100. roles:
  101. - { role: kubernetes/master, tags: master }
  102. ```
  103. ### Run
  104. It is possible to define variables for different environments.
  105. For instance, in order to deploy the cluster on 'dev' environment run the following command.
  106. ```
  107. ansible-playbook -i inventory/dev/inventory.cfg cluster.yml -u root
  108. ```
  109. Kubernetes
  110. -------------------------
  111. ### Multi master notes
  112. * 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 ...),
  113. the server address has to be present on both groups 'kube-master' and 'kube-node'.
  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. ### Check cluster status
  123. #### Kubernetes components
  124. * Check the status of the processes
  125. ```
  126. systemctl status kubelet
  127. ```
  128. * Check the logs
  129. ```
  130. journalctl -ae -u kubelet
  131. ```
  132. * Check the NAT rules
  133. ```
  134. iptables -nLv -t nat
  135. ```
  136. For the master nodes you'll have to see the docker logs for the apiserver
  137. ```
  138. docker logs [apiserver docker id]
  139. ```
  140. ### Available apps, installation procedure
  141. There are two ways of installing new apps
  142. #### Ansible galaxy
  143. Additionnal apps can be installed with ```ansible-galaxy```.
  144. you'll need to edit the file '*requirements.yml*' in order to chose needed apps.
  145. The list of available apps are available [there](https://github.com/ansibl8s)
  146. For instance it is **strongly recommanded** to install a dns server which resolves kubernetes service names.
  147. In order to use this role you'll need the following entries in the file '*requirements.yml*'
  148. Please refer to the [k8s-kubedns readme](https://github.com/ansibl8s/k8s-kubedns) for additionnal info.
  149. ```
  150. - src: https://github.com/ansibl8s/k8s-common.git
  151. path: roles/apps
  152. # version: v1.0
  153. - src: https://github.com/ansibl8s/k8s-kubedns.git
  154. path: roles/apps
  155. # version: v1.0
  156. ```
  157. **Note**: the role common is required by all the apps and provides the tasks and libraries needed.
  158. And empty the apps directory
  159. ```
  160. rm -rf roles/apps/*
  161. ```
  162. Then download the roles with ansible-galaxy
  163. ```
  164. ansible-galaxy install -r requirements.yml
  165. ```
  166. Finally update the playbook ```apps.yml``` with the chosen roles, and run it
  167. ```
  168. ...
  169. - hosts: kube-master
  170. roles:
  171. - { role: apps/k8s-kubedns, tags: ['kubedns', 'apps'] }
  172. ...
  173. ```
  174. ```
  175. ansible-playbook -i inventory/inventory.cfg apps.yml -u root
  176. ```
  177. #### Git submodules
  178. Alternatively the roles can be installed as git submodules.
  179. That way is easier if you want to do some changes and commit them.
  180. ### Networking
  181. #### Calico
  182. Check if the calico-node container is running
  183. ```
  184. docker ps | grep calico
  185. ```
  186. The **calicoctl** command allows to check the status of the network workloads.
  187. * Check the status of Calico nodes
  188. ```
  189. calicoctl status
  190. ```
  191. * Show the configured network subnet for containers
  192. ```
  193. calicoctl pool show
  194. ```
  195. * Show the workloads (ip addresses of containers and their located)
  196. ```
  197. calicoctl endpoint show --detail
  198. ```
  199. ##### Optionnal : BGP Peering with border routers
  200. In some cases you may want to route the pods subnet and so NAT is not needed on the nodes.
  201. For instance if you have a cluster spread on different locations and you want your pods to talk each other no matter where they are located.
  202. The following variables need to be set:
  203. **peer_with_router** enable the peering with border router of the datacenter (default value: false).
  204. you'll need to edit the inventory and add a and a hostvar **local_as** by node.
  205. ```
  206. node1 ansible_ssh_host=95.54.0.12 local_as=xxxxxx
  207. ```
  208. #### Flannel
  209. * Flannel configuration file should have been created there
  210. ```
  211. cat /run/flannel/subnet.env
  212. FLANNEL_NETWORK=10.233.0.0/18
  213. FLANNEL_SUBNET=10.233.16.1/24
  214. FLANNEL_MTU=1450
  215. FLANNEL_IPMASQ=false
  216. ```
  217. * Check if the network interface has been created
  218. ```
  219. ip a show dev flannel.1
  220. 4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
  221. link/ether e2:f3:a7:0f:bf:cb brd ff:ff:ff:ff:ff:ff
  222. inet 10.233.16.0/18 scope global flannel.1
  223. valid_lft forever preferred_lft forever
  224. inet6 fe80::e0f3:a7ff:fe0f:bfcb/64 scope link
  225. valid_lft forever preferred_lft forever
  226. ```
  227. * Docker must be configured with a bridge ip in the flannel subnet.
  228. ```
  229. ps aux | grep docker
  230. 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
  231. ```
  232. * Try to run a container and check its ip address
  233. ```
  234. kubectl run test --image=busybox --command -- tail -f /dev/null
  235. replicationcontroller "test" created
  236. kubectl describe po test-34ozs | grep ^IP
  237. IP: 10.233.16.2
  238. ```
  239. ```
  240. kubectl exec test-34ozs -- ip a show dev eth0
  241. 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
  242. link/ether 02:42:0a:e9:2b:03 brd ff:ff:ff:ff:ff:ff
  243. inet 10.233.16.2/24 scope global eth0
  244. valid_lft forever preferred_lft forever
  245. inet6 fe80::42:aff:fee9:2b03/64 scope link tentative flags 08
  246. valid_lft forever preferred_lft forever
  247. ```
  248. Congrats ! now you can walk through [kubernetes basics](http://kubernetes.io/v1.1/basicstutorials.html)