-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-k8s.yaml
317 lines (271 loc) · 8.87 KB
/
deploy-k8s.yaml
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
- name: Setup Prerequisites To Install Kubernetes
hosts: all
become: true
vars:
kube_prereq_packages: [curl, ca-certificates, apt-transport-https]
kube_packages: [kubeadm, kubectl, kubelet]
tasks:
- name: Test Reacheability
ansible.builtin.ping:
- name: Update Cache
ansible.builtin.apt:
update_cache: true
autoclean: true
- name: 1. Upgrade All the Packages to the latest
ansible.builtin.apt:
upgrade: "full"
- name: 2. Install Qemu-Guest-Agent
ansible.builtin.apt:
name:
- qemu-guest-agent
state: present
- name: 3. Setup a Container Runtime
ansible.builtin.apt:
name:
- containerd
state: present
- name: 4. Start Containerd If Stopped
ansible.builtin.service:
name: containerd
state: started
- name: 5. Create Containerd Directory
ansible.builtin.file:
path: /etc/containerd
state: directory
mode: '0755'
- name: 6. Check config.toml Exists
ansible.builtin.stat:
path: /etc/containerd/config.toml
register: pre_file_exist_result
- name: 6.1 Delete config.toml Exists
ansible.builtin.file:
path: /etc/containerd/config.toml
state: absent
when: pre_file_exist_result.stat.exists
- name: 7. Place Default Containerd Config Inside It
ansible.builtin.shell: |
set -o pipefail
containerd config default | sudo tee /etc/containerd/config.toml
register: output
changed_when: output.rc != 0
args:
executable: /bin/bash
tags:
- containerd_config
- name: 7.1 Check If New config.toml Exists Now
ansible.builtin.stat:
path: /etc/containerd/config.toml
register: post_file_exist_result
tags:
- containerd_config
- name: 7.2 Exit The Play If config.toml Does Not Exist
ansible.builtin.meta: end_play
when: not post_file_exist_result.stat.exists
tags:
- containerd_config
- name: 8.1 Disable Swap
ansible.builtin.command: sudo swapoff -a
register: output
changed_when: output.rc != 0
tags:
- disable_swap
- name: 8.2 Disable Swap permanently
ansible.builtin.replace:
path: /etc/fstab
regexp: '^([^#].*?\sswap\s+sw\s+.*)$'
replace: '# \1'
tags:
- disable_swap
- name: 9. Edit config.toml
ansible.builtin.replace:
path: /etc/containerd/config.toml
after: \[plugins\."io\.containerd\.grpc\.v1\.cri"\.containerd\.runtimes\.runc\.options\]
regexp: SystemdCgroup = false
replace: SystemdCgroup = true
- name: 10. Enable Ipv4 Bridging
ansible.builtin.replace:
path: /etc/sysctl.conf
regexp: ^#net\.ipv4\.ip_forward=1$
replace: net.ipv4.ip_forward=1
- name: 11.1 Delete k8s Config If Exists
ansible.builtin.file:
path: /etc/modules-load.d/k8s.conf
state: absent
tags:
- kube_config
- name: 11.2 Add k8s.config and Edit It
ansible.builtin.lineinfile:
path: /etc/modules-load.d/k8s.conf
line: br_netfilter
create: true
mode: '0755'
tags:
- kube_config
- name: 12.1 Reboot
ansible.builtin.reboot:
register: system_reboot
- name: 12.2 Verify Reboot Success
ansible.builtin.ping:
when: system_reboot.rebooted
- name: 13.1 Update Cache
ansible.builtin.apt:
update_cache: true
autoclean: true
tags:
- install_pre_kube_packages
- name: 13.2 Remove apt lock file
ansible.builtin.file:
state: absent
path: "/var/lib/dpkg/lock"
tags:
- install_pre_kube_packages
- name: 13.3 Install Prerequisite Packages
ansible.builtin.apt:
name: '{{ kube_prereq_packages }}'
tags:
- install_pre_kube_packages
- name: 13.4 Remove GPG Keys If They Exist
ansible.builtin.file:
path: "{{ item }}"
state: absent
with_items:
- /usr/share/keyrings/kubernetes-apt-keyring.gpg
- /usr/share/keyrings/kubernetes-apt-keyring.gpg_armored
tags:
- install_pre_kube_packages
- name: 13.5 Download Kubernetes APT Key
ansible.builtin.get_url:
url: https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key
dest: /usr/share/keyrings/kubernetes-apt-keyring.gpg_armored
mode: '0755'
tags:
- install_pre_kube_packages
- name: 13.6 De-Armor Kubernetes APT Key
ansible.builtin.shell: gpg --dearmor < /usr/share/keyrings/kubernetes-apt-keyring.gpg_armored > /etc/apt/keyrings/kubernetes-apt-keyring.gpg
no_log: true
args:
creates: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
tags:
- install_pre_kube_packages
- name: 13.7 Add Kubernetes APT Key
ansible.builtin.shell: |
set -o pipefail
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' \
| sudo tee /etc/apt/sources.list.d/kubernetes.list
register: apt_output
changed_when: apt_output.rc != 0
args:
executable: /bin/bash
tags:
- install_pre_kube_packages
- name: 14.1 Update Cache
ansible.builtin.apt:
update_cache: true
autoclean: true
tags:
- install_kube_packages
- name: 14.2 Remove apt lock file
ansible.builtin.file:
state: absent
path: "/var/lib/dpkg/lock"
tags:
- install_kube_packages
- name: 14.3 Install Required Packages
ansible.builtin.apt:
name: '{{ kube_packages }}'
tags:
- install_kube_packages
- name: 14.4 Hold Packages
ansible.builtin.dpkg_selections:
name: '{{ item }}'
selection: hold
with_items: '{{ kube_packages }}'
tags:
- install_kube_packages
- name: Prompt To Continue On To Configuring Control Nodes
ansible.builtin.pause:
prompt: Press RETURN when you want to continue configuring the Control nodes!
- name: Setup Controller Nodes
gather_facts: true
hosts: master
become: true
tasks:
- name: 1. Initialize Cluster
ansible.builtin.shell: |
set -o pipefail
sudo kubeadm init --apiserver-advertise-address=10.0.6.200 --pod-network-cidr=10.244.0.0/16
register: init_cluster_output
changed_when: init_cluster_output.rc != 0
args:
executable: /bin/bash
- name: 2.1 Create .kube Directory
ansible.builtin.file:
path: .kube
state: directory
mode: '0755'
tags:
- kube_admin_config
- name: 2.2 Copy Kubernetes Admin Config
ansible.builtin.copy:
remote_src: true
src: /etc/kubernetes/admin.conf
dest: .kube/config
mode: '0755'
tags:
- kube_admin_config
- name: 2.3 Change Config File Permission
ansible.builtin.command: chown {{ ansible_env.USER }}:{{ ansible_env.USER }} ".kube/config"
changed_when: false
when: not ansible_env.HOME is undefined
tags:
- kube_admin_config
- name: 3. Install An Overlay Network
ansible.builtin.shell: |
set -o pipefail
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
register: init_cluster_output
become: false
changed_when: init_cluster_output.rc != 0
args:
executable: /bin/bash
- name: 4.1 Execute Join String Generation Command
ansible.builtin.command: kubeadm token create --print-join-command
become: false
register: join_output
changed_when: false
tags:
- join_string
- name: 4.2 Display Join String
ansible.builtin.debug:
msg: 'Join Command : {{ join_output.stdout }}'
tags:
- join_string
- name: Copy Connection String To A Remote File
ansible.builtin.template:
src: ./files/k8s_worker_node_connection.j2
dest: worker_conn_string
mode: '0755'
- name: Check Connection String File Exists
ansible.builtin.stat:
path: worker_conn_string
register: conn_file_path_remote
- name: Fetch The Remote File
ansible.builtin.fetch:
src: worker_conn_string
dest: Remote_Files/worker_conn_string
flat: true
when: conn_file_path_remote.stat.exists
- name: Prompt To Continue On To Configuring Worker Nodes
ansible.builtin.pause:
prompt: Press RETURN when you want to continue configuring the Worker nodes!
- name: Join Worker Nodes
gather_facts: true
hosts: worker
become: true
vars:
node_conn_string: "{{ lookup('ansible.builtin.file', 'Remote_Files/worker_conn_string') }}"
tasks:
- name: 1. Add Worker Nodes To The Controller
ansible.builtin.command: '{{ node_conn_string }}'
changed_when: false
throttle: 1