diff --git a/ansible/inventories/kubernetes.yml b/ansible/inventories/kubernetes.yml index da756f0..de23916 100644 --- a/ansible/inventories/kubernetes.yml +++ b/ansible/inventories/kubernetes.yml @@ -6,15 +6,15 @@ all: control_plane: hosts: controlplane: - ansible_host: 10.203.146.176 + ansible_host: 10.203.146.7 workers: hosts: node01: - ansible_host: 10.203.146.190 + ansible_host: 10.203.146.55 node02: - ansible_host: 10.203.146.198 + ansible_host: 10.203.146.254 node03: - ansible_host: 10.203.146.250 + ansible_host: 10.203.146.131 vars: ansible_user: ubuntu ansible_become: yes diff --git a/ansible/playbooks/kubernetes.yml b/ansible/playbooks/kubernetes.yml index 1d82dad..7eeb380 100644 --- a/ansible/playbooks/kubernetes.yml +++ b/ansible/playbooks/kubernetes.yml @@ -49,6 +49,17 @@ tasks: - include_tasks: ../tasks/kubernetes/worker-join.yml +- name: Deploy Rancher Local Path Provisioner + hosts: k8s_cluster + become: yes + vars_files: + - ../defaults/kubernetes.yml + tags: + - storage + - localpath + tasks: + - include_tasks: ../tasks/kubernetes/deploy-localpath.yml + - name: Validate Kubernetes cluster hosts: k8s_cluster become: yes diff --git a/ansible/tasks/kubernetes/deploy-localpath.yml b/ansible/tasks/kubernetes/deploy-localpath.yml new file mode 100644 index 0000000..1856ba3 --- /dev/null +++ b/ansible/tasks/kubernetes/deploy-localpath.yml @@ -0,0 +1,65 @@ +--- +# ansible/tasks/kubernetes/deploy-localpath.yml +# Deploy Rancher Local Path Provisioner to worker nodes + +- name: Create local-path directory on worker nodes + file: + path: /opt/local-path-provisioner + state: directory + mode: '0755' + delegate_to: "{{ item }}" + with_items: "{{ groups['workers'] }}" + +- name: Download Local Path Provisioner YAML + get_url: + url: https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml + dest: /tmp/local-path-storage.yaml + delegate_to: "{{ groups['control_plane'][0] }}" + register: download_result + +- name: Apply Local Path Provisioner YAML + shell: kubectl --kubeconfig=/etc/kubernetes/admin.conf apply -f /tmp/local-path-storage.yaml + delegate_to: "{{ groups['control_plane'][0] }}" + register: apply_result + changed_when: apply_result.rc == 0 + failed_when: apply_result.rc != 0 and "already exists" not in apply_result.stderr + +- name: Wait for Local Path Provisioner to be ready + shell: > + kubectl --kubeconfig=/etc/kubernetes/admin.conf -n local-path-storage wait + --for=condition=ready pod + --selector=app=local-path-provisioner + --timeout=60s + delegate_to: "{{ groups['control_plane'][0] }}" + register: wait_result + changed_when: false + failed_when: false + +- name: Get Local Path Provisioner status + shell: > + kubectl --kubeconfig=/etc/kubernetes/admin.conf -n local-path-storage get pod + delegate_to: "{{ groups['control_plane'][0] }}" + register: status_result + changed_when: false + +- name: Display Local Path Provisioner status + debug: + msg: "{{ status_result.stdout_lines }}" + +- name: Verify StorageClass was created + shell: > + kubectl --kubeconfig=/etc/kubernetes/admin.conf get storageclass | grep local-path + delegate_to: "{{ groups['control_plane'][0] }}" + register: sc_result + changed_when: false + failed_when: false + +- name: Set Local Path Provisioner as default StorageClass + shell: > + kubectl --kubeconfig=/etc/kubernetes/admin.conf patch storageclass local-path + -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}' + delegate_to: "{{ groups['control_plane'][0] }}" + register: default_sc_result + changed_when: default_sc_result.rc == 0 + failed_when: false + when: sc_result.rc == 0