From 1d6d1bea7a77cbe57317a9e2f629743ce80ccd6c Mon Sep 17 00:00:00 2001 From: "tomas.limpinho" Date: Sun, 12 Oct 2025 11:55:56 +0100 Subject: [PATCH] v0 --- .gitea/workflows/deploy-k8s.yml | 23 +++++++++++++++++++++++ inventory.ini | 9 +++++++++ playbook.yml | 15 +++++++++++++++ roles/common/tasks/main.yml | 12 ++++++++++++ roles/kube-master/tasks/main.yml | 21 +++++++++++++++++++++ roles/kube-node/tasks/main.yml | 3 +++ roles/kubernetes/tasks/main.yml | 31 +++++++++++++++++++++++++++++++ 7 files changed, 114 insertions(+) create mode 100644 .gitea/workflows/deploy-k8s.yml create mode 100644 inventory.ini create mode 100644 playbook.yml create mode 100644 roles/common/tasks/main.yml create mode 100644 roles/kube-master/tasks/main.yml create mode 100644 roles/kube-node/tasks/main.yml create mode 100644 roles/kubernetes/tasks/main.yml diff --git a/.gitea/workflows/deploy-k8s.yml b/.gitea/workflows/deploy-k8s.yml new file mode 100644 index 0000000..e1ada71 --- /dev/null +++ b/.gitea/workflows/deploy-k8s.yml @@ -0,0 +1,23 @@ +name: IAC + +on: + push: + branches: [ main ] + workflow_dispatch: + +jobs: + hello: + runs-on: ubuntu-latest + steps: + - name: Cloning ansible repository + uses: actions/checkout@v4 + with: + path: ansible/iac + + - name: Install Ansible + run: sudo apt-get install -y ansible + + - name: Run Ansible Playbook + working-directory: ansible/ + run: | + ansible-playbook -i inventory.ini playbook.yml \ No newline at end of file diff --git a/inventory.ini b/inventory.ini new file mode 100644 index 0000000..70d9f51 --- /dev/null +++ b/inventory.ini @@ -0,0 +1,9 @@ +[kube-master] +master1 ansible_host=192.168.1.10 ansible_user=ubuntu + +[kube-node] +node1 ansible_host=192.168.1.11 ansible_user=ubuntu +node2 ansible_host=192.168.1.12 ansible_user=ubuntu + +[all:vars] +ansible_python_interpreter=/usr/bin/python3 \ No newline at end of file diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..fa4763f --- /dev/null +++ b/playbook.yml @@ -0,0 +1,15 @@ +- name: Configure Kubernetes Master + hosts: kube-master + become: yes + roles: + - common + - kubernetes + - kube-master + +- name: Configure Kubernetes Nodes + hosts: kube-node + become: yes + roles: + - common + - kubernetes + - kube-node diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml new file mode 100644 index 0000000..408dd77 --- /dev/null +++ b/roles/common/tasks/main.yml @@ -0,0 +1,12 @@ +- name: Update apt cache + apt: + update_cache: yes + +- name: Install basic packages + apt: + name: + - curl + - vim + - net-tools + - gnupg + state: present \ No newline at end of file diff --git a/roles/kube-master/tasks/main.yml b/roles/kube-master/tasks/main.yml new file mode 100644 index 0000000..65a2373 --- /dev/null +++ b/roles/kube-master/tasks/main.yml @@ -0,0 +1,21 @@ +- name: Initialize Kubernetes master + command: kubeadm init --pod-network-cidr=192.168.1.3/16 + args: + creates: /etc/kubernetes/admin.conf + +- name: Set up kubeconfig for user + copy: + src: /etc/kubernetes/admin.conf + dest: /home/ubuntu/.kube/config + remote_src: yes + owner: ubuntu + group: ubuntu + mode: 0644 + +- name: Install Calico CNI + become: yes + become_user: ubuntu + shell: | + kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml + environment: + KUBECONFIG: /home/ubuntu/.kube/config diff --git a/roles/kube-node/tasks/main.yml b/roles/kube-node/tasks/main.yml new file mode 100644 index 0000000..24c1eef --- /dev/null +++ b/roles/kube-node/tasks/main.yml @@ -0,0 +1,3 @@ +- name: Join Kubernetes cluster + shell: "{{ hostvars['master1']['kubeadm_join_command'] }}" + when: hostvars['master1']['kubeadm_join_command'] is defined diff --git a/roles/kubernetes/tasks/main.yml b/roles/kubernetes/tasks/main.yml new file mode 100644 index 0000000..ac7d6c9 --- /dev/null +++ b/roles/kubernetes/tasks/main.yml @@ -0,0 +1,31 @@ +- name: Install containerd + apt: + name: containerd + state: present + +- name: Add Kubernetes APT key + apt_key: + url: https://packages.cloud.google.com/apt/doc/apt-key.gpg + state: present + +- name: Add Kubernetes repo + apt_repository: + repo: deb http://apt.kubernetes.io/ kubernetes-xenial main + state: present + +- name: Install Kubernetes components + apt: + name: + - kubelet + - kubeadm + - kubectl + state: present + update_cache: yes + +- name: Hold Kubernetes packages + apt: + name: + - kubelet + - kubeadm + - kubectl + state: hold