|
| 1 | +--- |
| 2 | +# Deploy Security Stack (Trivy + Falco) to K3s Cluster |
| 3 | +# Usage: ansible-playbook -i inventories/production.ini playbooks/deploy-security.yml |
| 4 | +- name: Deploy Security Stack (Trivy + Falco) |
| 5 | + hosts: k3s_server[0] |
| 6 | + become: yes |
| 7 | + gather_facts: yes |
| 8 | + |
| 9 | + vars: |
| 10 | + kubeconfig_path: "{{ ansible_env.HOME }}/.kube/k3s-basilica-config" |
| 11 | + k8s_security_dir: "{{ playbook_dir }}/../../k8s/security" |
| 12 | + trivy_operator_image: "ghcr.io/aquasecurity/trivy-operator:0.20.0" |
| 13 | + falco_image: "falcosecurity/falco-no-driver:0.37.0" |
| 14 | + falcosidekick_image: "falcosecurity/falcosidekick:2.28.0" |
| 15 | + |
| 16 | + tasks: |
| 17 | + - name: Verify K3s cluster is healthy |
| 18 | + command: kubectl --kubeconfig={{ kubeconfig_path }} get nodes |
| 19 | + register: nodes_status |
| 20 | + changed_when: false |
| 21 | + failed_when: "'NotReady' in nodes_status.stdout" |
| 22 | + |
| 23 | + - name: Display cluster nodes |
| 24 | + debug: |
| 25 | + msg: "{{ nodes_status.stdout_lines }}" |
| 26 | + |
| 27 | + # ===== Trivy Deployment ===== |
| 28 | + - name: Create trivy-system namespace |
| 29 | + kubernetes.core.k8s: |
| 30 | + kubeconfig: "{{ kubeconfig_path }}" |
| 31 | + state: present |
| 32 | + src: "{{ k8s_security_dir }}/trivy/namespace.yaml" |
| 33 | + |
| 34 | + - name: Deploy Trivy RBAC |
| 35 | + kubernetes.core.k8s: |
| 36 | + kubeconfig: "{{ kubeconfig_path }}" |
| 37 | + state: present |
| 38 | + src: "{{ k8s_security_dir }}/trivy/rbac.yaml" |
| 39 | + |
| 40 | + - name: Deploy Trivy Operator |
| 41 | + kubernetes.core.k8s: |
| 42 | + kubeconfig: "{{ kubeconfig_path }}" |
| 43 | + state: present |
| 44 | + src: "{{ k8s_security_dir }}/trivy/operator-deployment.yaml" |
| 45 | + |
| 46 | + - name: Wait for Trivy Operator readiness |
| 47 | + kubernetes.core.k8s_info: |
| 48 | + kubeconfig: "{{ kubeconfig_path }}" |
| 49 | + kind: Deployment |
| 50 | + namespace: trivy-system |
| 51 | + name: trivy-operator |
| 52 | + wait: yes |
| 53 | + wait_timeout: 300 |
| 54 | + wait_condition: |
| 55 | + type: Available |
| 56 | + status: "True" |
| 57 | + |
| 58 | + - name: Verify Trivy Operator pod status |
| 59 | + command: > |
| 60 | + kubectl --kubeconfig={{ kubeconfig_path }} |
| 61 | + get pods -n trivy-system -l app=trivy-operator -o jsonpath='{.items[0].status.phase}' |
| 62 | + register: trivy_pod_status |
| 63 | + changed_when: false |
| 64 | + |
| 65 | + - name: Display Trivy Operator status |
| 66 | + debug: |
| 67 | + msg: "Trivy Operator status: {{ trivy_pod_status.stdout }}" |
| 68 | + |
| 69 | + # ===== Falco Deployment ===== |
| 70 | + - name: Create falco namespace |
| 71 | + kubernetes.core.k8s: |
| 72 | + kubeconfig: "{{ kubeconfig_path }}" |
| 73 | + state: present |
| 74 | + src: "{{ k8s_security_dir }}/falco/namespace.yaml" |
| 75 | + |
| 76 | + - name: Deploy Falco RBAC |
| 77 | + kubernetes.core.k8s: |
| 78 | + kubeconfig: "{{ kubeconfig_path }}" |
| 79 | + state: present |
| 80 | + src: "{{ k8s_security_dir }}/falco/rbac.yaml" |
| 81 | + |
| 82 | + - name: Deploy Falco ConfigMap |
| 83 | + kubernetes.core.k8s: |
| 84 | + kubeconfig: "{{ kubeconfig_path }}" |
| 85 | + state: present |
| 86 | + src: "{{ k8s_security_dir }}/falco/configmap.yaml" |
| 87 | + |
| 88 | + - name: Deploy Falco GPU security rules |
| 89 | + kubernetes.core.k8s: |
| 90 | + kubeconfig: "{{ kubeconfig_path }}" |
| 91 | + state: present |
| 92 | + src: "{{ k8s_security_dir }}/falco/rules-gpu.yaml" |
| 93 | + |
| 94 | + - name: Deploy Falco DaemonSet |
| 95 | + kubernetes.core.k8s: |
| 96 | + kubeconfig: "{{ kubeconfig_path }}" |
| 97 | + state: present |
| 98 | + src: "{{ k8s_security_dir }}/falco/daemonset.yaml" |
| 99 | + |
| 100 | + - name: Deploy Falcosidekick |
| 101 | + kubernetes.core.k8s: |
| 102 | + kubeconfig: "{{ kubeconfig_path }}" |
| 103 | + state: present |
| 104 | + src: "{{ k8s_security_dir }}/falco/falcosidekick-deployment.yaml" |
| 105 | + |
| 106 | + - name: Wait for Falco DaemonSet rollout |
| 107 | + command: > |
| 108 | + kubectl --kubeconfig={{ kubeconfig_path }} |
| 109 | + rollout status daemonset/falco -n falco --timeout=300s |
| 110 | + register: falco_rollout |
| 111 | + changed_when: false |
| 112 | + |
| 113 | + - name: Verify Falco DaemonSet coverage |
| 114 | + command: > |
| 115 | + kubectl --kubeconfig={{ kubeconfig_path }} |
| 116 | + get daemonset -n falco falco -o jsonpath='{.status.numberReady}/{.status.desiredNumberScheduled}' |
| 117 | + register: falco_coverage |
| 118 | + changed_when: false |
| 119 | + |
| 120 | + - name: Display Falco DaemonSet coverage |
| 121 | + debug: |
| 122 | + msg: "Falco coverage: {{ falco_coverage.stdout }} pods ready" |
| 123 | + |
| 124 | + - name: Wait for Falcosidekick readiness |
| 125 | + kubernetes.core.k8s_info: |
| 126 | + kubeconfig: "{{ kubeconfig_path }}" |
| 127 | + kind: Deployment |
| 128 | + namespace: falco |
| 129 | + name: falcosidekick |
| 130 | + wait: yes |
| 131 | + wait_timeout: 120 |
| 132 | + wait_condition: |
| 133 | + type: Available |
| 134 | + status: "True" |
| 135 | + |
| 136 | + # ===== Common Resources ===== |
| 137 | + - name: Deploy network policies |
| 138 | + kubernetes.core.k8s: |
| 139 | + kubeconfig: "{{ kubeconfig_path }}" |
| 140 | + state: present |
| 141 | + src: "{{ k8s_security_dir }}/common/network-policies.yaml" |
| 142 | + |
| 143 | + - name: Deploy resource quotas |
| 144 | + kubernetes.core.k8s: |
| 145 | + kubeconfig: "{{ kubeconfig_path }}" |
| 146 | + state: present |
| 147 | + src: "{{ k8s_security_dir }}/common/resource-quotas.yaml" |
| 148 | + |
| 149 | + # ===== Verification ===== |
| 150 | + - name: Get security stack pods |
| 151 | + command: > |
| 152 | + kubectl --kubeconfig={{ kubeconfig_path }} |
| 153 | + get pods -n {{ item }} -o wide |
| 154 | + loop: |
| 155 | + - trivy-system |
| 156 | + - falco |
| 157 | + register: security_pods |
| 158 | + changed_when: false |
| 159 | + |
| 160 | + - name: Display security stack status |
| 161 | + debug: |
| 162 | + msg: "{{ item.stdout_lines }}" |
| 163 | + loop: "{{ security_pods.results }}" |
| 164 | + loop_control: |
| 165 | + label: "{{ item.item }}" |
| 166 | + |
| 167 | + - name: Summary |
| 168 | + debug: |
| 169 | + msg: |
| 170 | + - "Security stack deployed successfully" |
| 171 | + - "Trivy Operator: {{ trivy_pod_status.stdout }}" |
| 172 | + - "Falco DaemonSet: {{ falco_coverage.stdout }} pods" |
| 173 | + - "" |
| 174 | + - "Next steps:" |
| 175 | + - " 1. Monitor VulnerabilityReports: kubectl get vulnerabilityreports -A" |
| 176 | + - " 2. Check Falco logs: kubectl logs -n falco -l app=falco --tail=50" |
| 177 | + - " 3. View architecture docs: docs/architecture/TRIVY-FALCO-INTEGRATION.md" |
0 commit comments