-
Notifications
You must be signed in to change notification settings - Fork 5
/
control_plane.yml
322 lines (278 loc) · 9.78 KB
/
control_plane.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
- hosts: all
vars_files:
- "variables.yml"
tasks:
- name: Ping all nodes
ping:
- name: Run download vars playbook
include: latest_kubernetes_version.yml
- name: Download the k8s control plane binaries
when: inventory_hostname in groups['k8s-node-1']
become: true
get_url:
url: "{{ item['value'] }}"
dest: "/usr/local/bin/{{ item['key'] }}"
mode: a+r,u+w,a+x
force: true
with_items:
- "{{ control_plane_binaries | dict2items | list }}"
tags:
- downloads
- name: Generating the kubeconfig for the kube-controller-manager
when: inventory_hostname in groups['k8s-node-1']
become: true
register: kube_controller_manager
shell: |
{
kubectl config set-cluster devkube \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=https://127.0.0.1:6443 \
--kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig
kubectl config set-credentials system:kube-controller-manager \
--client-certificate=kube-controller-manager.pem \
--client-key=kube-controller-manager-key.pem \
--embed-certs=true \
--kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig
kubectl config set-context default \
--cluster=devkube \
--user=system:kube-controller-manager \
--kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig
kubectl config use-context default --kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig
}
- when: inventory_hostname in groups['k8s-node-1']
debug:
var: kube_controller_manager.stdout_lines
- name: Generating the kubeconfig for the kube-scheduler
when: inventory_hostname in groups['k8s-node-1']
become: true
register: kube_scheduler
shell: |
{
kubectl config set-cluster devkube \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=https://127.0.0.1:6443 \
--kubeconfig=/var/lib/kubernetes/kube-scheduler.kubeconfig
kubectl config set-credentials system:kube-scheduler \
--client-certificate=kube-scheduler.pem \
--client-key=kube-scheduler-key.pem \
--embed-certs=true \
--kubeconfig=/var/lib/kubernetes/kube-scheduler.kubeconfig
kubectl config set-context default \
--cluster=devkube \
--user=system:kube-scheduler \
--kubeconfig=/var/lib/kubernetes/kube-scheduler.kubeconfig
kubectl config use-context default --kubeconfig=/var/lib/kubernetes/kube-scheduler.kubeconfig
}
- when: inventory_hostname in groups['k8s-node-1']
debug:
var: kube_scheduler.stdout_lines
- name: Generating the kubeconfig for the admin user
when: inventory_hostname in groups['k8s-node-1']
become: true
register: admin_kubeconfig
shell: |
{
kubectl config set-cluster devkube \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=https://127.0.0.1:6443 \
--kubeconfig=admin.kubeconfig
kubectl config set-credentials admin \
--client-certificate=admin.pem \
--client-key=admin-key.pem \
--embed-certs=true \
--kubeconfig=admin.kubeconfig
kubectl config set-context default \
--cluster=devkube \
--user=admin \
--kubeconfig=admin.kubeconfig
kubectl config use-context default --kubeconfig=admin.kubeconfig
}
- when: inventory_hostname in groups['k8s-node-1']
debug:
var: admin_kubeconfig.stdout_lines
- name: Create required directories
become: true
when: inventory_hostname in groups['k8s-node-1']
file:
path: "{{ item }}"
state: directory
with_items:
- /var/lib/etcd
- /etc/etcd
- /etc/kubernetes/config
- /var/lib/kubernetes/
- ~/etcd
- name: Unarchive etcd and move to correct dir
become: true
when: inventory_hostname in groups['k8s-node-1']
unarchive:
src: /usr/local/bin/etc-d
dest: ~/etcd
remote_src: yes
mode: a+r,u+w,a+x
extra_opts: ['--strip-components=1']
- when: inventory_hostname in groups['k8s-node-1']
become: true
shell: mv ~/etcd/etcd* /usr/local/bin/
- name: Move certs to correct place
become: true
when: inventory_hostname in groups['k8s-node-1']
copy:
src: "{{ item }}"
dest: /etc/etcd/
with_items:
- ca.pem
- kubernetes-key.pem
- kubernetes.pem
- name: Create the systemd unit file for etcd
become: true
when: inventory_hostname in groups['k8s-node-1']
template:
src: etcd.service.j2
dest: /etc/systemd/system/etcd.service
- name: Reload conf, start etcd node
become: true
when: inventory_hostname in groups['k8s-node-1']
systemd:
daemon_reload: yes
name: etcd
state: started
enabled: yes
- when: inventory_hostname in groups['k8s-node-1']
become: true
systemd:
daemon_reload: yes
name: etcd
state: restarted
enabled: yes
- name: Check if etcd is online
become: true
when: inventory_hostname in groups['k8s-node-1']
shell: |
ETCDCTL_API=3 etcdctl member list \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/etcd/ca.pem \
--cert=/etc/etcd/kubernetes.pem \
--key=/etc/etcd/kubernetes-key.pem
register: etcd_output
retries: 5
delay: 5
until: etcd_output.rc == 0
- when: inventory_hostname in groups['k8s-node-1']
debug:
var: etcd_output.stdout_lines
- name: move the cert files to /var/lib/kubernetes
become: true
when: inventory_hostname in groups['k8s-node-1']
copy:
src: "{{ item }}"
dest: /var/lib/kubernetes/
with_items:
- ca.pem
- ca-key.pem
- kubernetes-key.pem
- kubernetes.pem
- service-account-key.pem
- service-account.pem
- encryption-config.yaml
- name: move the cert files to /etc/kubernetes/config/
become: true
when: inventory_hostname in groups['k8s-node-1']
copy:
src: "{{ item }}"
dest: /etc/kubernetes/config/
with_items:
- kube-scheduler.yaml
- name: Writing the kube-apiserver.serivce systemd file
when: inventory_hostname in groups['k8s-node-1']
become: true
template:
src: kube-apiserver.service.j2
dest: /etc/systemd/system/kube-apiserver.service
- name: Writing the kube-controller-manager.serivce systemd file
become: true
when: inventory_hostname in groups['k8s-node-1']
template:
src: kube-controller-manager.service.j2
dest: /etc/systemd/system/kube-controller-manager.service
- name: Writing the kube-scheduler.serivce systemd file
become: true
when: inventory_hostname in groups['k8s-node-1']
template:
src: kube-scheduler.service.j2
dest: /etc/systemd/system/kube-scheduler.service
- name: Reload conf, start control plane
become: true
when: inventory_hostname in groups['k8s-node-1']
systemd:
daemon_reload: yes
name: "{{ item }}"
state: started
enabled: yes
with_items:
- kube-apiserver
- kube-controller-manager
- kube-scheduler
- when: inventory_hostname in groups['k8s-node-1']
become: true
systemd:
daemon_reload: yes
name: "{{ item }}"
state: restarted
enabled: yes
with_items:
- kube-apiserver
- kube-controller-manager
- kube-scheduler
- name: Sleeping for 10 seconds for the components to get up
pause:
seconds: 10
- name: Check if control plane is up
become: true
when: inventory_hostname in groups['k8s-node-1']
register: k8s_cp_output
shell: |
kubectl get componentstatuses --kubeconfig admin.kubeconfig
- when: inventory_hostname in groups['k8s-node-1']
debug:
var: k8s_cp_output.stdout_lines
- name: Copy apiserver rbac to master
when: inventory_hostname in groups['k8s-node-1']
copy:
src: kube-rbac-apiserver.yml
dest: ~/
- name: Apply apiserver rbac
become: true
when: inventory_hostname in groups['k8s-node-1']
shell: |
kubectl apply --kubeconfig admin.kubeconfig -f kube-rbac-apiserver.yml
retries: 5
delay: 5
register: rbac
until: rbac.rc == 0
- name: Generating the kubeconfig for remote access
run_once: true
delegate_to: localhost
shell: |
{
kubectl config set-cluster devkube \
--certificate-authority=ca.pem \
--embed-certs=true \
--server="https://{{ groups['k8s-node-1'] | join('') }}:6443"
kubectl config set-credentials admin \
--client-certificate=admin.pem \
--client-key=admin-key.pem \
--embed-certs=true
kubectl config set-context default \
--cluster=devkube \
--user=admin
kubectl config use-context default
}
- name: Display some debug msgs
run_once: true
delegate_to: localhost
debug:
msg: "{{ hostvars[groups['k8s-node-1'] | join('')]['ip']}}, {{ groups['all'] | map('extract',hostvars,'ansible_nodename') | join(',') }},{{ groups['all'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) | join(',') }},127.0.0.1,kubernetes.default"