polish validation and project structure

This commit is contained in:
Bartal Læarsson
2025-02-28 11:32:10 +00:00
parent 20d1964db3
commit 24d9859b2d
5 changed files with 219 additions and 129 deletions
+18 -1
View File
@@ -1,6 +1,6 @@
---
# Kubernetes configuration
kubernetes_version: "1.32"
kubernetes_version: "1.28"
pod_cidr: "192.168.0.0/16"
service_cidr: "10.96.0.0/16"
@@ -20,3 +20,20 @@ worker_disk: 20G
dns_servers:
- "8.8.8.8"
- "8.8.4.4"
# Kubernetes packages
kubernetes_packages:
- kubelet
- kubeadm
- kubectl
# Containerd configuration
containerd_version: "2.0.0"
containerd_arch: "amd64"
containerd_checksum: "sha256:6f8da716941f7e89315cefaa6e5a8f1ff10b323ff46611313c455df7ab1ebee1"
containerd_work_dir: "/tmp/containerd-install"
containerd_install_root: "/usr/local"
containerd_config_root: "/etc/containerd"
containerd_systemd_root: "/etc/systemd/system"
containerd_binary_path: "{{ containerd_install_root }}/bin"
containerd_group: "containerd"
+9
View File
@@ -1,4 +1,5 @@
---
# ansible/playbooks/kubernetes.yml
- name: Common configuration for all nodes
hosts: k8s_cluster
become: yes
@@ -21,6 +22,12 @@
- k8s-setup
tasks:
- include_tasks: ../tasks/kubernetes/node-setup.yml
handlers:
- name: Restart containerd
systemd:
name: containerd
state: restarted
enabled: yes
- name: Initialize Kubernetes control plane
hosts: control_plane
@@ -45,6 +52,8 @@
- name: Validate Kubernetes cluster
hosts: k8s_cluster
become: yes
vars_files:
- ../defaults/kubernetes.yml
tags:
- validate
tasks:
@@ -0,0 +1,122 @@
---
# ansible/tasks/kubernetes/install-containerd.yml
- name: Create work directory for containerd installation
file:
path: "{{ containerd_work_dir }}"
state: directory
mode: '0755'
- name: Download containerd tarball
get_url:
url: "https://github.com/containerd/containerd/releases/download/v{{ containerd_version }}/containerd-{{ containerd_version }}-linux-{{ containerd_arch }}.tar.gz"
dest: "{{ containerd_work_dir }}/containerd.tar.gz"
checksum: "{{ containerd_checksum }}"
mode: '0644'
- name: Extract containerd binaries to install directory
unarchive:
src: "{{ containerd_work_dir }}/containerd.tar.gz"
dest: "{{ containerd_install_root }}"
remote_src: yes
- name: Create containerd config directory
file:
path: "{{ containerd_config_root }}"
state: directory
mode: '0755'
- name: Detect if containerd is already installed
stat:
path: "{{ containerd_binary_path }}/containerd"
register: containerd_binary
- name: Generate default containerd configuration
command: "{{ containerd_binary_path }}/containerd config default"
register: default_config
changed_when: false
when: containerd_binary.stat.exists
- name: Ensure containerd group exists
group:
name: "{{ containerd_group }}"
system: yes
state: present
- name: Get containerd group GID
shell: "getent group {{ containerd_group }} | cut -d: -f3"
register: containerd_gid_cmd
changed_when: false
- name: Write containerd configuration file
copy:
content: "{{ default_config.stdout }}"
dest: "{{ containerd_config_root }}/config.toml"
mode: '0644'
when: containerd_binary.stat.exists and default_config.stdout is defined
register: config_written
- name: Update containerd config for SystemdCgroup
replace:
path: "{{ containerd_config_root }}/config.toml"
regexp: 'SystemdCgroup = false'
replace: 'SystemdCgroup = true'
when: config_written.changed
notify: Restart containerd
- name: Update containerd config for GRPC socket group (gid)
replace:
path: "{{ containerd_config_root }}/config.toml"
regexp: '^(\s*gid\s*=\s*)0'
replace: '\g<1>{{ containerd_gid_cmd.stdout }}'
when: config_written.changed
notify: Restart containerd
- name: Create containerd systemd service file
copy:
dest: "{{ containerd_systemd_root }}/containerd.service"
mode: '0644'
content: |
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target network-online.target
[Service]
ExecStart={{ containerd_binary_path }}/containerd --config {{ containerd_config_root }}/config.toml
Restart=always
RestartSec=5
Delegate=yes
KillMode=process
OOMScoreAdjust=-999
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
[Install]
WantedBy=multi-user.target
notify: Restart containerd
- name: Add current user to containerd group
user:
name: "{{ ansible_user }}"
groups: "{{ containerd_group }}"
append: yes
- name: Reload systemd daemon
systemd:
daemon_reload: yes
- name: Enable and start containerd service
systemd:
name: containerd
enabled: yes
state: started
- name: Set permissions for containerd socket
file:
path: /run/containerd/containerd.sock
owner: root
group: "{{ containerd_group }}"
mode: '0660'
when: ansible_system_vendor != 'Microsoft Corporation' # Avoid issues on WSL2
+5 -77
View File
@@ -1,5 +1,5 @@
---
# tasks/node-setup.yml
# tasks/kubernetes/node-setup.yml
- name: Update apt cache
apt:
update_cache: yes
@@ -13,83 +13,11 @@
- gnupg
state: present
# Add Docker GPG key
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
filename: docker
- name: Install containerd from Docker repository
apt:
name: containerd.io
state: present
update_cache: yes
- name: Create containerd directory
file:
path: /etc/containerd
state: directory
mode: '0755'
- name: Check if containerd config command works
command: which containerd
register: containerd_check
changed_when: false
failed_when: false
- name: Generate default containerd config (if containerd exists)
command: containerd config default
register: containerd_config
changed_when: false
when: containerd_check.rc == 0
- name: Create a basic containerd config (if command failed)
copy:
content: |
version = 2
[plugins]
[plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".containerd]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
[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
dest: /etc/containerd/config.toml
owner: root
group: root
mode: '0644'
when: containerd_check.rc != 0
- name: Write containerd config from generated output
copy:
content: "{{ containerd_config.stdout }}"
dest: /etc/containerd/config.toml
owner: root
group: root
mode: '0644'
when: containerd_check.rc == 0 and containerd_config.stdout is defined and containerd_config.stdout != ""
- name: Enable SystemdCgroup in containerd config
replace:
path: /etc/containerd/config.toml
regexp: 'SystemdCgroup = false'
replace: 'SystemdCgroup = true'
when: containerd_check.rc == 0
- name: Restart containerd
systemd:
name: containerd
state: restarted
enabled: yes
# Install containerd directly from GitHub release
- name: Install containerd directly from release
include_tasks: install-containerd.yml
# Continue with Kubernetes repository setup and installation
- name: Get latest Kubernetes version
shell: curl -L -s https://dl.k8s.io/release/stable.txt | awk 'BEGIN { FS="." } { printf "%s.%s", $1, $2 }'
register: kube_latest
+65 -51
View File
@@ -1,84 +1,99 @@
---
# ansible/tasks/kubernetes/validate-cluster.yml
- name: Check control plane components
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods -n kube-system
register: control_plane_pods
changed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
when: "'control_plane' in group_names"
- name: Check node status
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf get nodes
register: node_status
changed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Wait for all pods to be ready
- name: Display node status
debug:
var: node_status.stdout_lines
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Check all pods status
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods --all-namespaces
register: all_pods
changed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Display all pods status
debug:
var: all_pods.stdout_lines
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Count pods not in Running or Completed state
shell: |
kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods --all-namespaces | grep -v 'Running\|Completed' || echo "All pods running"
register: pod_status
until: pod_status.stdout == "All pods running"
retries: 30
delay: 10
kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods --all-namespaces | grep -v "Running\|Completed" | grep -v "NAME" | wc -l
register: non_running_pods
changed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Verify worker node joined successfully
- name: Display non-running pods count
debug:
var: non_running_pods.stdout
delegate_to: "{{ groups['control_plane'][0] }}"
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf get nodes {{ inventory_hostname }} -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'
register: node_ready
until: node_ready.stdout == "True"
retries: 10
delay: 10
changed_when: false
when: "'workers' in group_names"
- name: Verify CNI pods are running
- name: Verify all pods are ready
debug:
msg: "All pods are in Running or Completed state"
when: non_running_pods.stdout|int == 0
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Display problematic pods if any
shell: |
kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods -n calico-system --no-headers | wc -l
register: cni_pod_count
until: cni_pod_count.stdout|int > 0
retries: 10
delay: 10
kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods --all-namespaces | grep -v "Running\|Completed" | grep -v "NAME"
register: problem_pods
when: non_running_pods.stdout|int > 0
changed_when: false
failed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Test basic functionality with a test deployment
- name: Show problematic pods
debug:
var: problem_pods.stdout_lines
when: non_running_pods.stdout|int > 0
delegate_to: "{{ groups['control_plane'][0] }}"
# Simplified test deployment - with more debugging
- name: Test basic functionality with a simple deployment
block:
- name: Create test namespace
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf create namespace test
register: namespace_result
failed_when:
- namespace_result.rc != 0
- "'already exists' not in namespace_result.stderr"
failed_when: false
changed_when: "'created' in namespace_result.stdout"
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Display namespace creation result
debug:
var: namespace_result
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Deploy test nginx
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf create deployment nginx --image=nginx -n test
register: deployment_result
failed_when:
- deployment_result.rc != 0
- "'already exists' not in deployment_result.stderr"
changed_when: "'created' in deployment_result.stdout"
failed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Wait for nginx pod to be ready
- name: Display deployment result
debug:
var: deployment_result
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Wait for nginx pod (non-blocking)
shell: |
kubectl --kubeconfig=/etc/kubernetes/admin.conf wait --for=condition=ready pods -l app=nginx -n test --timeout=60s
register: nginx_ready
kubectl --kubeconfig=/etc/kubernetes/admin.conf get pods -n test -l app=nginx -o wide
register: nginx_status
changed_when: false
failed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
when: inventory_hostname == groups['control_plane'][0]
- name: Check if CoreDNS is functioning
shell: |
kubectl --kubeconfig=/etc/kubernetes/admin.conf run -n test --rm -i --restart=Never --image=busybox dns-test -- nslookup kubernetes.default
register: dns_test
failed_when: "\"can't resolve\" in dns_test.stdout or dns_test.rc != 0"
changed_when: false
delegate_to: "{{ groups['control_plane'][0] }}"
- name: Display nginx pod status
debug:
var: nginx_status.stdout_lines
delegate_to: "{{ groups['control_plane'][0] }}"
when: inventory_hostname == groups['control_plane'][0]
- name: Clean up test resources
@@ -93,10 +108,9 @@
- name: Display cluster status summary
debug:
msg:
- "Kubernetes cluster is validated and fully functional"
- "Control plane components: Running"
- "Worker nodes: {{ groups['workers'] | length }} Ready"
- "CNI (Calico): Operational"
- "CoreDNS: Functional"
- "Kubernetes cluster validation summary:"
- "Control plane: Available"
- "Worker nodes: {{ groups['workers'] | length }} nodes"
- "All pods: {{ 'Ready' if non_running_pods.stdout|int == 0 else 'Some pods not ready' }}"
delegate_to: "{{ groups['control_plane'][0] }}"
when: inventory_hostname == groups['control_plane'][0]