initial commit

This commit is contained in:
2026-06-14 14:21:09 +07:00
commit 054835e2a7
17 changed files with 771 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
---
- name: Common OS prerequisites for all k8s nodes
hosts: k8s
become: true
tasks:
- name: Disable swap now
command: swapoff -a
when: ansible_facts['swaptotal_mb'] > 0
- name: Remove swap from /etc/fstab
replace:
path: /etc/fstab
regexp: '^([^#].*\s+swap\s+.*)$'
replace: '# \1'
- name: Load kernel modules at boot
copy:
dest: /etc/modules-load.d/k8s.conf
content: |
overlay
br_netfilter
- name: Load kernel modules now
modprobe:
name: "{{ item }}"
loop:
- overlay
- br_netfilter
- name: Set sysctl for k8s networking
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

View File

@@ -0,0 +1,15 @@
---
- name: Configure /etc/hosts for all nodes
hosts: k8s
become: true
tasks:
- name: Add cluster nodes to /etc/hosts
blockinfile:
path: /etc/hosts
marker: "# {mark} k8s cluster nodes"
block: |
192.168.1.31 master01
192.168.1.35 worker01
192.168.1.36 worker02
192.168.1.37 worker03

View File

@@ -0,0 +1,35 @@
---
- name: Install and configure containerd
hosts: k8s
become: true
tasks:
- name: Add Docker CE repo (provides containerd.io)
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docker-ce.repo
- name: Install containerd
dnf:
name: containerd.io
state: present
- name: Create containerd config directory
file:
path: /etc/containerd
state: directory
- name: Generate default containerd config (overwrites Docker CE default which disables CRI)
shell: containerd config default > /etc/containerd/config.toml
- name: Enable SystemdCgroup in containerd config
replace:
path: /etc/containerd/config.toml
regexp: 'SystemdCgroup = false'
replace: 'SystemdCgroup = true'
- name: Enable and start containerd
systemd:
name: containerd
enabled: true
state: restarted

View File

@@ -0,0 +1,35 @@
---
- name: Install kubeadm, kubelet, kubectl
hosts: k8s
become: true
vars:
kube_version: "1.32"
tasks:
- name: Add Kubernetes repo
copy:
dest: /etc/yum.repos.d/kubernetes.repo
content: |
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v{{ kube_version }}/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v{{ kube_version }}/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
- name: Install kubeadm, kubelet, kubectl
dnf:
name:
- kubelet
- kubeadm
- kubectl
state: present
disable_excludes: kubernetes
- name: Enable kubelet
systemd:
name: kubelet
enabled: true
state: started

View File

@@ -0,0 +1,67 @@
---
- name: Initialize Kubernetes master
hosts: masters
become: true
vars:
pod_network_cidr: "10.244.0.0/16" # Flannel default
tasks:
- name: Check if cluster already initialized
stat:
path: /etc/kubernetes/admin.conf
register: kubeadm_init_done
- name: Run kubeadm init
command: >
kubeadm init
--apiserver-advertise-address={{ ansible_host }}
--pod-network-cidr={{ pod_network_cidr }}
when: not kubeadm_init_done.stat.exists
register: kubeadm_output
- name: Create .kube dir for cloud-user
file:
path: /home/cloud-user/.kube
state: directory
mode: "0700"
owner: cloud-user
group: cloud-user
- name: Copy admin.conf to cloud-user kubeconfig
copy:
src: /etc/kubernetes/admin.conf
dest: /home/cloud-user/.kube/config
remote_src: true
owner: cloud-user
group: cloud-user
mode: "0600"
- name: Create .kube dir for root (enables sudo kubectl)
file:
path: /root/.kube
state: directory
mode: "0700"
- name: Copy admin.conf to root kubeconfig
copy:
src: /etc/kubernetes/admin.conf
dest: /root/.kube/config
remote_src: true
mode: "0600"
- name: Install Flannel CNI
command: kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
environment:
KUBECONFIG: /etc/kubernetes/admin.conf
- name: Get join command
command: kubeadm token create --print-join-command
register: join_command
- name: Save join command to local file
local_action:
module: copy
content: "{{ join_command.stdout }}"
dest: /tmp/k8s_join_command.sh
mode: "0600"

View File

@@ -0,0 +1,21 @@
---
- name: Join workers to Kubernetes cluster
hosts: workers
become: true
tasks:
- name: Check if already joined
stat:
path: /etc/kubernetes/kubelet.conf
register: kubelet_conf
- name: Read join command from local file
local_action:
module: slurp
src: /tmp/k8s_join_command.sh
register: join_command_b64
when: not kubelet_conf.stat.exists
- name: Join cluster
command: "{{ join_command_b64.content | b64decode | trim }}"
when: not kubelet_conf.stat.exists