Files
Deploy-Laboratory/ansible/playbooks/verify/02-02.yml
2026-03-27 16:58:41 +08:00

82 lines
2.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
# 02-02-nginx-control-ingressroute.md
# nginx M2控制节点 + IngressRoute路径 /demo-m2
- name: Deploy 02-02 nginx control + IngressRoute (M2)
hosts: k3s_server
become: true
run_once: true
vars:
k3s_kubeconfig: /etc/rancher/k3s/k3s.yaml
manifest_src: "{{ playbook_dir }}/../../files/02-05/02-control-ingressroute.yaml"
manifest_dest: /tmp/nginx-m2.yaml
tasks:
- name: Copy manifest
ansible.builtin.copy:
src: "{{ manifest_src }}"
dest: "{{ manifest_dest }}"
mode: "0644"
- name: Apply manifest
ansible.builtin.shell: |
set -e
KUBECONFIG={{ k3s_kubeconfig }} kubectl apply -f {{ manifest_dest }}
args:
executable: /bin/bash
changed_when: true
- name: Verify 02-02 nginx control + IngressRoute (M2)
hosts: k3s_server
become: true
run_once: true
vars:
k3s_kubeconfig: /etc/rancher/k3s/k3s.yaml
verify_entry_base: "{{ nginx_entry_base | default('http://' ~ k3s_server_ip) }}"
tasks:
- name: Rollout status nginx-m2
ansible.builtin.shell: |
set -e
KUBECONFIG={{ k3s_kubeconfig }} kubectl rollout status deployment/nginx-m2 -n default --timeout=180s
args:
executable: /bin/bash
changed_when: false
- name: HTTP check /demo-m2 (retry 503 for convergence)
ansible.builtin.shell: |
set -e
base="{{ verify_entry_base | trim | regex_replace('/+$','') }}"
url="$base/demo-m2/"
ok=0
for i in 1 2 3 4 5 6 7 8 9 10; do
code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 --max-time 8 "$url" 2>/dev/null || echo "000")
echo "try $i: $url -> $code"
if [ "$code" = "200" ]; then ok=1; break; fi
sleep 2
done
test "$ok" = "1"
backend=$(curl -sS -D - -o /dev/null --connect-timeout 3 --max-time 8 "$url" 2>/dev/null | awk -F': ' '/^X-Backend:/{print $2; exit}' | tr -d '\r')
echo "X-Backend=$backend"
test "$backend" = "M2"
args:
executable: /bin/bash
changed_when: false
- name: Teardown 02-02 nginx control + IngressRoute (M2)
hosts: k3s_server
become: true
run_once: true
vars:
k3s_kubeconfig: /etc/rancher/k3s/k3s.yaml
verify_teardown: "{{ (VERIFY_TEARDOWN | default('1')) | string }}"
manifest_dest: /tmp/nginx-m2.yaml
tasks:
- name: Delete resources when VERIFY_TEARDOWN=1
when: verify_teardown == "1"
ansible.builtin.shell: |
set -e
KUBECONFIG={{ k3s_kubeconfig }} kubectl delete -f {{ manifest_dest }} --ignore-not-found=true
args:
executable: /bin/bash
changed_when: true