Skip to content

Commit

Permalink
fix(compose): edit some related fields to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 committed Dec 4, 2024
2 parents 72814e8 + 22255fe commit 5c60edc
Show file tree
Hide file tree
Showing 32 changed files with 1,897 additions and 294 deletions.
992 changes: 793 additions & 199 deletions app/directory_generators/ansible_generator.py

Large diffs are not rendered by default.

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.
3 changes: 3 additions & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/initk8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- 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

Expand Down Expand Up @@ -49,12 +50,14 @@
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:
Expand Down
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.
67 changes: 66 additions & 1 deletion app/media/MyAnsible/roles/k8s/tasks/k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- name: Disable SWAP in fstab since kubernetes can't work with swap enabled
replace:
path: /etc/fstab
regexp: '^([^#].*?\sswap\ssw\s+.*)$'
regexp: '^([^#].*?\sswap\s+sw\s+.*)$'
replace: '# \1'

- name: Check if ufw is installed
Expand Down Expand Up @@ -128,3 +128,68 @@
systemd:
name: containerd
enabled: yes

- name: Delete the existing Kubernetes APT keyring file if it exists
file:
path: '{{ kubernetes_gpg_keyring_path }}'
state: absent

- name: Download Kubernetes GPG key
shell: |
curl -fsSL '{{ kubernetes_gpg_key_url }}' | gpg --dearmor -o '{{ kubernetes_gpg_keyring_path }}'
- name: Add Kubernetes repo
apt_repository:
repo: "deb [signed-by={{ kubernetes_gpg_keyring_path }}] {{ kubernetes_apt_repo }} /"
state: present
filename: kubernetes.list

- name: Update apt cache
apt:
update_cache: yes

- name: Install Kubernetes packages
apt:
name: "{{ item }}"
state: present
loop:
- kubeadm=1.31.2-1.1
- kubelet=1.31.2-1.1
- kubectl=1.31.2-1.1

- name: Hold Kubernetes packages
dpkg_selections:
name: "{{ item }}"
selection: hold
loop:
- kubeadm
- kubelet
- kubectl
- containerd.io

- name: Configure node ip
lineinfile:
path: /etc/default/kubelet
line: KUBELET_EXTRA_ARGS=--node-ip={{ private_ip }}
create: yes
state: present
notify: Restart kubelet

- name: Add hosts to /etc/hosts
lineinfile:
path: /etc/hosts
line: "{{ hostvars[item].private_ip }} {{ item }} {{ item }}.{{ domain }}"
state: present
create: no
loop: "{{ groups['all'] }}"
when: hostvars[item].private_ip is defined

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

- name: Pull Kubernetes images | If you got error check your dns and sanction
command:
cmd: kubeadm config images pull
11 changes: 0 additions & 11 deletions app/media/MyAnsible/roles/lb/templates/check_apiserveer.sh.j2

This file was deleted.

28 changes: 0 additions & 28 deletions app/media/MyAnsible/roles/lb/templates/haproxy.cfg.j2

This file was deleted.

26 changes: 0 additions & 26 deletions app/media/MyAnsible/roles/lb/templates/keepalived.conf.j2

This file was deleted.

Loading

0 comments on commit 5c60edc

Please sign in to comment.