Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
992 changes: 793 additions & 199 deletions app/directory_generators/ansible_generator.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion app/directory_generators/compose_generator.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Hello! It looks like you entered just the letter "M." How can I assist you today?
11 changes: 3 additions & 8 deletions app/media/MyAnsible/group_vars/all
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# General
install_ansible_modules: "true"
disable_transparent_huge_pages: "true"

setup_interface: "false"

# Network Calico see here for more details https://github.com/projectcalico/calico/releases
Expand Down Expand Up @@ -28,15 +29,9 @@ k8s_version: "1.31.2" # see here https://kubernetes.io/releases/patch-releases/
# CRI
cri_socket: unix:///var/run/containerd/containerd.sock

# VRRP and HAProxy
interface_name: "enp0s8"
virtual_ip: "192.168.178.100"
haproxy_frontend_password: "password"

# Ansible Connection

ansible_user: root
ansible_port: 22
ansible_python_interpreter: "/usr/bin/python3"
domain="devopsgpt.com"
apiserver_url="devopsgpt.com"
domain: "devopsgpt.com"
apiserver_url: "devopsgpt.com"
4 changes: 0 additions & 4 deletions app/media/MyAnsible/hosts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[all]
string private_ip=x.x.x.x
string private_ip=x.x.x.x
string private_ip=x.x.x.x

[k8s]
string
Expand All @@ -12,6 +11,3 @@ string

[k8s_workers]
string

[lb]
string
32 changes: 32 additions & 0 deletions app/media/MyAnsible/kubernetes_playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,35 @@
gather_facts: yes
any_errors_fatal: true
tags: [preinstall]

- hosts: k8s
roles:
- role: k8s
gather_facts: yes
any_errors_fatal: true
tags: [k8s]

- hosts: k8s
roles:
- role: init_k8s
gather_facts: yes
any_errors_fatal: true
tags: [init_k8s]

- hosts: k8s_masters
roles:
- role: preinstall
- role: k8s
- role: join_master
gather_facts: yes
any_errors_fatal: true
tags: [join_master]

- hosts: k8s_workers
roles:
- role: preinstall
- role: k8s
- role: join_worker
gather_facts: yes
any_errors_fatal: true
tags: [join_worker]
Empty file.
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/cni.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- block:
- name: Check if Calico CRDs exist
command: kubectl get crd felixconfigurations.crd.projectcalico.org
register: calico_crd_check
ignore_errors: true
delegate_to: "{{ groups['k8s_masters'][0] }}"

- block:
- name: Apply CNI plugin (Calico)
command: kubectl create -f {{ calico_operator_url }}
retries: 3
delay: 3

- name: Apply CNI plugin (Calico)
command: kubectl create -f {{ calico_crd_url }}
retries: 3
delay: 3
delegate_to: "{{ groups['k8s_masters'][0] }}"
when: calico_crd_check.rc != 0
run_once: true
64 changes: 64 additions & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/initk8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
- name: Init cluster | Check if kubeadm has already run
stat:
path: "/var/lib/kubelet/config.yaml"
register: kubeadm_already_run
when: inventory_hostname == groups['k8s_masters'][0]
delegate_to: "{{ groups['k8s_masters'][0] }}"

- block:
- name: Init cluster | Copy kubeadmcnf.yaml
template:
src: kubeadmcnf.yml.j2
dest: /root/kubeadmcnf.yaml

- name: Init cluster | Initiate cluster on node groups['kube_master'][0]
shell: kubeadm init --config=/root/kubeadmcnf.yaml
register: kubeadm_init
# Retry is because upload config sometimes fails
until: kubeadm_init is succeeded or "field is immutable" in kubeadm_init.stderr
notify: Restart kubelet

when: inventory_hostname == groups['k8s_masters'][0] and not kubeadm_already_run.stat.exists
delegate_to: "{{ groups['k8s_masters'][0] }}"

- block:
- name: Create kubectl directory
file:
path: /root/.kube
state: directory

- name: Configure kubectl
copy:
src: /etc/kubernetes/admin.conf
dest: /root/.kube/config
remote_src: yes

- name: Fetch kubeconfig
fetch:
src: /etc/kubernetes/admin.conf
dest: kubeconfig/
flat: yes
when: inventory_hostname == groups['k8s_masters'][0]
delegate_to: "{{ groups['k8s_masters'][0] }}"

- name: Sleep for 300 seconds and reboot the Master1 server
wait_for:
timeout: 300
delegate_to: localhost

- name: Reboot the servers
command: reboot
async: 1
poll: 0
# ignore_errors: yes
delegate_to: "{{ groups['k8s_masters'][0] }}"

- name: Sleep for 300 seconds to Master1 up and running
wait_for:
timeout: 300
delegate_to: localhost
# when: use_iran == "true"

- name: Example Task After Reboot
debug:
msg: "Server back online and ready for tasks."
8 changes: 8 additions & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# tasks file for init_k8s

- name: Initialize kubernetes cluster
include_tasks: initk8s.yml

