Merge pull request #61 from svc-design/k3s-cluster

refactor(k3s-cluster): use synchronize to run scripts
This commit is contained in:
shenlan 2025-04-07 23:42:19 +08:00 committed by GitHub
commit ea8629dc84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 78 additions and 1163 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,37 @@
#!/bin/bash
set -e
# === 必要参数 ===
INSTALL_K3S_URL="https://get.k3s.io"
FLANNEL_IFACE=${FLANNEL_IFACE:-br0}
# === 安装命令拼接(最简)===
INSTALL_K3S_EXEC="server \
--disable=traefik,servicelb,local-storage \
--data-dir=/opt/rancher/k3s \
--advertise-address=$(hostname -I | awk '{print $1}') \
--kube-apiserver-arg=service-node-port-range=0-50000"
[[ -n "$FLANNEL_IFACE" ]] && INSTALL_K3S_EXEC+=" --flannel-iface=${FLANNEL_IFACE}"
# === 下载并执行安装 ===
curl -sfL ${INSTALL_K3S_URL} -o install_k3s.sh && chmod +x install_k3s.sh
INSTALL_K3S_EXEC="$INSTALL_K3S_EXEC" ./install_k3s.sh
# === 等待 CoreDNS 启动 ===
echo "⏳ 等待 CoreDNS 启动..."
until kubectl get pods -A 2>/dev/null | grep -q "coredns.*Running"; do
sleep 3
done
# === 设置本地 kubeconfig ===
mkdir -p ~/.kube
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
chmod 600 ~/.kube/config
export KUBECONFIG=~/.kube/config
curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
echo "✅ K3s 安装完成kubectl/helm 已就绪"

View File

@ -1,6 +1,44 @@
---
- name: Run install_k3s.sh
script: install_k3s.sh
- name: Ensure remote tmp directory exists
file:
path: /tmp/ansible-{{ ansible_user }}
state: directory
mode: '0755'
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
- name: Sync setup_k3s.sh to remote
synchronize:
src: files/setup_k3s.sh
dest: /tmp/ansible-{{ ansible_user }}/setup_k3s.sh
mode: push
delegate_to: localhost
become: false
- name: Sync set-registry.sh to remote
synchronize:
src: files/set-registry.sh
dest: /tmp/ansible-{{ ansible_user }}/set-registry.sh
mode: push
delegate_to: localhost
become: false
- name: Ensure setup_k3s.sh is executable
file:
path: /tmp/ansible-{{ ansible_user }}/setup_k3s.sh
mode: '0755'
- name: Ensure set-registry.sh is executable
file:
path: /tmp/ansible-{{ ansible_user }}/set-registry.sh
mode: '0755'
- name: Run setup_k3s.sh
command: ./setup_k3s.sh
args:
chdir: /tmp/ansible-{{ ansible_user }}
- name: Run set-registry.sh
script: set-registry.sh
command: ./set-registry.sh
args:
chdir: /tmp/ansible-{{ ansible_user }}