initial commit

This commit is contained in:
2026-05-21 10:30:21 +03:30
commit ab19062111
8 changed files with 241 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
[master]
master1 ansible_host=192.168.1.10 ansible_user=ansible
[workers]
worker1 ansible_host=192.168.1.11 ansible_user=ansible
worker2 ansible_host=192.168.1.12 ansible_user=ansible
[k8s_cluster:children]
master
workers
[all:vars]
ansible_python_interpreter=/usr/bin/python3
+35
View File
@@ -0,0 +1,35 @@
- name: Disable swap
command: swapoff -a
changed_when: false
- name: Remove swap from fstab
replace:
path: /etc/fstab
regexp: '^(.*\s+swap\s+.*)$'
replace: '# \1'
- name: Load kernel modules config
copy:
dest: /etc/modules-load.d/k8s.conf
content: |
overlay
br_netfilter
- name: Load kernel modules
modprobe:
name: "{{ item }}"
loop:
- overlay
- br_netfilter
- name: Sysctl for Kubernetes
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
changed_when: false
+78
View File
@@ -0,0 +1,78 @@
- name: Install prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
state: present
update_cache: yes
- name: Create keyrings directory
file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Download Docker GPG key for Ubuntu
get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
mode: '0644'
when: ansible_distribution == "Ubuntu"
- name: Download Docker GPG key for Debian
get_url:
url: https://download.docker.com/linux/debian/gpg
dest: /etc/apt/keyrings/docker.asc
mode: '0644'
when: ansible_distribution == "Debian"
- name: Add Docker repository for Ubuntu
apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
filename: docker
when: ansible_distribution == "Ubuntu"
- name: Add Docker repository for Debian
apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian bookworm stable"
state: present
filename: docker
when: ansible_distribution == "Debian"
- name: Install containerd
apt:
name: containerd.io
state: present
update_cache: yes
- name: Ensure containerd config dir
file:
path: /etc/containerd
state: directory
- name: Generate default config
shell: containerd config default > /etc/containerd/config.toml
args:
creates: /etc/containerd/config.toml
- name: Enable SystemdCgroup
replace:
path: /etc/containerd/config.toml
regexp: 'SystemdCgroup = false'
replace: 'SystemdCgroup = true'
- name: Remove disabled CRI plugin if present
replace:
path: /etc/containerd/config.toml
regexp: '^\s*disabled_plugins\s*=\s*\[\s*"cri"\s*\]'
replace: '# disabled_plugins = ["cri"]'
- name: Restart containerd
systemd:
name: containerd
state: restarted
enabled: yes
+46
View File
@@ -0,0 +1,46 @@
- name: Install prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- conntrack
- gpg
state: present
update_cache: yes
- name: Create Kubernetes keyrings directory
file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Download Kubernetes key
shell: |
curl -fsSL https://pkgs.k8s.io/core:/stable:/v{{ kubernetes_version }}/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
args:
creates: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
- name: Add Kubernetes repo
copy:
dest: /etc/apt/sources.list.d/kubernetes.list
content: |
deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v{{ kubernetes_version }}/deb/ /
- name: Install kube packages
apt:
name:
- kubelet
- kubeadm
- kubectl
state: present
update_cache: yes
- name: Hold kube packages
dpkg_selections:
name: "{{ item }}"
selection: hold
loop:
- kubelet
- kubeadm
- kubectl
+33
View File
@@ -0,0 +1,33 @@
- name: Check if cluster initialized
stat:
path: /etc/kubernetes/admin.conf
register: k8s_admin_conf
- name: Initialize cluster
shell: kubeadm init --apiserver-advertise-address={{ control_plane_endpoint }} --pod-network-cidr={{ pod_network_cidr }}
when: not k8s_admin_conf.stat.exists
register: kubeadm_init
- name: Create kube dir
file:
path: /root/.kube
state: directory
- name: Copy admin.conf
copy:
src: /etc/kubernetes/admin.conf
dest: /root/.kube/config
remote_src: yes
- name: Install flannel
shell: kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
environment:
KUBECONFIG: /etc/kubernetes/admin.conf
- name: Generate join command
shell: kubeadm token create --print-join-command
register: join_command_raw
- name: Set join command fact
set_fact:
join_command: "{{ join_command_raw.stdout }}"
+8
View File
@@ -0,0 +1,8 @@
- name: Check if node joined
stat:
path: /etc/kubernetes/kubelet.conf
register: kubelet_conf
- name: Join node
shell: "{{ hostvars[groups['master'][0]]['join_command'] }}"
when: not kubelet_conf.stat.exists
+25
View File
@@ -0,0 +1,25 @@
- name: Prepare all Kubernetes nodes
hosts: k8s_cluster
become: yes
vars_files:
- vars.yml
roles:
- common
- containerd
- kubernetes
- name: Initialize control plane
hosts: master
become: yes
vars_files:
- vars.yml
roles:
- master
- name: Join worker nodes
hosts: workers
become: yes
vars_files:
- vars.yml
roles:
- worker
+3
View File
@@ -0,0 +1,3 @@
kubernetes_version: "1.30"
pod_network_cidr: "10.244.0.0/16"
control_plane_endpoint: "192.168.1.10"