# Pod-Monitoring (Task 6.5): kube-state-metrics und Lesezugang für Prometheus in k3s, danach der Infra-Stack # (Prometheus-Jobs kubelet/cadvisor/kube-state-metrics, file_sd-Ziele, Dashboard „k3s – Pods“). Läuft auf ops: # ansible-playbook playbooks/monitoring-k3s.yml # Token und CA landen auf ops unter /root/.config/knaster (0600), die Rolle infra-stack kopiert sie nach infra. - name: k3s – kube-state-metrics und Zugang für Prometheus hosts: localhost connection: local gather_facts: false vars: manifests: "{{ playbook_dir }}/../../../../deploy/k8s/monitoring" token_file: /root/.config/knaster/k3s-prometheus.token ca_file: /root/.config/knaster/k3s-ca.crt tasks: - name: Manifeste anwenden ansible.builtin.command: kubectl apply -f {{ manifests }}/{{ item }} loop: [prometheus-access.yaml, kube-state-metrics.yaml] register: apply changed_when: "'configured' in apply.stdout or 'created' in apply.stdout" - name: Warten, bis kube-state-metrics bereit ist ansible.builtin.command: kubectl -n kube-system rollout status deployment/kube-state-metrics --timeout=180s changed_when: false - name: Token des ServiceAccounts prometheus-infra lesen (Secret wird vom Controller befüllt) ansible.builtin.shell: > set -o pipefail; kubectl -n kube-system get secret prometheus-infra-token -o go-template='{{ "{{" }}.data.token{{ "}}" }}' | base64 -d args: {executable: /bin/bash} register: token until: token.stdout | length > 0 retries: 10 delay: 2 changed_when: false no_log: true - name: Token als Datei auf ops ablegen (600, nie ausgeben) ansible.builtin.copy: content: "{{ token.stdout }}" dest: "{{ token_file }}" mode: "0600" no_log: true # Die kubelet-Zertifikate sind von k3s-server-ca signiert und tragen die Knoten-IP (19.10. geprüft). - name: CA des Clusters lesen ansible.builtin.shell: > set -o pipefail; kubectl -n kube-system get secret prometheus-infra-token -o go-template='{{ "{{" }}index .data "ca.crt"{{ "}}" }}' | base64 -d args: {executable: /bin/bash} register: ca changed_when: false - name: CA als Datei auf ops ablegen ansible.builtin.copy: content: "{{ ca.stdout }}\n" dest: "{{ ca_file }}" mode: "0600" - name: Zugang prüfen (cAdvisor eines Knotens mit Token und CA) ansible.builtin.shell: > set -o pipefail; ip=$(kubectl get node k3s -o jsonpath='{.status.addresses[?(@.type=="InternalIP")].address}'); curl -fsS -m 10 --cacert {{ ca_file }} -H "Authorization: Bearer $(cat {{ token_file }})" https://$ip:10250/metrics/cadvisor | grep -c '^container_memory_working_set_bytes' args: {executable: /bin/bash} register: probe changed_when: false - name: Ergebnis ansible.builtin.debug: msg: "cAdvisor auf k3s erreichbar, {{ probe.stdout }} Reihen container_memory_working_set_bytes" - name: Infra-LXC – Prometheus, Ziele und Dashboard hosts: infra roles: [infra-stack]