- name: Initialize Calico CNI
include_tasks: cni.yml
Empty file.
Empty file.
Empty file.
100 changes: 100 additions & 0 deletions app/media/MyAnsible/roles/join_master/tasks/join_master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
- name: Init cluster | Check if kubeadm has already run
stat:
path: "/var/lib/kubelet/config.yaml"
register: kubeadm_already_run

- block:
- name: Generate join command
command: kubeadm token create --print-join-command
register: join_command

- name: Print join command
debug:
msg: "{{ join_command.stdout_lines[0] }}"

- name: Copy join command to local file
become: false
local_action: copy content="{{ join_command.stdout_lines[0] }} $@" dest="roles/join_master/files/join-command"

- name: copy kubeadmcnf.yaml
template:
src: kubeadmcnf-join.yml.j2
dest: /root/kubeadm-config.yaml

when:
- inventory_hostname == groups['k8s_masters'][0]
delegate_to: "{{ groups['k8s_masters'][0] }}"

- block:
- name: Copy the join command to server location
copy:
src: roles/join_master/files/join-command
dest: /root/join-command.sh
mode: "0777"

when:
- inventory_hostname != groups['k8s_masters'][0]
- inventory_hostname in groups['k8s_masters']
- not kubeadm_already_run.stat.exists

- block:
- name: get certificate key
shell: kubeadm init phase upload-certs --upload-certs --config=/root/kubeadm-config.yaml
register: kubeadm_cert_key

- name: Print certificate key
debug:
msg: "{{ kubeadm_cert_key.stdout_lines[2] }}"

- name: register the cert key
set_fact:
control_plane_certkey: "{{ kubeadm_cert_key.stdout_lines[2] }}"

when:
- inventory_hostname in groups['k8s_masters'][0]
delegate_to: "{{ groups['k8s_masters'][0] }}"
run_once: false
delegate_facts: true

- name: Join | Join control-plane to cluster
command: "sh /root/join-command.sh --control-plane --certificate-key={{ hostvars[groups['k8s_masters'][0]].control_plane_certkey }} --cri-socket={{ cri_socket }}"
when:
- inventory_hostname != groups['k8s_masters'][0]
- inventory_hostname in groups['k8s_masters']
- not kubeadm_already_run.stat.exists

- block:
- name: Create kubectl directory
file:
path: /root/.kube
state: directory

- name: Configure kubectl
copy:
src: /etc/kubernetes/admin.conf
dest: /root/.kube/config
remote_src: yes

- name: Fetch kubeconfig
fetch:
src: /etc/kubernetes/admin.conf
dest: kubeconfig/
flat: yes
when:
- inventory_hostname != groups['k8s_masters'][0]
- inventory_hostname in groups['k8s_masters']
- not kubeadm_already_run.stat.exists

- name: remove apiserver_url to point to the masters temporary
lineinfile:
dest: /etc/hosts
line: "{{ hostvars[groups['k8s_masters'][0]].private_ip }} {{ apiserver_url }}"
state: absent

- name: Add apiserver_url to point to the masters
lineinfile:
dest: /etc/hosts
line: "{{ private_ip }} {{ apiserver_url }}"
state: present
when:
- inventory_hostname in groups['k8s_masters']
5 changes: 5 additions & 0 deletions app/media/MyAnsible/roles/join_master/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
# tasks file for join_master

- name: Join master(s) node to cluster
include_tasks: join_master.yml
Empty file.
Empty file.
Empty file.
38 changes: 38 additions & 0 deletions app/media/MyAnsible/roles/join_worker/tasks/join_worker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
- name: Init cluster | Check if kubeadm has already run
stat:
path: "/var/lib/kubelet/config.yaml"
register: kubeadm_already_run

- block:
- name: Generate join command
command: kubeadm token create --print-join-command
register: join_command

- name: Print join command
debug:
msg: "{{ join_command.stdout_lines[0] }}"

- name: Copy join command to local file
become: false
local_action: copy content="{{ join_command.stdout_lines[0] }} $@" dest="roles/join_worker/files/join-command"

when:
- inventory_hostname not in groups['k8s_masters'][0]
delegate_to: "{{ groups['k8s_masters'][0] }}"

- block:
- name: Copy the join command to server location
copy:
src: roles/join_worker/files/join-command
dest: /root/join-command.sh
mode: "0777"

when:
- inventory_hostname not in groups['k8s_masters']
- not kubeadm_already_run.stat.exists

- name: Join | Join worker nodes to the cluster
command: sh /root/join-command.sh
when:
- inventory_hostname not in groups['k8s_masters']
- not kubeadm_already_run.stat.exists
5 changes: 5 additions & 0 deletions app/media/MyAnsible/roles/join_worker/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
# tasks file for join_worker

- name: Join worker(s) node to cluster
include_tasks: join_worker.yml
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions app/media/MyAnsible/roles/k8s/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
# handlers file for k8s

- name: Remove temporary GPG key file
file:
path: "/tmp/docker.list"
state: absent

- name: Restart kubelet
service:
name: kubelet
state: restarted
Loading
Loading