forked from dcj/ansible-kubeadm-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster-create.yml
141 lines (105 loc) · 3.38 KB
/
cluster-create.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#
# Example invocation:
# ansible-playbook cluster-create.yml -e "cluster_name=foo"
#
---
- hosts: localhost
gather_facts: false
roles:
- role: admission_token
tags:
- token
tasks:
- debug:
var: admission_token
tags:
- token
# Implements Step 1 of http://kubernetes.io/docs/getting-started-guides/kubeadm/
# (1/4) Installing kubelet and kubeadm on your hosts
- hosts: "{{ cluster_name }}"
become: true
roles:
- role: kubeadm_install
tags:
- repo
- master
- nodes
# Implements Step 2 of http://kubernetes.io/docs/getting-started-guides/kubeadm/
# (2/4) Initializing your master
- hosts: "{{ cluster_name }}_master"
become: true
pre_tasks:
- name: Read token from file
include_vars: cfg/{{ cluster_name }}/token.yml
tasks:
- set_fact:
master_ip_address: "{{ master_ip_address_configured }}"
when: master_ip_address_configured is defined
- set_fact:
master_ip_address: "{{ ansible_default_ipv4['address'] }}"
when: master_ip_address_configured is not defined
- name: Save master_ip_address
shell: "echo master_ip_address{{ ':' }} {{ master_ip_address }} > cfg/{{ cluster_name }}/master_ip_address.yml"
delegate_to: 127.0.0.1
become: false
- name: Initialize master
command: kubeadm init --token {{ admission_token }}
args:
creates: /etc/kubernetes/pki # Not 100% sure that the creates clause is correct
register: master_init
ignore_errors: true
tags:
- master
- init
- name: Copy /etc/kubernetes/admin.conf on master to cfg/cluster_name/admin.conf
fetch:
src: /etc/kubernetes/admin.conf
dest: cfg/{{ cluster_name }}/admin.conf
flat: true
tags:
- master
- remote
# Implements Step 3 of http://kubernetes.io/docs/getting-started-guides/kubeadm/
# (3/4) Installing a pod network
- name: Install pod network
command: /usr/bin/kubectl --kubeconfig /etc/kubernetes/admin.conf apply -f https://git.io/weave-kube-1.6
register: pod_network
tags:
- master
- network
ignore_errors: true
- debug:
var: pod_network.stdout_lines
- name: Wait for Kube-DNS pod running
shell: "kubectl --kubeconfig /etc/kubernetes/admin.conf get pods --all-namespaces | grep kube-dns"
register: result
until: result.stdout.find("Running") != -1
retries: 30
delay: 10
# Implements Step 4 of http://kubernetes.io/docs/getting-started-guides/kubeadm/
# (4/4) Joining your nodes
- hosts: "{{ cluster_name }}_node"
become: true
pre_tasks:
- name: Read token from file
include_vars: cfg/{{ cluster_name }}/token.yml
- name: Read master_ip_address from file
include_vars: cfg/{{ cluster_name }}/master_ip_address.yml
tasks:
- debug:
var: admission_token
run_once: true
- debug:
var: master_ip_address
run_once: true
- name: Join nodes to cluster
command: kubeadm join --token {{ admission_token }} {{ master_ip_address }}:6443
args:
creates: /etc/kubernetes/kubelet.conf # Not 100% sure that the creates clause is correct
register: node_join
ignore_errors: true
tags:
- nodes
- join
- debug:
var: node_join.stdout_lines