mirror of
https://gitlab.sectorq.eu/jaydee/ansible.git
synced 2026-03-13 05:42:46 +01:00
253 lines
6.7 KiB
YAML
253 lines
6.7 KiB
YAML
- name: Install Kubernetes on Ubuntu 24
|
|
become: "{{ 'no' if inventory_hostname == 'nas.home.lan' else 'yes' }}"
|
|
block:
|
|
|
|
- name: Update apt cache
|
|
apt:
|
|
update_cache: yes
|
|
|
|
- name: Disable swap
|
|
command: swapoff -a
|
|
when: ansible_swaptotal_mb > 0
|
|
|
|
- name: Remove swap from fstab
|
|
replace:
|
|
path: /etc/fstab
|
|
regexp: '.*swap.*'
|
|
replace: ''
|
|
|
|
- name: Enable kernel modules
|
|
copy:
|
|
dest: /etc/modules-load.d/k8s.conf
|
|
content: |
|
|
overlay
|
|
br_netfilter
|
|
|
|
- name: Load kernel modules
|
|
modprobe:
|
|
name: "{{ item }}"
|
|
state: present
|
|
loop:
|
|
- overlay
|
|
- br_netfilter
|
|
|
|
- name: Set sysctl params
|
|
copy:
|
|
dest: /etc/sysctl.d/k8s.conf
|
|
content: |
|
|
net.bridge.bridge-nf-call-iptables = 1
|
|
net.bridge.bridge-nf-call-ip6tables = 1
|
|
net.ipv4.ip_forward = 1
|
|
|
|
- name: Apply sysctl
|
|
command: sysctl --system
|
|
|
|
- name: Install required packages
|
|
apt:
|
|
name:
|
|
- apt-transport-https
|
|
- ca-certificates
|
|
- curl
|
|
- gpg
|
|
- curl
|
|
- gnupg2
|
|
- software-properties-common
|
|
state: present
|
|
|
|
- name: Remove old Docker repository files (if any)
|
|
file:
|
|
path: "{{ item }}"
|
|
state: absent
|
|
loop:
|
|
- /etc/apt/sources.list.d/docker.list
|
|
- /etc/apt/keyrings/docker.gpg
|
|
- /etc/apt/trusted.gpg.d/containerd.gpg
|
|
|
|
- name: Create keyrings directory
|
|
file:
|
|
path: /etc/apt/keyrings
|
|
state: directory
|
|
mode: '0755'
|
|
|
|
- name: Download Docker GPG key
|
|
get_url:
|
|
url: https://download.docker.com/linux/ubuntu/gpg
|
|
dest: /etc/apt/keyrings/docker.gpg
|
|
mode: '0644'
|
|
|
|
- name: Add Docker repository (Ubuntu 24 safe)
|
|
apt_repository:
|
|
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable"
|
|
state: present
|
|
filename: docker
|
|
update_cache: no
|
|
|
|
- name: Update apt cache
|
|
apt:
|
|
update_cache: yes
|
|
- name: Install required packages
|
|
apt:
|
|
name:
|
|
- containerd.io
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Generate default containerd config
|
|
command: containerd config default
|
|
register: containerd_config
|
|
|
|
- name: Save containerd configuration
|
|
copy:
|
|
dest: /etc/containerd/config.toml
|
|
content: "{{ containerd_config.stdout }}"
|
|
owner: root
|
|
group: root
|
|
mode: 0644
|
|
- name: Enable SystemdCgroup in containerd config
|
|
replace:
|
|
path: /etc/containerd/config.toml
|
|
regexp: 'SystemdCgroup\s*=\s*false'
|
|
replace: 'SystemdCgroup = true'
|
|
|
|
- name: Restart containerd
|
|
systemd:
|
|
name: containerd
|
|
state: restarted
|
|
enabled: yes
|
|
|
|
- name: Add Kubernetes GPG key
|
|
ansible.builtin.apt_key:
|
|
url: https://pkgs.k8s.io/core:/stable:/v1.34/deb/Release.key
|
|
keyring: /etc/apt/keyrings/k8s.gpg
|
|
state: present
|
|
|
|
- name: Add Kubernetes repository
|
|
copy:
|
|
dest: /etc/apt/sources.list.d/k8s.list
|
|
content: 'deb [signed-by=/etc/apt/keyrings/k8s.gpg] https://pkgs.k8s.io/core:/stable:/v1.34/deb/ /'
|
|
owner: root
|
|
group: root
|
|
mode: 0644
|
|
|
|
- name: Install Kubernetes packages
|
|
apt:
|
|
name:
|
|
- kubelet
|
|
- kubeadm
|
|
- kubectl
|
|
state: present
|
|
update_cache: yes
|
|
|
|
register: k8s_install
|
|
retries: 5
|
|
delay: 10
|
|
until: k8s_install is succeeded
|
|
|
|
- name: Hold Kubernetes packages
|
|
command: apt-mark hold kubelet kubeadm kubectl
|
|
|
|
- name: Enable kubelet
|
|
systemd:
|
|
name: kubelet
|
|
enabled: yes
|
|
state: started
|
|
|
|
# Optional UFW configuration
|
|
- name: Ensure UFW is installed
|
|
apt:
|
|
name: ufw
|
|
state: present
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
|
|
- name: Allow Kubernetes ports
|
|
ufw:
|
|
rule: allow
|
|
port: "{{ item }}"
|
|
proto: tcp
|
|
loop:
|
|
- 6443
|
|
- 2379:2380
|
|
- 10250
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
|
|
- name: Configure containerd for kubeadm
|
|
copy:
|
|
dest: /etc/containerd/config.toml
|
|
content: |
|
|
[plugins."io.containerd.grpc.v1.cri".containerd]
|
|
snapshotter = "overlayfs"
|
|
|
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
|
|
runtime_type = "io.containerd.runc.v2"
|
|
|
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
|
|
SystemdCgroup = true
|
|
|
|
- name: Enable and start containerd
|
|
systemd:
|
|
name: containerd
|
|
state: restarted
|
|
|
|
- name: Pause for 5 minutes to build app cache
|
|
ansible.builtin.pause:
|
|
minutes: 2
|
|
|
|
- name: Create .kube directory
|
|
file:
|
|
path: /home/{{ ansible_user }}/.kube
|
|
state: directory
|
|
owner: "{{ ansible_user }}"
|
|
group: "{{ ansible_user }}"
|
|
mode: '0755'
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
|
|
- name: Initialize Kubernetes
|
|
command: sudo kubeadm init --pod-network-cidr=10.244.0.0/16
|
|
args:
|
|
creates: /etc/kubernetes/admin.conf
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
|
|
|
|
- name: Copy kubeconfig to user
|
|
copy:
|
|
remote_src: yes
|
|
src: /etc/kubernetes/admin.conf
|
|
dest: /home/{{ ansible_user }}/.kube/config
|
|
owner: "{{ ansible_user }}"
|
|
group: "{{ ansible_user }}"
|
|
mode: '0644'
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
- name: Pause for 5 minutes to build app cache
|
|
ansible.builtin.pause:
|
|
minutes: 2
|
|
|
|
- name: Install Flannel CNI
|
|
ansible.builtin.command:
|
|
cmd: kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
|
|
environment:
|
|
KUBECONFIG: /etc/kubernetes/admin.conf
|
|
when: inventory_hostname == "ubuntu24-vm01.home.lan"
|
|
register: flannel_result
|
|
changed_when: "'created' in flannel_result.stdout or 'configured' in flannel_result.stdout"
|
|
|
|
- name: Get join command
|
|
command: sudo kubeadm token create --print-join-command
|
|
register: join_command
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name: Save join command
|
|
set_fact:
|
|
worker_join_cmd: "{{ join_command.stdout }}"
|
|
when: inventory_hostname == 'ubuntu24-vm01.home.lan'
|
|
|
|
- name: Join cluster
|
|
command: "{{ hostvars['ubuntu24-vm01.home.lan'].worker_join_cmd }}"
|
|
args:
|
|
creates: /etc/kubernetes/kubelet.conf
|
|
when: inventory_hostname != 'ubuntu24-vm01.home.lan' |