Fixes #2: Make role work with nodes joining master.

This commit is contained in:
Jeff Geerling 2018-05-14 22:27:26 -05:00
parent 4536128126
commit 2138cc7d69
5 changed files with 118 additions and 54 deletions

View File

@ -22,6 +22,10 @@ Available variables are listed below, along with default values (see `defaults/m
Kubernetes packages to be installed on the server. You can either provide a list of package names, or set `name` and `state` to have more control over whether the package is `present`, `absent`, `latest`, etc. Kubernetes packages to be installed on the server. You can either provide a list of package names, or set `name` and `state` to have more control over whether the package is `present`, `absent`, `latest`, etc.
kubernetes_role: master
Whether the particular server will serve as a Kubernetes `master` (default) or `node`. The master will have `kubeadm init` run on it to intialize the entire K8s control plane, while `node`s will have `kubeadm join` run on them to join them to the `master`.
kubernetes_kubelet_extra_args: "" kubernetes_kubelet_extra_args: ""
Extra args to pass to `kubelet` during startup. E.g. to allow `kubelet` to start up even if there is swap is enabled on your server, set this to: `"--fail-swap-on=false"`. Extra args to pass to `kubelet` during startup. E.g. to allow `kubelet` to start up even if there is swap is enabled on your server, set this to: `"--fail-swap-on=false"`.
@ -34,11 +38,16 @@ Whether to remove the taint that denies pods from being deployed to the Kubernet
Whether to enable the Kubernetes web dashboard UI (only accessible on the master itself, or proxied). Whether to enable the Kubernetes web dashboard UI (only accessible on the master itself, or proxied).
kuberenetes_debug: False
Whether to show extra debug info in Ansible's logs (e.g. the output of the `kubeadm init` command).
kubernetes_pod_network_cidr: '10.0.1.0/16' kubernetes_pod_network_cidr: '10.0.1.0/16'
kubernetes_apiserver_advertise_address: ''
kubernetes_version: 'stable-1.10' kubernetes_version: 'stable-1.10'
kubernetes_ignore_preflight_errors: 'all' kubernetes_ignore_preflight_errors: 'all'
Options passed to `kubeadm init` when initializing the Kubernetes master. Options passed to `kubeadm init` when initializing the Kubernetes master. The `apiserver_advertise_address` defaults to `ansible_default_ipv4.address` if it's left empty.
kubernetes_apt_release_channel: main kubernetes_apt_release_channel: main
kubernetes_apt_repository: "deb http://apt.kubernetes.io/ kubernetes-xenial {{ kubernetes_apt_release_channel }}" kubernetes_apt_repository: "deb http://apt.kubernetes.io/ kubernetes-xenial {{ kubernetes_apt_release_channel }}"
@ -54,9 +63,9 @@ Yum repository options for Kubernetes installation.
None. None.
## Example Playbook ## Example Playbooks
For a single node (master) Kubernetes cluster: ### Single node (master-only) cluster
```yaml ```yaml
- hosts: all - hosts: all
@ -69,6 +78,35 @@ For a single node (master) Kubernetes cluster:
- geerlingguy.kubernetes - geerlingguy.kubernetes
``` ```
### Two or more nodes (single master) cluster
Master inventory vars:
```yaml
kubernetes_role: "master"
```
Node(s) inventory vars:
```yaml
kubernetes_role: "node"
```
Playbook:
```yaml
- hosts: all
vars:
kubernetes_allow_pods_on_master: True
roles:
- geerlingguy.docker
- geerlingguy.kubernetes
```
Then, log into the Kubernetes master, and run `kubectl get nodes` as root, and you should see a list of all the servers.
## License ## License
MIT / BSD MIT / BSD

View File

@ -9,12 +9,16 @@ kubernetes_packages:
- name: kubernetes-cni - name: kubernetes-cni
state: present state: present
kubernetes_role: master
kubernetes_kubelet_extra_args: "" kubernetes_kubelet_extra_args: ""
kubernetes_allow_pods_on_master: True kubernetes_allow_pods_on_master: True
kubernetes_enable_web_ui: True kubernetes_enable_web_ui: True
kuberenetes_debug: False
kubernetes_pod_network_cidr: '10.0.1.0/16' kubernetes_pod_network_cidr: '10.0.1.0/16'
kubernetes_apiserver_advertise_address: ''
kubernetes_version: 'stable-1.10' kubernetes_version: 'stable-1.10'
kubernetes_ignore_preflight_errors: 'all' kubernetes_ignore_preflight_errors: 'all'

View File

@ -35,57 +35,17 @@
path: /etc/kubernetes/admin.conf path: /etc/kubernetes/admin.conf
register: kubernetes_init_stat register: kubernetes_init_stat
- name: Initialize the Kubernetes master with kubeadm init. # Set up master.
command: > - include_tasks: master-setup.yml
kubeadm init when: kubernetes_role == 'master'
--pod-network-cidr={{ kubernetes_pod_network_cidr }}
--apiserver-advertise-address={{ ansible_default_ipv4.address }}
--kubernetes-version {{ kubernetes_version }}
--ignore-preflight-errors={{ kubernetes_ignore_preflight_errors }}
register: kubeadmin_init
failed_when: False
when: kubernetes_init_stat.stat.exists == False
- name: Print the init output to screen. # Set up nodes.
debug: var=kubeadmin_init.stdout - name: Get the kubeadm join command from the Kubernetes master.
when: kubernetes_init_stat.stat.exists == False shell: kubeadm token create --print-join-command
- name: Ensure .kube directory exists.
file:
path: ~/.kube
state: directory
- name: Symlink the kubectl admin.conf to ~/.kube/conf.
file:
src: /etc/kubernetes/admin.conf
dest: ~/.kube/config
state: link
- name: Configure Flannel networking.
command: "{{ item }}"
with_items:
- kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
- kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml
register: flannel_result
changed_when: "'created' in flannel_result.stdout"
# TODO: Check if taint exists with something like `kubectl describe nodes`
# instead of using kubernetes_init_stat.stat.exists check.
- name: Allow pods on master node (if configured).
command: "kubectl taint nodes --all node-role.kubernetes.io/master-"
when:
- kubernetes_allow_pods_on_master
- kubernetes_init_stat.stat.exists == False
- name: Check if Kubernetes Dashboard UI service already exists.
shell: kubectl get services --namespace kube-system | grep -q kubernetes-dashboard
changed_when: False changed_when: False
failed_when: False when: kubernetes_role == 'master'
register: kubernetes_dashboard_service run_once: True
when: kubernetes_enable_web_ui register: kubernetes_join_command
- name: Enable the Kubernetes Web Dashboard UI (if configured). - include_tasks: node-setup.yml
command: "kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml" when: kubernetes_role == 'node'
when:
- kubernetes_enable_web_ui
- kubernetes_dashboard_service is failed

57
tasks/master-setup.yml Normal file
View File

@ -0,0 +1,57 @@
---
- name: Initialize Kubernetes master with kubeadm init.
command: >
kubeadm init
--pod-network-cidr={{ kubernetes_pod_network_cidr }}
--apiserver-advertise-address={{ kubernetes_apiserver_advertise_address | default(ansible_default_ipv4.address) }}
--kubernetes-version {{ kubernetes_version }}
--ignore-preflight-errors={{ kubernetes_ignore_preflight_errors }}
register: kubeadmin_init
failed_when: False
when: kubernetes_init_stat.stat.exists == False
- name: Print the init output to screen.
debug: var=kubeadmin_init.stdout
when:
- kuberenetes_debug
- kubernetes_init_stat.stat.exists == False
- name: Ensure .kube directory exists.
file:
path: ~/.kube
state: directory
- name: Symlink the kubectl admin.conf to ~/.kube/conf.
file:
src: /etc/kubernetes/admin.conf
dest: ~/.kube/config
state: link
- name: Configure Flannel networking.
command: "{{ item }}"
with_items:
- kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
- kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml
register: flannel_result
changed_when: "'created' in flannel_result.stdout"
# TODO: Check if taint exists with something like `kubectl describe nodes`
# instead of using kubernetes_init_stat.stat.exists check.
- name: Allow pods on master node (if configured).
command: "kubectl taint nodes --all node-role.kubernetes.io/master-"
when:
- kubernetes_allow_pods_on_master
- kubernetes_init_stat.stat.exists == False
- name: Check if Kubernetes Dashboard UI service already exists.
shell: kubectl get services --namespace kube-system | grep -q kubernetes-dashboard
changed_when: False
failed_when: False
register: kubernetes_dashboard_service
when: kubernetes_enable_web_ui
- name: Enable the Kubernetes Web Dashboard UI (if configured).
command: "kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml"
when:
- kubernetes_enable_web_ui
- kubernetes_dashboard_service is failed

5
tasks/node-setup.yml Normal file
View File

@ -0,0 +1,5 @@
---
- name: Join node to Kubernetes master
shell: >
{{ kubernetes_join_command.stdout }}
creates=/etc/kubernetes/kubelet.conf