-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbook.yml
280 lines (239 loc) · 7.86 KB
/
playbook.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
- hosts: all
become: true
tasks:
- name: Assign inventory name to hostname
hostname:
name: "{{ inventory_hostname }}"
- name: Disable SELinux at boot (RedHat)
selinux:
state: disabled
when: ansible_os_family == 'RedHat'
- name: Stop SELinux (RedHat)
command: setenforce 0
when: ansible_os_family == 'RedHat'
- name: Get list of services
service_facts:
- name: Stop firewalld and disable it at boot
systemd:
name: firewalld
state: stopped
enabled: no
when:
- "'firewalld.service' in ansible_facts.services"
- ansible_facts.services['firewalld.service'].status != 'not-found'
- name: Stop swap
command: swapoff -a
- name: Disable swap at boot
replace:
path: /etc/fstab
regexp: ^([^#].*\sswap\s.*)$
replace: '# \1'
- name: Disable swap.target (Debian)
systemd:
name: swap.target
masked: true
when: ansible_os_family == 'Debian'
- name: Enable required kernel modules at boot
lineinfile:
path: /etc/modules-load.d/containerd.conf
line: "{{ item }}"
create: true
loop:
- overlay
- br_netfilter
- name: Load required kernel modules
modprobe:
name: "{{ item }}"
loop:
- overlay
- br_netfilter
- name: Setup required sysctl params
sysctl:
name: "{{ item.name }}"
value: "{{ item.value }}"
loop:
- name: net.bridge.bridge-nf-call-iptables
value: "1"
- name: net.bridge.bridge-nf-call-ip6tables
value: "1"
- name: net.ipv4.ip_forward
value: "1"
- name: Add Docker repository (RedHat)
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docker-ce.repo
when: ansible_os_family == 'RedHat'
- name: Install curl (Debian)
apt:
name: curl
update_cache: true
when: ansible_os_family == 'Debian'
- name: Create APT keyrings directory
file:
path: /etc/apt/keyrings
state: directory
- name: Add Docker repository (Debian)
shell: |
set -euxo pipefail
curl -fsSL https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg -o /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/{{ ansible_distribution | lower }} $(. /etc/os-release && echo "$VERSION_CODENAME") stable" > /etc/apt/sources.list.d/docker.list
args:
executable: /bin/bash
when: ansible_os_family == 'Debian'
- name: Install containerd (RedHat)
yum:
name: containerd.io
when: ansible_os_family == 'RedHat'
- name: Install containerd (Debian)
apt:
name: containerd.io
update_cache: true
when: ansible_os_family == 'Debian'
- name: Get containerd default config
command: containerd config default
register: get_containerd_default_config
- name: Set containerd config
copy:
content: "{{ get_containerd_default_config.stdout }}"
dest: /etc/containerd/config.toml
- name: Set containerd cgroup driver to systemd
replace:
path: /etc/containerd/config.toml
regexp: SystemdCgroup = false
replace: SystemdCgroup = true
- name: Restart containerd
systemd:
name: containerd
state: restarted
- name: Start containerd and enable it at boot
systemd:
name: containerd
state: started
enabled: true
- name: Set Kubernetes version
set_fact:
kubernetes_version: "{{ kubernetes_version | default('v1.31') }}"
- name: Add Kubernetes repository (RedHat)
copy:
content: |
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version }}/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version }}/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
dest: /etc/yum.repos.d/kubernetes.repo
when: ansible_os_family == 'RedHat'
- name: Install gpg (Debian)
apt:
name: gpg
update_cache: true
when: ansible_os_family == 'Debian'
- name: Add Kubernetes repository (Debian)
shell: |
set -euxo pipefail
curl -fsSL https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version }}/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg --yes
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version }}/deb/ /' > /etc/apt/sources.list.d/kubernetes.list
args:
executable: /bin/bash
when: ansible_os_family == 'Debian'
- name: Install Kubernetes packages (RedHat)
yum:
name:
- kubelet
- kubeadm
- kubectl
disable_excludes: kubernetes
when: ansible_os_family == 'RedHat'
- name: Install Kubernetes packages (Debian)
apt:
name:
- kubelet
- kubeadm
- kubectl
update_cache: true
when: ansible_os_family == 'Debian'
- name: Start kubelet and enable it at boot
systemd:
name: kubelet
state: started
enabled: true
- name: Initialize Kubernetes master node
command:
cmd: kubeadm init {{ kubernetes_kubeadm_init_extra_args | default('') }}
creates: /etc/kubernetes/admin.conf
when: kubernetes_role == 'master'
- name: Fetch kubeconfig
fetch:
src: /etc/kubernetes/admin.conf
dest: kubeconfig
flat: true
when: kubernetes_role == 'master'
- name: Create kubeconfig directory
file:
path: ~/.kube
state: directory
become: false
when: kubernetes_role == 'master'
- name: Get kubeconfig
slurp:
src: /etc/kubernetes/admin.conf
register: get_kubeconfig
when: kubernetes_role == 'master'
- name: Copy kubeconfig
copy:
content: "{{ get_kubeconfig.content | b64decode }}"
dest: ~/.kube/config
mode: 0600
become: false
when: kubernetes_role == 'master'
- name: Allow pods on master node
command: kubectl taint nodes --all node-role.kubernetes.io/"{{ item }}"-
loop:
- master
- control-plane
become: false
when: kubernetes_role == 'master'
failed_when: false
- name: Get kubeadm join command
command: kubeadm token create --print-join-command
when: kubernetes_role == 'master'
register: get_kubeadm_join_command
- name: Set kubeadm join command globally
set_fact:
kubernetes_join_command: "{{ get_kubeadm_join_command.stdout }}"
when: get_kubeadm_join_command.stdout is defined
delegate_to: "{{ item }}"
delegate_facts: true
with_items: "{{ groups['all'] }}"
- name: Join Kubernetes node
command:
cmd: "{{ kubernetes_join_command }}"
creates: /etc/kubernetes/kubelet.conf
when: kubernetes_role == 'node'
- name: Install NFS, iSCSI & NVMe utils (RedHat)
yum:
name:
- nfs-utils
- iscsi-initiator-utils
- nvme-cli
when: ansible_os_family == 'RedHat'
- name: Install NFS, iSCSI & NVMe utils (Debian)
apt:
name:
- nfs-common
- open-iscsi
- nvme-cli
update_cache: true
when: ansible_os_family == 'Debian'
- name: Load NVMe TCP kernel module
modprobe:
name: nvme_tcp
state: present
persistent: present
- name: Load NVMe RDMA kernel module
modprobe:
name: nvme_rdma
state: present
persistent: present