From e5ef964dc607b12bb0fdd3853f4f6ea763833729 Mon Sep 17 00:00:00 2001 From: tiago Date: Thu, 7 Dec 2023 20:45:13 +0000 Subject: [PATCH] checkpoint 2 up and running --- laravelio-deploy.yml | 7 +-- laravelio-undeploy.yml | 8 +++- roles/laravelio_deploy/tasks/main.yml | 45 +++++++++++++++---- .../templates/laravelio-service.yml | 2 +- roles/mysql_deploy/tasks/main.yml | 1 + roles/mysql_deploy/templates/mysql-pvc.yml | 2 +- roles/post_deploy/tasks/main.yml | 7 ++- roles/pre_deploy/tasks/main.yml | 5 +++ roles/undeploy_all/tasks/main.yml | 44 ++++++++++++++++++ 9 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 roles/pre_deploy/tasks/main.yml create mode 100644 roles/undeploy_all/tasks/main.yml diff --git a/laravelio-deploy.yml b/laravelio-deploy.yml index 717beef..34cd062 100644 --- a/laravelio-deploy.yml +++ b/laravelio-deploy.yml @@ -3,7 +3,8 @@ hosts: localhost gather_facts: false roles: - - mysql_deploy - - laravelio_deploy - - post_deploy + - pre_deploy + - mysql_deploy + - laravelio_deploy + - post_deploy # need to add pre_deploy to pip install kubernetes diff --git a/laravelio-undeploy.yml b/laravelio-undeploy.yml index 996dd9e..016528d 100644 --- a/laravelio-undeploy.yml +++ b/laravelio-undeploy.yml @@ -1,3 +1,7 @@ ---- # Playbook to undeploy laravel.io and its component -# TO DO \ No newline at end of file +--- +- name: Undeploy Laravelio + hosts: localhost + gather_facts: false + roles: + - undeploy_all diff --git a/roles/laravelio_deploy/tasks/main.yml b/roles/laravelio_deploy/tasks/main.yml index f86dfb2..6cc7070 100644 --- a/roles/laravelio_deploy/tasks/main.yml +++ b/roles/laravelio_deploy/tasks/main.yml @@ -5,15 +5,12 @@ kind: Service name: mysql-service namespace: default - register: service_info + register: mysql_service_info - name: Extract ClusterIP from MySQL service set_fact: - cluster_ip: "{{ service_info.resources[0].spec.clusterIP }}" + cluster_ip: "{{ mysql_service_info.resources[0].spec.clusterIP }}" -- name: Print ClusterIP - debug: - var: cluster_ip - name: Create ConfigMap for laravelio kubernetes.core.k8s: name: laravelio-config @@ -29,11 +26,23 @@ DB_USERNAME: laraveliouser DB_PASSWORD: "123456" +- name: Wait for MySQL to be Running + kubernetes.core.k8s_info: + namespace: default + kind: Pod + label_selectors: + - "app=mysql" + register: mysql_pod_info + until: mysql_pod_info.resources[0].status.phase == "Running" + retries: 60 + delay: 5 + - name: Create laravelio Deployment kubernetes.core.k8s: namespace: default state: present src: roles/laravelio_deploy/templates/laravelio-deployment.yml + wait: yes - name: Create laravelio Service kubernetes.core.k8s: @@ -42,9 +51,27 @@ src: roles/laravelio_deploy/templates/laravelio-service.yml wait: yes -- name: Seed database - kubernetes.core.k8s_exec: +# Seed database section + +- name: Get laravelio pod name + kubernetes.core.k8s_info: namespace: default - pod: laravelio - command: "php artisan db:seed" + kind: Pod + label_selectors: + - "app=laravelio" + register: laravelio_pod_info + when: seed_database is defined and seed_database == "true" + +- name: Extract laravelio pod name + set_fact: + laravelio_pod_name: "{{ laravelio_pod_info.resources[0].metadata.name }}" + + +- name: Migrate database + command: "kubectl exec -t {{ laravelio_pod_name }} -- php artisan migrate" + when: seed_database is defined and seed_database == "true" + + +- name: Seed database + command: "kubectl exec -t {{ laravelio_pod_name }} -- php artisan db:seed" when: seed_database is defined and seed_database == "true" diff --git a/roles/laravelio_deploy/templates/laravelio-service.yml b/roles/laravelio_deploy/templates/laravelio-service.yml index 7500b07..06afb78 100644 --- a/roles/laravelio_deploy/templates/laravelio-service.yml +++ b/roles/laravelio_deploy/templates/laravelio-service.yml @@ -8,7 +8,7 @@ metadata: app: laravelio tier: app spec: - type: LoadBalancer + type: LoadBalancer selector: app: laravelio ports: diff --git a/roles/mysql_deploy/tasks/main.yml b/roles/mysql_deploy/tasks/main.yml index 8b230a1..97dec62 100644 --- a/roles/mysql_deploy/tasks/main.yml +++ b/roles/mysql_deploy/tasks/main.yml @@ -17,3 +17,4 @@ state: present src: roles/mysql_deploy/templates/mysql-service.yml wait: yes + diff --git a/roles/mysql_deploy/templates/mysql-pvc.yml b/roles/mysql_deploy/templates/mysql-pvc.yml index 99af15a..ef931f4 100644 --- a/roles/mysql_deploy/templates/mysql-pvc.yml +++ b/roles/mysql_deploy/templates/mysql-pvc.yml @@ -7,7 +7,7 @@ metadata: spec: accessModes: - ReadWriteOnce - storageClassName: local-storage + storageClassName: standard-rwo resources: requests: storage: 20Gi diff --git a/roles/post_deploy/tasks/main.yml b/roles/post_deploy/tasks/main.yml index 7d46872..622384f 100644 --- a/roles/post_deploy/tasks/main.yml +++ b/roles/post_deploy/tasks/main.yml @@ -7,13 +7,16 @@ label_selectors: - "app=laravelio" register: service_info - until: service_facts.resources[0].status.loadBalancer.ingress[0].ip is defined + until: service_info.resources[0].status.loadBalancer.ingress[0].ip is defined retries: 60 delay: 10 - name: Store app_ip set_fact: - app_ip: "{{ service_facts.resources[0].status.loadBalancer.ingress[0].ip }}" + app_ip: "{{ service_info.resources[0].status.loadBalancer.ingress[0].ip }}" +- name: Print app_ip + debug: + var: app_ip - name: Update app_ip in Inventory lineinfile: diff --git a/roles/pre_deploy/tasks/main.yml b/roles/pre_deploy/tasks/main.yml new file mode 100644 index 0000000..7bb5a6d --- /dev/null +++ b/roles/pre_deploy/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Installing Kubernetes through pip + pip: + name: kubernetes + state: present diff --git a/roles/undeploy_all/tasks/main.yml b/roles/undeploy_all/tasks/main.yml new file mode 100644 index 0000000..ff12ae3 --- /dev/null +++ b/roles/undeploy_all/tasks/main.yml @@ -0,0 +1,44 @@ +--- +- name: Undeploy laravelio Service + kubernetes.core.k8s: + namespace: default + state: absent + src: roles/laravelio_deploy/templates/laravelio-service.yml + +- name: Undeploy laravelio Deployment + kubernetes.core.k8s: + namespace: default + state: absent + src: roles/laravelio_deploy/templates/laravelio-deployment.yml + +- name: Undeploy laravelio ConfigMap + kubernetes.core.k8s: + namespace: default + state: absent + definition: + apiVersion: v1 + kind: ConfigMap + metadata: + name: laravelio-config + namespace: default + +- name: Undeploy MySQL Service + kubernetes.core.k8s: + namespace: default + state: absent + src: roles/mysql_deploy/templates/mysql-service.yml + wait: yes + +- name: Undeploy MySQL Deployment + kubernetes.core.k8s: + namespace: default + state: absent + src: roles/mysql_deploy/templates/mysql-deployment.yml + +# add a flag to not delete data +- name: Undeploy MySQL Persistent Volume Claim + kubernetes.core.k8s: + namespace: default + state: absent + src: roles/mysql_deploy/templates/mysql-pvc.yml +