add kubernetes
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
multipass/cloud-init/ssh_key.txt
|
multipass/cloud-init/ssh_key.txt
|
||||||
|
multipass/cloud-init/common_processed.yaml
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
[postgres_primary]
|
||||||
|
pg-primary ansible_host=10.203.146.218
|
||||||
|
|
||||||
|
[postgres_replicas]
|
||||||
|
pg-replica1 ansible_host=10.203.146.159
|
||||||
|
pg-replica2 ansible_host=10.203.146.95
|
||||||
|
|
||||||
|
[postgres_lb]
|
||||||
|
pg-lb ansible_host=10.203.146.72
|
||||||
|
|
||||||
|
[postgres:children]
|
||||||
|
postgres_primary
|
||||||
|
postgres_replicas
|
||||||
|
postgres_lb
|
||||||
|
|
||||||
|
[all:vars]
|
||||||
|
ansible_user=ubuntu
|
||||||
|
ansible_ssh_private_key_file=~/.ssh/id_rsa
|
||||||
|
ansible_python_interpreter=/usr/bin/python3
|
||||||
|
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
---
|
||||||
|
# ansible/playbooks/kubernetes.yml
|
||||||
|
- name: Common configuration for all nodes
|
||||||
|
hosts: k8s_cluster
|
||||||
|
become: yes
|
||||||
|
tags:
|
||||||
|
- common
|
||||||
|
tasks:
|
||||||
|
- include_tasks: ../tasks/kubernetes/setup-hosts.yml
|
||||||
|
- include_tasks: ../tasks/kubernetes/update-dns.yml
|
||||||
|
- include_tasks: ../tasks/kubernetes/ssh.yml
|
||||||
|
- include_tasks: ../tasks/kubernetes/setup-kernel.yml
|
||||||
|
|
||||||
|
- name: Kubernetes setup for all nodes
|
||||||
|
hosts: k8s_cluster
|
||||||
|
become: yes
|
||||||
|
tags:
|
||||||
|
- k8s-setup
|
||||||
|
tasks:
|
||||||
|
- include_tasks: ../tasks/kubernetes/node-setup.yml
|
||||||
|
|
||||||
|
- name: Initialize Kubernetes control plane
|
||||||
|
hosts: control_plane
|
||||||
|
become: yes
|
||||||
|
tags:
|
||||||
|
- control-plane
|
||||||
|
tasks:
|
||||||
|
- include_tasks: ../tasks/kubernetes/control-plane-init.yml
|
||||||
|
|
||||||
|
- name: Join worker nodes to cluster
|
||||||
|
hosts: workers
|
||||||
|
become: yes
|
||||||
|
tags:
|
||||||
|
- workers
|
||||||
|
tasks:
|
||||||
|
- include_tasks: ../tasks/kubernetes/worker-join.yml
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
---
|
||||||
|
- name: Configure PostgreSQL Primary
|
||||||
|
hosts: postgres_primary
|
||||||
|
become: true
|
||||||
|
tasks:
|
||||||
|
- name: Install PostgreSQL
|
||||||
|
apt:
|
||||||
|
name: postgresql
|
||||||
|
state: present
|
||||||
|
update_cache: yes
|
||||||
|
|
||||||
|
- name: Ensure PostgreSQL is running
|
||||||
|
service:
|
||||||
|
name: postgresql
|
||||||
|
state: started
|
||||||
|
enabled: yes
|
||||||
|
|
||||||
|
- name: Create test database
|
||||||
|
become: true
|
||||||
|
# Replace the direct become_user with a shell command that uses sudo
|
||||||
|
shell: |
|
||||||
|
sudo -u postgres psql -c "CREATE DATABASE testdb WITH OWNER = postgres ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8' TEMPLATE = template0;"
|
||||||
|
# Only run this if the database doesn't already exist
|
||||||
|
register: db_creation
|
||||||
|
changed_when: db_creation.rc == 0
|
||||||
|
failed_when: db_creation.rc != 0 and "already exists" not in db_creation.stderr
|
||||||
|
# The following approach handles error gracefully if DB already exists
|
||||||
|
args:
|
||||||
|
executable: /bin/bash
|
||||||
|
|
||||||
|
# Rest of the playbook remains the same
|
||||||
|
- name: Configure PostgreSQL Replicas
|
||||||
|
hosts: postgres_replicas
|
||||||
|
become: true
|
||||||
|
tasks:
|
||||||
|
- name: Install PostgreSQL
|
||||||
|
apt:
|
||||||
|
name: postgresql
|
||||||
|
state: present
|
||||||
|
update_cache: yes
|
||||||
|
|
||||||
|
- name: Ensure PostgreSQL is running
|
||||||
|
service:
|
||||||
|
name: postgresql
|
||||||
|
state: started
|
||||||
|
enabled: yes
|
||||||
|
|
||||||
|
- name: Configure Load Balancer
|
||||||
|
hosts: postgres_lb
|
||||||
|
become: true
|
||||||
|
tasks:
|
||||||
|
- name: Install HAProxy
|
||||||
|
apt:
|
||||||
|
name: haproxy
|
||||||
|
state: present
|
||||||
|
update_cache: yes
|
||||||
|
|
||||||
|
- name: Create simple HAProxy configuration
|
||||||
|
copy:
|
||||||
|
dest: /etc/haproxy/haproxy.cfg
|
||||||
|
content: |
|
||||||
|
global
|
||||||
|
log /dev/log local0
|
||||||
|
log /dev/log local1 notice
|
||||||
|
chroot /var/lib/haproxy
|
||||||
|
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
|
||||||
|
stats timeout 30s
|
||||||
|
user haproxy
|
||||||
|
group haproxy
|
||||||
|
daemon
|
||||||
|
|
||||||
|
defaults
|
||||||
|
log global
|
||||||
|
mode tcp
|
||||||
|
option tcplog
|
||||||
|
option dontlognull
|
||||||
|
timeout connect 5000
|
||||||
|
timeout client 50000
|
||||||
|
timeout server 50000
|
||||||
|
|
||||||
|
frontend postgresql
|
||||||
|
bind *:5432
|
||||||
|
mode tcp
|
||||||
|
default_backend postgresql_backends
|
||||||
|
|
||||||
|
backend postgresql_backends
|
||||||
|
mode tcp
|
||||||
|
balance roundrobin
|
||||||
|
server primary {{ hostvars[groups['postgres_primary'][0]]['ansible_host'] }}:5432 check
|
||||||
|
backup: yes
|
||||||
|
|
||||||
|
- name: Restart HAProxy
|
||||||
|
service:
|
||||||
|
name: haproxy
|
||||||
|
state: restarted
|
||||||
|
enabled: yes
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
---
|
||||||
|
# tasks/control-plane-init.yml
|
||||||
|
- name: Set Kubernetes network CIDRs for Calico
|
||||||
|
set_fact:
|
||||||
|
pod_cidr: "192.168.0.0/16" # Calico default CIDR
|
||||||
|
service_cidr: "10.96.0.0/16"
|
||||||
|
|
||||||
|
- name: Initialize Kubernetes control plane
|
||||||
|
command: >
|
||||||
|
kubeadm init
|
||||||
|
--pod-network-cidr={{ pod_cidr }}
|
||||||
|
--service-cidr={{ service_cidr }}
|
||||||
|
--apiserver-advertise-address={{ ansible_default_ipv4.address }}
|
||||||
|
register: kubeadm_init
|
||||||
|
args:
|
||||||
|
creates: /etc/kubernetes/admin.conf
|
||||||
|
|
||||||
|
- name: Create .kube directory for current user
|
||||||
|
file:
|
||||||
|
path: "/home/{{ ansible_user }}/.kube"
|
||||||
|
state: directory
|
||||||
|
owner: "{{ ansible_user }}"
|
||||||
|
group: "{{ ansible_user }}"
|
||||||
|
mode: '0755'
|
||||||
|
|
||||||
|
- name: Copy Kubernetes admin.conf to user's .kube/config
|
||||||
|
copy:
|
||||||
|
src: /etc/kubernetes/admin.conf
|
||||||
|
dest: "/home/{{ ansible_user }}/.kube/config"
|
||||||
|
remote_src: yes
|
||||||
|
owner: "{{ ansible_user }}"
|
||||||
|
group: "{{ ansible_user }}"
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Install Calico Operator
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.1/manifests/tigera-operator.yaml
|
||||||
|
register: calico_operator_result
|
||||||
|
changed_when: "'created' in calico_operator_result.stdout"
|
||||||
|
failed_when:
|
||||||
|
- calico_operator_result.rc != 0
|
||||||
|
- "'already exists' not in calico_operator_result.stderr"
|
||||||
|
|
||||||
|
- name: Install Calico Custom Resources
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.1/manifests/custom-resources.yaml
|
||||||
|
register: calico_resources_result
|
||||||
|
changed_when: "'created' in calico_resources_result.stdout"
|
||||||
|
failed_when:
|
||||||
|
- calico_resources_result.rc != 0
|
||||||
|
- "'already exists' not in calico_resources_result.stderr"
|
||||||
|
|
||||||
|
- name: Wait for calico-system namespace to be created
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf get namespace | grep calico-system
|
||||||
|
register: calico_ns
|
||||||
|
until: calico_ns.rc == 0
|
||||||
|
retries: 30
|
||||||
|
delay: 10
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Wait for Calico pods to be ready
|
||||||
|
shell: |
|
||||||
|
# Give pods some time to be created
|
||||||
|
sleep 30
|
||||||
|
kubectl --kubeconfig=/etc/kubernetes/admin.conf -n calico-system wait --for=condition=ready pods --all --timeout=300s
|
||||||
|
register: calico_pods_ready
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false # Don't fail if timeout occurs, we'll verify the node status later
|
||||||
|
|
||||||
|
- name: Wait for CoreDNS to be ready
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf -n kube-system wait --for=condition=ready pods -l k8s-app=kube-dns --timeout=120s
|
||||||
|
register: coredns_ready
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Wait for node to be ready
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf wait --for=condition=ready nodes --all --timeout=120s
|
||||||
|
register: node_ready
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Remove taint from control plane node
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf taint nodes --all node-role.kubernetes.io/control-plane-
|
||||||
|
register: taint_result
|
||||||
|
changed_when: "'untainted' in taint_result.stdout"
|
||||||
|
failed_when: false # This might fail if the taint doesn't exist, which is fine
|
||||||
|
|
||||||
|
- name: Remove load balancer label from all nodes
|
||||||
|
shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf label nodes --all node.kubernetes.io/exclude-from-external-load-balancers-
|
||||||
|
register: label_result
|
||||||
|
changed_when: "'labeled' in label_result.stdout"
|
||||||
|
failed_when: false # This might fail if the label doesn't exist, which is fine
|
||||||
|
|
||||||
|
- name: Get join command
|
||||||
|
command: kubeadm token create --print-join-command
|
||||||
|
register: join_command
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Store join command
|
||||||
|
set_fact:
|
||||||
|
join_command: "{{ join_command.stdout }}"
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
---
|
||||||
|
# tasks/node-setup.yml
|
||||||
|
- name: Update apt cache
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
|
||||||
|
- name: Install apt transport packages
|
||||||
|
apt:
|
||||||
|
name:
|
||||||
|
- apt-transport-https
|
||||||
|
- ca-certificates
|
||||||
|
- curl
|
||||||
|
- 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
|
||||||
|
|
||||||
|
- 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
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Create keyrings directory
|
||||||
|
file:
|
||||||
|
path: /etc/apt/keyrings
|
||||||
|
state: directory
|
||||||
|
mode: '0755'
|
||||||
|
|
||||||
|
- name: Download Kubernetes apt key
|
||||||
|
get_url:
|
||||||
|
url: "https://pkgs.k8s.io/core:/stable:/{{ kube_latest.stdout }}/deb/Release.key"
|
||||||
|
dest: /tmp/kubernetes-release.key
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Add Kubernetes apt key
|
||||||
|
shell: cat /tmp/kubernetes-release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
|
||||||
|
args:
|
||||||
|
creates: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
|
||||||
|
|
||||||
|
- name: Add Kubernetes apt repository
|
||||||
|
copy:
|
||||||
|
content: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/{{ kube_latest.stdout }}/deb/ /"
|
||||||
|
dest: /etc/apt/sources.list.d/kubernetes.list
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Update apt cache
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
|
||||||
|
- name: Install Kubernetes components
|
||||||
|
apt:
|
||||||
|
name:
|
||||||
|
- kubelet
|
||||||
|
- kubeadm
|
||||||
|
- kubectl
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Hold Kubernetes packages
|
||||||
|
dpkg_selections:
|
||||||
|
name: "{{ item }}"
|
||||||
|
selection: hold
|
||||||
|
loop:
|
||||||
|
- kubelet
|
||||||
|
- kubeadm
|
||||||
|
- kubectl
|
||||||
|
|
||||||
|
- name: Check if crictl is installed
|
||||||
|
command: which crictl
|
||||||
|
register: crictl_check
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Configure crictl
|
||||||
|
shell: |
|
||||||
|
crictl config \
|
||||||
|
--set runtime-endpoint=unix:///run/containerd/containerd.sock \
|
||||||
|
--set image-endpoint=unix:///run/containerd/containerd.sock
|
||||||
|
changed_when: false
|
||||||
|
when: crictl_check.rc == 0
|
||||||
|
|
||||||
|
- name: Get primary network interface IP
|
||||||
|
shell: ip -4 addr show {{ ansible_default_ipv4.interface }} | grep "inet" | head -1 | awk '{print $2}' | cut -d/ -f1
|
||||||
|
register: node_ip
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Configure kubelet node IP
|
||||||
|
copy:
|
||||||
|
content: "KUBELET_EXTRA_ARGS='--node-ip {{ node_ip.stdout }}'"
|
||||||
|
dest: /etc/default/kubelet
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: '0644'
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
---
|
||||||
|
- name: Remove unwanted entries from /etc/hosts
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/hosts
|
||||||
|
regexp: "{{ item }}"
|
||||||
|
state: absent
|
||||||
|
loop:
|
||||||
|
- "^.*ubuntu-jammy.*$"
|
||||||
|
- "^.*{{ ansible_hostname }}.*$"
|
||||||
|
|
||||||
|
- name: Get primary IP address
|
||||||
|
shell: "ip route | grep default | grep -Pv '10\\.\\d+\\.\\d+\\.\\d+' | awk '{ print $9 }'"
|
||||||
|
register: primary_ip
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Set PRIMARY_IP environment variable
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/environment
|
||||||
|
line: "PRIMARY_IP={{ primary_ip.stdout }}"
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Add controlplane entry to /etc/hosts
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/hosts
|
||||||
|
line: "{{ hostvars['controlplane']['ansible_host'] }} controlplane"
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Add worker nodes to /etc/hosts
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/hosts
|
||||||
|
line: "{{ hostvars[item]['ansible_host'] }} {{ item }}"
|
||||||
|
state: present
|
||||||
|
loop: "{{ groups['workers'] }}"
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
---
|
||||||
|
# tasks/setup-kernel.yml
|
||||||
|
- name: Create Kubernetes kernel modules config file
|
||||||
|
copy:
|
||||||
|
content: |
|
||||||
|
overlay
|
||||||
|
br_netfilter
|
||||||
|
dest: /etc/modules-load.d/k8s.conf
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Attempt to load overlay module
|
||||||
|
shell: modprobe overlay || true
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Attempt to load br_netfilter module
|
||||||
|
shell: modprobe br_netfilter || true
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Create Kubernetes sysctl config file
|
||||||
|
copy:
|
||||||
|
content: |
|
||||||
|
net.bridge.bridge-nf-call-iptables = 1
|
||||||
|
net.bridge.bridge-nf-call-ip6tables = 1
|
||||||
|
net.ipv4.ip_forward = 1
|
||||||
|
dest: /etc/sysctl.d/k8s.conf
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: '0644'
|
||||||
|
register: sysctl_conf
|
||||||
|
|
||||||
|
- name: Apply sysctl changes
|
||||||
|
command: sysctl --system
|
||||||
|
when: sysctl_conf.changed
|
||||||
|
|
||||||
|
- name: Check loaded modules
|
||||||
|
shell: "lsmod | grep -E 'overlay|br_netfilter' || echo 'Modules may be built into kernel'"
|
||||||
|
register: modules_check
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Display module status
|
||||||
|
debug:
|
||||||
|
var: modules_check.stdout_lines
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
# tasks/ssh.yml
|
||||||
|
- name: Enable password authentication in sshd_config
|
||||||
|
replace:
|
||||||
|
path: /etc/ssh/sshd_config
|
||||||
|
regexp: '#PasswordAuthentication'
|
||||||
|
replace: 'PasswordAuthentication'
|
||||||
|
|
||||||
|
- name: Enable KbdInteractiveAuthentication in sshd_config
|
||||||
|
replace:
|
||||||
|
path: /etc/ssh/sshd_config
|
||||||
|
regexp: 'KbdInteractiveAuthentication no'
|
||||||
|
replace: 'KbdInteractiveAuthentication yes'
|
||||||
|
|
||||||
|
- name: Restart SSH service
|
||||||
|
systemd:
|
||||||
|
name: ssh
|
||||||
|
state: restarted
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
- name: Ensure systemd-resolved is running
|
||||||
|
systemd:
|
||||||
|
name: systemd-resolved
|
||||||
|
state: started
|
||||||
|
enabled: yes
|
||||||
|
|
||||||
|
- name: Configure systemd-resolved to use Google DNS
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/systemd/resolved.conf
|
||||||
|
regexp: '^#?DNS='
|
||||||
|
line: 'DNS=8.8.8.8'
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Restart systemd-resolved service
|
||||||
|
systemd:
|
||||||
|
name: systemd-resolved
|
||||||
|
state: restarted
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
# tasks/worker-join.yml
|
||||||
|
- name: Get join command from control plane
|
||||||
|
delegate_to: controlplane
|
||||||
|
command: kubeadm token create --print-join-command
|
||||||
|
register: join_command_result
|
||||||
|
changed_when: false
|
||||||
|
run_once: true
|
||||||
|
|
||||||
|
- name: Join worker node to the cluster
|
||||||
|
command: "{{ join_command_result.stdout }}"
|
||||||
|
args:
|
||||||
|
creates: /etc/kubernetes/kubelet.conf
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#cloud-config
|
||||||
|
package_update: true
|
||||||
|
package_upgrade: true
|
||||||
|
|
||||||
|
packages:
|
||||||
|
- python3
|
||||||
|
- python3-pip
|
||||||
|
- openssh-server
|
||||||
|
|
||||||
|
ssh_authorized_keys:
|
||||||
|
- $SSH_PUBLIC_KEY
|
||||||
|
|
||||||
|
runcmd:
|
||||||
|
- systemctl restart sshd
|
||||||
@@ -1 +0,0 @@
|
|||||||
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDNxAOn78XFG3JELc+adb5CLbR0654QIHzI1ARmEcFMbVRSIqWqdIj4JckOHgxKxOrqV3PLxBbPvjWatJ3RMLixUTimokFLu42ecE/vWGHh96+XGVJ01em0WduS07VssqHc/9sG0gVMHINOylPUBCcudqqhsPKMtFk2MlAKxegxbT4+WuyYslY/cSC5N6DpzhafOB79ogGTuJ/hKsOTCtRALe/M+8Pvel+8ONU/0LimScTJPYggNxe6vaGvL9eY+69t9jXzn0nXKrwfArcurNtFyeOVA8IznyF7ahvPN6NVg0Ac6aAZLkua3Yv33Xi2dBUScBiig9pmPF7XEysu5brcTu+jTPXupcneD8HRWqKdY3J4e1n7HAUGaFW+E6zgXKNUPY68kUVik74FH2RHPKs8BxeXf1J7Y9sTt6OigEM2cwkPR620GCRV18flmUqftzzGlpnF94gBnyxVhWB6Ti3KQJyG8a0CWPOTAJ3jx2tj9SW3qjqgiDTh8wDCZttlZHgFMMHNHCN30UCJethaZiem9CLT/97QzF6x3rTe8Ru61xJOwVX6IMEjPQwRSUOQJ8FKhOP1QhRAjfL2HvkJck/kei0HktLpV81+unpShEynPFOVh7AC+kqugNOOR/zU4BxEhuUQHHZvdHWrOEIXJeNPHfdK9GSNaGHSeyNWSdiQYQ== your_email@sev.fo
|
|
||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# cleanup-postgres.sh - Removes PostgreSQL testing environment
|
||||||
|
|
||||||
|
echo "Deleting PostgreSQL VMs..."
|
||||||
|
multipass delete pg-primary pg-replica1 pg-replica2 pg-lb
|
||||||
|
multipass purge
|
||||||
|
|
||||||
|
echo "Cleanup complete."
|
||||||
Regular → Executable
+106
@@ -0,0 +1,106 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# provision-kubernetes.sh - Creates and configures a Kubernetes environment
|
||||||
|
|
||||||
|
# Set variables
|
||||||
|
CONTROL_PLANE="controlplane"
|
||||||
|
NODE01="node01"
|
||||||
|
NODE02="node02"
|
||||||
|
NODE03="node03"
|
||||||
|
SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
|
||||||
|
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
|
||||||
|
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
|
||||||
|
INVENTORY_FILE="$(pwd)/ansible/inventories/kubernetes.yml"
|
||||||
|
|
||||||
|
# Check if any VMs already exist
|
||||||
|
VM_EXISTS=false
|
||||||
|
if multipass info $CONTROL_PLANE &>/dev/null || \
|
||||||
|
multipass info $NODE01 &>/dev/null || \
|
||||||
|
multipass info $NODE02 &>/dev/null || \
|
||||||
|
multipass info $NODE03 &>/dev/null; then
|
||||||
|
VM_EXISTS=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$VM_EXISTS" = true ]; then
|
||||||
|
echo "Some Kubernetes VMs already exist, proceeding to Ansible configuration..."
|
||||||
|
else
|
||||||
|
# Verify SSH key file exists
|
||||||
|
if [ ! -f "$SSH_KEY_FILE" ]; then
|
||||||
|
echo "Error: SSH key file not found at $SSH_KEY_FILE"
|
||||||
|
echo "Please create this file with your public SSH key."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Read SSH key
|
||||||
|
SSH_KEY=$(cat "$SSH_KEY_FILE")
|
||||||
|
|
||||||
|
# Process the cloud-init template and replace the placeholder
|
||||||
|
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$CLOUD_INIT_TEMPLATE" > "$CLOUD_INIT_PROCESSED"
|
||||||
|
|
||||||
|
echo "Creating Kubernetes VMs with Multipass..."
|
||||||
|
# Control plane gets more resources
|
||||||
|
multipass launch --name $CONTROL_PLANE --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
multipass launch --name $NODE01 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
multipass launch --name $NODE02 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
multipass launch --name $NODE03 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
|
||||||
|
# Remove processed file after use
|
||||||
|
rm "$CLOUD_INIT_PROCESSED"
|
||||||
|
|
||||||
|
# Give VMs a moment to initialize
|
||||||
|
echo "Waiting for VMs to initialize..."
|
||||||
|
sleep 10
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Extracting IP addresses..."
|
||||||
|
# Extract IPs correctly
|
||||||
|
CP_IP=$(multipass info $CONTROL_PLANE | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
NODE01_IP=$(multipass info $NODE01 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
NODE02_IP=$(multipass info $NODE02 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
NODE03_IP=$(multipass info $NODE03 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
|
||||||
|
# Print the IPs to verify
|
||||||
|
echo "Control Plane IP: $CP_IP"
|
||||||
|
echo "Node01 IP: $NODE01_IP"
|
||||||
|
echo "Node02 IP: $NODE02_IP"
|
||||||
|
echo "Node03 IP: $NODE03_IP"
|
||||||
|
|
||||||
|
echo "Generating Ansible inventory..."
|
||||||
|
# Create inventory file
|
||||||
|
cat > "$INVENTORY_FILE" << EOF
|
||||||
|
---
|
||||||
|
all:
|
||||||
|
children:
|
||||||
|
k8s_cluster:
|
||||||
|
children:
|
||||||
|
control_plane:
|
||||||
|
hosts:
|
||||||
|
controlplane:
|
||||||
|
ansible_host: $CP_IP
|
||||||
|
workers:
|
||||||
|
hosts:
|
||||||
|
node01:
|
||||||
|
ansible_host: $NODE01_IP
|
||||||
|
node02:
|
||||||
|
ansible_host: $NODE02_IP
|
||||||
|
node03:
|
||||||
|
ansible_host: $NODE03_IP
|
||||||
|
vars:
|
||||||
|
ansible_user: ubuntu
|
||||||
|
ansible_become: yes
|
||||||
|
ansible_ssh_private_key_file: ~/.ssh/id_rsa
|
||||||
|
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "Verifying inventory file contents:"
|
||||||
|
cat "$INVENTORY_FILE"
|
||||||
|
|
||||||
|
echo "Running Ansible playbook for Kubernetes deployment..."
|
||||||
|
ansible-playbook -i "$INVENTORY_FILE" kubernetes/main.yml
|
||||||
|
|
||||||
|
echo "Kubernetes environment setup complete."
|
||||||
|
echo "Control Plane: $CP_IP"
|
||||||
|
echo "Workers: $NODE01_IP, $NODE02_IP, $NODE03_IP"
|
||||||
|
echo ""
|
||||||
|
echo "To access the cluster, run:"
|
||||||
|
echo "multipass shell $CONTROL_PLANE"
|
||||||
|
echo "And then: kubectl get nodes"
|
||||||
|
|||||||
Regular → Executable
+100
@@ -0,0 +1,100 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# provision-postgres.sh - Creates and configures a PostgreSQL environment
|
||||||
|
|
||||||
|
# Set variables
|
||||||
|
PRIMARY_NAME="pg-primary"
|
||||||
|
REPLICA1_NAME="pg-replica1"
|
||||||
|
REPLICA2_NAME="pg-replica2"
|
||||||
|
LB_NAME="pg-lb"
|
||||||
|
SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
|
||||||
|
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
|
||||||
|
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
|
||||||
|
INVENTORY_FILE="$(pwd)/ansible/inventories/postgres.yml"
|
||||||
|
|
||||||
|
# Check if any VMs already exist
|
||||||
|
VM_EXISTS=false
|
||||||
|
if multipass info $PRIMARY_NAME &>/dev/null || \
|
||||||
|
multipass info $REPLICA1_NAME &>/dev/null || \
|
||||||
|
multipass info $REPLICA2_NAME &>/dev/null || \
|
||||||
|
multipass info $LB_NAME &>/dev/null; then
|
||||||
|
VM_EXISTS=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$VM_EXISTS" = true ]; then
|
||||||
|
echo "Some PostgreSQL VMs already exist, proceeding to Ansible configuration..."
|
||||||
|
else
|
||||||
|
# Verify SSH key file exists
|
||||||
|
if [ ! -f "$SSH_KEY_FILE" ]; then
|
||||||
|
echo "Error: SSH key file not found at $SSH_KEY_FILE"
|
||||||
|
echo "Please create this file with your public SSH key."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Read SSH key
|
||||||
|
SSH_KEY=$(cat "$SSH_KEY_FILE")
|
||||||
|
|
||||||
|
# Process the cloud-init template and replace the placeholder
|
||||||
|
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$CLOUD_INIT_TEMPLATE" > "$CLOUD_INIT_PROCESSED"
|
||||||
|
|
||||||
|
echo "Creating PostgreSQL VMs with Multipass..."
|
||||||
|
multipass launch --name $PRIMARY_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
multipass launch --name $REPLICA1_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
multipass launch --name $REPLICA2_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
multipass launch --name $LB_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||||
|
|
||||||
|
# Remove processed file after use
|
||||||
|
rm "$CLOUD_INIT_PROCESSED"
|
||||||
|
|
||||||
|
# Give VMs a moment to initialize
|
||||||
|
echo "Waiting for VMs to initialize..."
|
||||||
|
sleep 10
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Extracting IP addresses..."
|
||||||
|
# Extract IPs correctly
|
||||||
|
PRIMARY_IP=$(multipass info pg-primary | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
REPLICA1_IP=$(multipass info pg-replica1 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
REPLICA2_IP=$(multipass info pg-replica2 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
LB_IP=$(multipass info pg-lb | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||||
|
|
||||||
|
# Print the IPs to verify
|
||||||
|
echo "Primary IP: $PRIMARY_IP"
|
||||||
|
echo "Replica1 IP: $REPLICA1_IP"
|
||||||
|
echo "Replica2 IP: $REPLICA2_IP"
|
||||||
|
echo "Load Balancer IP: $LB_IP"
|
||||||
|
|
||||||
|
echo "Generating Ansible inventory..."
|
||||||
|
# Create inventory file - using a simpler format to avoid issues
|
||||||
|
cat > "$INVENTORY_FILE" << EOF
|
||||||
|
[postgres_primary]
|
||||||
|
pg-primary ansible_host=$PRIMARY_IP
|
||||||
|
|
||||||
|
[postgres_replicas]
|
||||||
|
pg-replica1 ansible_host=$REPLICA1_IP
|
||||||
|
pg-replica2 ansible_host=$REPLICA2_IP
|
||||||
|
|
||||||
|
[postgres_lb]
|
||||||
|
pg-lb ansible_host=$LB_IP
|
||||||
|
|
||||||
|
[postgres:children]
|
||||||
|
postgres_primary
|
||||||
|
postgres_replicas
|
||||||
|
postgres_lb
|
||||||
|
|
||||||
|
[all:vars]
|
||||||
|
ansible_user=ubuntu
|
||||||
|
ansible_ssh_private_key_file=~/.ssh/id_rsa
|
||||||
|
ansible_python_interpreter=/usr/bin/python3
|
||||||
|
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "Verifying inventory file contents:"
|
||||||
|
cat "$INVENTORY_FILE"
|
||||||
|
|
||||||
|
echo "Running Ansible playbook..."
|
||||||
|
ansible-playbook -i "$INVENTORY_FILE" ansible/playbooks/postgres.yml
|
||||||
|
|
||||||
|
echo "PostgreSQL environment is ready!"
|
||||||
|
echo "Primary: $PRIMARY_IP"
|
||||||
|
echo "Replicas: $REPLICA1_IP, $REPLICA2_IP"
|
||||||
|
echo "Load Balancer: $LB_IP"
|
||||||
|
|||||||
Reference in New Issue
Block a user