-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServerPrepare.yaml
330 lines (242 loc) · 8.83 KB
/
ServerPrepare.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
318
319
320
321
322
323
324
325
326
327
328
329
330
# sudo ansible servers -m ping -i inventory.ini -u root
# sudo ansible-playbook -i inventory.ini Kubernetes/ServerPrepare.yml -u root
---
- hosts: all
become: yes
vars:
user: "{{ ansible_env.USER }}"
tasks:
- name: Print initialization (0)
debug:
msg: "Installing docker 24.0.0 on ubuntu 22.04 is going to start ..."
- name: Disable swap (1)
shell: |
swapoff -a
- name: Disable swap in fstab (2)
replace:
path: /etc/fstab
regexp: '^([^#].*?\sswap\s+sw\s+.*)$'
replace: '# \1'
- name: Configure hostname (3)
ansible.builtin.hostname:
name: "{{ inventory_hostname }}"
become: true
- name: Add IP address of all hosts to all hosts (4)
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}$'
line: "{{ hostvars[item].ansible_host }} {{item}}"
state: present
when: hostvars[item].ansible_host is defined
with_items: "{{ groups.all }}"
- name: Disable the ufw firewall (on Ubuntu, if configured). (5)
service:
name: ufw
state: stopped
enabled: false
when:
- ansible_distribution == "Ubuntu"
#-------------------------------------------------------------------
- name: Docker initialization
debug:
msg: "Installation of docker is going to start ...
(based on https://docs.docker.com/engine/install/ubuntu/"
- name: Install required packages (6)
ansible.builtin.apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- gpg
- gnupg
- software-properties-common
- name: Create directory for Docker's GPG key (6.1)
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Add Docker GPG key (6.2)
get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
- name: Get DEB architecture
shell: dpkg --print-architecture
register: deb_architecture
- name: Print DEB architecture
debug:
msg: "deb_architecture.stdout: {{ deb_architecture.stdout }}"
- name: Add Docker repo (6.3)
ansible.builtin.apt_repository:
filename: docker
repo: 'deb [arch={{ deb_architecture.stdout }} signed-by=/etc/apt/keyrings/docker.asc]
https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable'
state: present
- name: Update Repository cache (7)
apt:
update_cache: true
cache_valid_time: 3600
force_apt_get: true
- name: Downloding docker (8)
debug:
msg: "Downloding docker & containerd ..."
- name: Install Docker stuff (9)
apt:
pkg:
- docker-ce=5:24.0.0-1~ubuntu.22.04~jammy
- docker-ce-cli=5:24.0.0-1~ubuntu.22.04~jammy
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- name: Enable and start Docker services (10)
ansible.builtin.systemd:
name: "{{ item }}"
enabled: true
state: started
loop:
- docker.service
- containerd.service
#-------------------------------------------------------------------
- name: cri-dockerd initialization
debug:
msg: "Installation of cri-dockerd is going to start
(based on https://github.com/Mirantis/cri-dockerd)"
- name: Create directory for cri-dockerd
ansible.builtin.file:
path: /opt/cri-dockerd
state: directory
mode: '0755'
# you can download & install .deb package instead from :
# https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.10/cri-dockerd_0.3.10.3-0.ubuntu-jammy_amd64.deb
- name: Download cri-dockerd
get_url:
url: https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.10/cri-dockerd-0.3.10.amd64.tgz
dest: /opt/cri-dockerd
timeout: 300
- name: Extract cri-dockerd-0.3.10.amd64.tgz into /opt/cri-dockerd
ansible.builtin.unarchive:
src: /opt/cri-dockerd/cri-dockerd-0.3.10.amd64.tgz
dest: /opt/cri-dockerd
remote_src: yes
owner: root
group: root
mode: '775'
- name: Create directory for opt/packaging/systemd
ansible.builtin.file:
path: /opt/packaging/systemd
state: directory
owner: root
group: root
recurse: yes
mode: '0755'
- name: Download cri-dockerd.service & cri-dockerd.socket
get_url:
url: "{{ item }}"
dest: /opt/packaging/systemd
loop:
- https://github.com/Mirantis/cri-dockerd/raw/master/packaging/systemd/cri-docker.service
- https://github.com/Mirantis/cri-dockerd/raw/master/packaging/systemd/cri-docker.socket
- name: Install cri-dockerd service & socket
ansible.builtin.copy:
src: /opt/cri-dockerd/cri-dockerd/cri-dockerd
dest: /usr/local/bin/cri-dockerd
owner: root
group: root
mode: '0755'
remote_src: yes
- name: Install cri-dockerd service & socket
ansible.builtin.copy:
src: "{{ item }}"
dest: /etc/systemd/system
remote_src: yes
loop:
- /opt/packaging/systemd/cri-docker.service
- /opt/packaging/systemd/cri-docker.socket
- name: Install cri-dockerd service & socket
ansible.builtin.shell: "{{ item }}"
with_items:
- sed -i -e 's,/usr/bin/cri-dockerd,/usr/local/bin/cri-dockerd,' /etc/systemd/system/cri-docker.service
- name: Just force systemd to reread configs
ansible.builtin.systemd_service:
daemon_reload: true
- name: Enable and start cri-dockerd services
ansible.builtin.systemd:
name: "cri-docker.service"
enabled: true
state: started
- name: Enable and start cri-dockerd services
ansible.builtin.systemd:
name: "cri-docker.socket"
enabled: true
state: started
#-------------------------------------------------------------------
- name: kubeadm, kubelet and kubectl initialization ...
debug:
msg: "Installing kubeadm, kubelet and kubectl is going to start
(based on https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/)"
- name: Add Kubernetes GPG key
ansible.builtin.shell: "{{ item }}"
with_items:
- curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key |
sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
- name: Add Kubernetes repo
ansible.builtin.apt_repository:
filename: kubernetes
repo: 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg]
https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /'
state: present
- name: Update Repository cache
apt:
update_cache: true
cache_valid_time: 3600
force_apt_get: true
- name: Downloding kubelet, kubeadm and kubectl ...
debug:
msg: "Downloding docker & containerd ..."
- name: Install kubelet, kubeadm and kubectl
ansible.builtin.apt:
pkg:
- kubelet
- kubeadm
- kubectl
- name: Hold kubelet, kubeadm and kubectl
ansible.builtin.dpkg_selections:
name: "{{ item }}"
selection: hold
loop:
- kubelet
- kubeadm
- kubectl
#-------------------------------------------------------------------
- name: Extra Configs initialization ...
debug:
msg: "Extra Configs started ..."
- name: Set Docker CgroupDriver (systemd)
ansible.builtin.shell: |
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
- name: reload & restart docker & kubelet ...
ansible.builtin.shell: "{{ item }}"
with_items:
- sudo systemctl daemon-reload
- sudo systemctl restart docker
- sudo systemctl restart kubelet
- name: Network Configs ...
ansible.builtin.shell: |
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
- name: Forwarding IPv4 and letting iptables see bridged traffic
ansible.builtin.shell: |
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
- name: Apply sysctl params without reboot (kernel configs) ...
ansible.builtin.shell: "{{ item }}"
with_items:
- sudo sysctl --system