177 lines
6.8 KiB
YAML
177 lines
6.8 KiB
YAML
---
|
|
# - name: Setup Local Laptop Tools
|
|
# hosts: localhost
|
|
# connection: local
|
|
# become: yes
|
|
# tasks:
|
|
# - name: Set ANSIBLE_CONFIG locally
|
|
# become: yes
|
|
# ansible.builtin.lineinfile:
|
|
# path: "~/.bashrc"
|
|
# line: 'export ANSIBLE_CONFIG="/mnt/f/Invixel/k8s-infra-cluster/ansible.cfg"'
|
|
#
|
|
# Run this on master node 192.168.3.91
|
|
# sudo mkdir -p /etc/rancher/k3s
|
|
# sudo nano /etc/rancher/k3s/config.yaml
|
|
# state: present
|
|
# bind-address: "192.168.3.91"
|
|
# advertise-address: "192.168.3.91"
|
|
# tls-san:
|
|
# - "192.168.3.91"
|
|
# cluster-init: true
|
|
|
|
# Reset on invixel-vm1 192.168.3.91
|
|
#sudo systemctl stop k3s
|
|
#sudo k3s server --cluster-reset
|
|
#sudo systemctl start k3s
|
|
|
|
|
|
|
|
# sudo systemctl start k3s
|
|
|
|
- name: Setup Laptop to Control K3s Cluster
|
|
hosts: master[0]
|
|
become: yes
|
|
|
|
vars:
|
|
# Replace this with your Master's actual LAN or Tailscale IP
|
|
master_public_ip: "192.168.3.156"
|
|
local_kube_path: "~/.kube/config"
|
|
|
|
tasks:
|
|
# - name: Ensure K3s is running with etcd HA mode (--cluster-init)
|
|
# # Ensure K3s is running with TLS SAN for the Public IP
|
|
# # curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--tls-san {{ master_public_ip }}" sh -
|
|
# ansible.builtin.shell: |
|
|
# # Auf dem ERSTEN, existierenden Master ausführen:
|
|
# curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--tls-san {{ master_public_ip }} --cluster-init" sh -
|
|
# register: k3s_install
|
|
|
|
|
|
# Run this only one time
|
|
- name: Ensure K3s is running with etcd HA mode (--cluster-init)
|
|
ansible.builtin.shell: |
|
|
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--bind-address={{ master_public_ip }} --advertise-address={{ master_public_ip }} --tls-san {{ master_public_ip }} --cluster-init" sh -
|
|
# Verhindert mehrfaches Ausführen, wenn K3s bereits existiert:
|
|
args:
|
|
creates: /usr/local/bin/k3s
|
|
- name: Wait for k3s.yaml to be fully generated
|
|
ansible.builtin.wait_for:
|
|
path: /etc/rancher/k3s/k3s.yaml
|
|
search_regex: "certificate-authority-data" # Ensures the file isn't empty
|
|
timeout: 30
|
|
|
|
# - name: Read k3s.yaml as plain text
|
|
# ansible.builtin.command: cat /etc/rancher/k3s/k3s.yaml
|
|
# register: k3s_config_raw
|
|
# changed_when: false
|
|
|
|
# - name: Prepare and save config to local laptop
|
|
# delegate_to: localhost
|
|
# become: no
|
|
# ansible.builtin.copy:
|
|
# # We use .stdout here because that's where the 'cat' text is stored
|
|
# content: "{{ k3s_config_raw.stdout | replace('127.0.0.1', master_public_ip) }}"
|
|
# dest: "{{ local_kube_path }}"
|
|
# mode: '0600'
|
|
|
|
# For Phase 2
|
|
- name: Fetch the cluster join token from the first master
|
|
ansible.builtin.slurp:
|
|
src: /var/lib/rancher/k3s/server/node-token
|
|
register: master_token_encoded
|
|
|
|
- name: Share the token with the rest of the playbook run
|
|
ansible.builtin.set_fact:
|
|
cluster_token: "{{ master_token_encoded.content | b64decode | trim }}"
|
|
delegate_to: localhost
|
|
delegate_facts: true
|
|
# For Phase 2
|
|
|
|
|
|
- name: Read k3s.yaml from master
|
|
ansible.builtin.slurp:
|
|
src: /etc/rancher/k3s/k3s.yaml
|
|
register: k3s_config_encoded
|
|
|
|
- name: Prepare and save config to local laptop
|
|
delegate_to: localhost
|
|
become: no
|
|
ansible.builtin.copy:
|
|
content: "{{ k3s_config_encoded.content | b64decode | replace('127.0.0.1', master_public_ip) }}"
|
|
dest: "{{ local_kube_path }}"
|
|
mode: '0600'
|
|
|
|
# ==============================================================================
|
|
# PHASE 2: WEITERE CONTROL PLANES HINZUFÜGEN (JOIN)
|
|
# ==============================================================================
|
|
- name: Join Additional Control Planes to the Cluster
|
|
hosts: master[1:] # Führt dies auf ALLEN ANDEREN Servern in der Master-Gruppe aus (Server 2, 3, etc.)
|
|
become: yes
|
|
vars:
|
|
first_master_ip: "192.168.3.156" # Die IP des ersten Masters, an den sich alle anmelde
|
|
tasks:
|
|
- name: Join as additional HA Control Plane
|
|
ansible.builtin.shell: |
|
|
# Auf dem NEUEN, zweiten Server ausführen:
|
|
curl -sfL https://get.k3s.io | K3S_TOKEN="{{ hostvars['localhost']['cluster_token'] }}" INSTALL_K3S_EXEC="--server https://{{ first_master_ip }}:6443 --tls-san {{ ansible_host }}" sh -
|
|
# Hinweis: {{ ansible_host }} nimmt automatisch die IP des jeweiligen Servers aus deiner hosts.ini
|
|
|
|
|
|
# ==============================================================================
|
|
# PHASE 3: ALLE WORKER NODES (DB- & NORMALE WORKER) SAUBER HINZUFÜGEN
|
|
# ==============================================================================
|
|
- name: Join All Worker Nodes to the Cluster
|
|
hosts: k3s_agents # Benutze die Sammelgruppe aus deiner hosts.ini
|
|
become: yes
|
|
vars:
|
|
first_master_ip: "192.168.3.156"
|
|
tasks:
|
|
- name: Check if an old K3s installation exists
|
|
ansible.builtin.stat:
|
|
path: /usr/local/bin/k3s-agent-uninstall.sh
|
|
register: old_k3s_agent
|
|
|
|
- name: Clean old cluster configuration if present
|
|
ansible.builtin.shell: /usr/local/bin/k3s-agent-uninstall.sh
|
|
when: old_k3s_agent.stat.exists
|
|
|
|
- name: Join as K3s Agent (Fresh or Re-Join)
|
|
ansible.builtin.shell: |
|
|
curl -sfL https://get.k3s.io | K3S_URL="https://{{ first_master_ip }}:6443" K3S_TOKEN="{{ hostvars['localhost']['cluster_token'] }}" sh -
|
|
|
|
# ==============================================================================
|
|
# PHASE 4: KUBERNETES ROLES AUTOMATISCH ZUWEISEN
|
|
# ==============================================================================
|
|
- name: Assign Kubernetes Roles to Nodes
|
|
hosts: master[0]
|
|
become: yes
|
|
tasks:
|
|
- name: Label DB Workers
|
|
ansible.builtin.command:
|
|
# cmd: "kubectl label node {{ item }} node-role.kubernetes.io/dbworker= --overwrite"
|
|
# sudo kubectl label node invixel-ubuntu2 node-role.kubernetes.io/nworker=
|
|
# sudo kubectl label node invixel-ubuntu3 node-role.kubernetes.io/nworker=
|
|
# sudo kubectl label node invixel-ubuntu-5 node-role.kubernetes.io/nworker=
|
|
cmd: >
|
|
kubectl label node
|
|
{{ hostvars[item]['ansible_hostname'] | default(hostvars[item]['inventory_hostname']) }}
|
|
node-role.kubernetes.io/dbworker= --overwrite
|
|
loop: "{{ groups['pure_dbworkers'] }}"
|
|
ignore_errors: yes
|
|
|
|
- name: Label Normal Workers
|
|
ansible.builtin.command:
|
|
# cmd: "kubectl label node {{ item }} node-role.kubernetes.io/nworker= --overwrite"
|
|
cmd: >
|
|
kubectl label node
|
|
{{ hostvars[item]['ansible_hostname'] | default(hostvars[item]['inventory_hostname']) }}
|
|
node-role.kubernetes.io/nworker= --overwrite
|
|
# Verwendet die Namen aus deiner [workers] Gruppe in der hosts.ini
|
|
loop: "{{ groups['workers'] }}"
|
|
ignore_errors: yes
|
|
|
|
|
|
# export ANSIBLE_CONFIG=./ansible.cfg
|
|
|
|
# ansible-playbook -i hosts.ini k3s-remote-control.yml |