Skip to content

Commit 816f8f7

Browse files
committed
fix: add push images in offline docs
Signed-off-by: redscholar <blacktiledhouse@gmail.com>
1 parent 8cde720 commit 816f8f7

19 files changed

Lines changed: 860 additions & 24 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
- hosts:
3+
- all
4+
gather_facts: true
5+
roles:
6+
- native/root
7+
8+
- import_playbook: hook/pre_install.yaml
9+
10+
# Load default variables and perform prechecks on all hosts
11+
- hosts:
12+
- all
13+
gather_facts: true
14+
roles:
15+
- defaults
16+
- precheck
17+
18+
# Download all required software and generate certificates on the localhost
19+
- hosts:
20+
- localhost
21+
roles:
22+
- certs/init
23+
- download
24+
25+
# Initialize all nodes and install necessary software packages
26+
- hosts:
27+
- etcd
28+
- k8s_cluster
29+
- image_registry
30+
- nfs
31+
roles:
32+
- native
33+
34+
# Upgrade external etcd cluster one by one (only when upgrade.etcd is set)
35+
- hosts:
36+
- etcd
37+
serial: 1
38+
gather_facts: true
39+
roles:
40+
- role: etcd/backup
41+
when:
42+
- .etcd.deployment_type | eq "external"
43+
- .upgrade.etcd
44+
- .etcd.etcd_version | semverCompare (printf ">v%s" (index .etcd_install_version "stdout" "etcd Version"))
45+
- role: etcd/prepare
46+
when:
47+
- .etcd.deployment_type | eq "external"
48+
- .upgrade.etcd
49+
- role: etcd/upgrade
50+
when:
51+
- .etcd.deployment_type | eq "external"
52+
- .upgrade.etcd
53+
54+
# Upgrade Kubernetes binaries and container runtime on all cluster nodes
55+
# cri sub-roles control their own behavior via upgrade.cri internally
56+
- hosts:
57+
- k8s_cluster
58+
gather_facts: true
59+
roles:
60+
- cri
61+
- kubernetes/pre-kubernetes
62+
63+
# Upgrade control plane nodes one by one using kubeadm upgrade
64+
- hosts:
65+
- kube_control_plane
66+
serial: 1
67+
gather_facts: true
68+
roles:
69+
- role: kubernetes/upgrade-kubernetes
70+
when: .kubernetes_install_ActiveState.stdout | eq "active"
71+
72+
# Upgrade worker nodes using kubeadm upgrade node
73+
- hosts:
74+
- kube_worker
75+
gather_facts: true
76+
roles:
77+
- role: kubernetes/upgrade-kubernetes
78+
when: .kubernetes_install_ActiveState.stdout | eq "active"
79+
80+
# Upgrade network and storage components if their versions changed
81+
# Only when upgrade.cni / upgrade.storage_class is set
82+
- hosts:
83+
- kube_control_plane|random
84+
gather_facts: true
85+
roles:
86+
- role: cni
87+
when: .upgrade.cni
88+
- role: storageclass
89+
when: .upgrade.storage_class
90+
91+
- import_playbook: hook/post_install.yaml

builtin/core/roles/cri/containerd/tasks/main.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
---
2+
# Scenario 1: Create cluster or upgrading CRI together.
3+
# Full containerd installation/upgrade including backup, runc, binaries, config, and certs.
4+
- name: Containerd | Full containerd installation or upgrade
5+
when:
6+
- or (.kubernetes_install_LoadState.stdout | default "" | ne "loaded") (.upgrade.cri)
7+
block:
8+
- name: Containerd | Backup containerd configuration and binaries before upgrade
9+
when:
10+
- .kubernetes_install_LoadState.stdout | default "" | eq "loaded"
11+
command: |
12+
BACKUP_DIR="/etc/kubekey/backup/containerd/$(date +%Y%m%d-%H%M%S)"
13+
mkdir -p "${BACKUP_DIR}"
14+
cp /etc/containerd/config.toml "${BACKUP_DIR}/" 2>/dev/null || true
15+
cp /usr/local/bin/containerd "${BACKUP_DIR}/" 2>/dev/null || true
16+
cp /usr/local/bin/ctr "${BACKUP_DIR}/" 2>/dev/null || true
17+
cp /usr/local/bin/runc "${BACKUP_DIR}/" 2>/dev/null || true
18+
echo "✅ Containerd backup saved to ${BACKUP_DIR}"
219
320
- name: Containerd | Sync Containerd binaries
421
block:
@@ -71,3 +88,13 @@
7188
when: or .upgrade.cri (or (.containerd_install_version.error | empty | not) (.containerd_install_version.stdout | contains (printf " %s " .cri.containerd_version) | not))
7289
command: |
7390
systemctl daemon-reload && systemctl restart containerd.service && systemctl enable containerd.service
91+
92+
# Scenario 2: Upgrading Kubernetes but NOT upgrading CRI.
93+
# Containerd is already installed and running, skip all installation steps.
94+
- name: Containerd | Skip containerd operations when not upgrading cri
95+
when:
96+
- .kubernetes_install_LoadState.stdout | default "" | eq "loaded"
97+
- .upgrade.cri | not
98+
debug:
99+
msg: >-
100+
⏭️ Containerd is not marked for upgrade (upgrade.cri=false). Skipping containerd installation/update. If the installed containerd version is incompatible with Kubernetes {{ .kubernetes.kube_version }}, set --all or --set upgrade.cri=true to upgrade CRI.

builtin/core/roles/cri/crictl/tasks/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
register: crictl_install_version
66

77
- name: Crictl | Install and configure crictl if not present or version mismatch
8-
when: or (.crictl_install_version.error | empty | not) (.crictl_install_version.stdout | ne (printf "crictl version %s" .cri.crictl_version))
8+
when: or (.upgrade.cri) (.crictl_install_version.error | empty | not) (.crictl_install_version.stdout | ne (printf "crictl version %s" .cri.crictl_version))
99
block:
1010
- name: Crictl | Copy crictl binary archive to the remote node
1111
copy:

builtin/core/roles/cri/docker/tasks/cridockerd.yaml

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
11
---
2+
# cri-dockerd is only needed for Kubernetes >= v1.24.0 (dockershim removal)
3+
- name: Cridockerd | Skip when Kubernetes version is below v1.24.0
4+
when: .kubernetes.kube_version | semverCompare "<v1.24.0"
5+
debug:
6+
msg: "cri-dockerd is not required for Kubernetes < v1.24.0, skipping."
7+
28
- name: Cridockerd | Check if cri-dockerd is installed on the system
9+
when: .kubernetes.kube_version | semverCompare ">=v1.24.0"
310
ignore_errors: true
411
command: cri-dockerd --version
512
register: cridockerd_install_version
613

7-
- name: Cridockerd | Install and configure cri-dockerd if not present or version mismatch
8-
when: or (.cridockerd_install_version.error | empty | not) (.cridockerd_install_version.stdout | hasPrefix (printf "cri-dockerd %s " .cri.cridockerd_version) | not)
14+
# When upgrade.cri=true or create cluster: install or upgrade cri-dockerd if version mismatch
15+
- name: Cridockerd | Install or upgrade cri-dockerd
16+
when:
17+
- .kubernetes.kube_version | semverCompare ">=v1.24.0"
18+
- or (.kubernetes_install_LoadState.stdout | default "" | ne "loaded") (.upgrade.cri)
19+
- or (.cridockerd_install_version.error | empty | not) (.cridockerd_install_version.stdout | hasPrefix (printf "cri-dockerd %s " .cri.cridockerd_version) | not)
20+
block:
21+
- name: Cridockerd | Backup cri-dockerd binary before upgrade
22+
when: .kubernetes_install_LoadState.stdout | default "" | eq "loaded"
23+
command: |
24+
BACKUP_DIR="/etc/kubekey/backup/cridockerd/$(date +%Y%m%d-%H%M%S)"
25+
mkdir -p "${BACKUP_DIR}"
26+
cp /usr/local/bin/cri-dockerd "${BACKUP_DIR}/" 2>/dev/null || true
27+
echo "✅ cri-dockerd backup saved to ${BACKUP_DIR}"
28+
- name: Cridockerd | Copy cri-dockerd binary archive to the remote node
29+
copy:
30+
src: >-
31+
{{ .binary_dir }}/cri-dockerd/{{ .cri.cridockerd_version }}/{{ .binary_type }}/cri-dockerd-{{ .cri.cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz
32+
dest: >-
33+
{{ .tmp_dir }}/cri-dockerd-{{ .cri.cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz
34+
- name: Cridockerd | Extract cri-dockerd binary to /usr/local/bin
35+
command: |
36+
tar -xvf {{ .tmp_dir }}/cri-dockerd-{{ .cri.cridockerd_version | default "" | trimPrefix "v" }}.{{ .binary_type }}.tgz --strip-components=1 -C /usr/local/bin/
37+
- name: Cridockerd | Generate cri-dockerd systemd service file
38+
template:
39+
src: cri-dockerd.service
40+
dest: /etc/systemd/system/cri-dockerd.service
41+
- name: Cridockerd | Start and enable the cri-dockerd service
42+
command: |
43+
systemctl daemon-reload && systemctl start cri-dockerd.service && systemctl enable cri-dockerd.service
44+
45+
# When upgrade.cri=false and K8s is being upgraded: only install cri-dockerd if missing (not upgrade)
46+
- name: Cridockerd | Install cri-dockerd if missing when not upgrading cri
47+
when:
48+
- .kubernetes.kube_version | semverCompare ">=v1.24.0"
49+
- .kubernetes_install_LoadState.stdout | default "" | eq "loaded"
50+
- .upgrade.cri | not
51+
- .cridockerd_install_version.error | empty | not
952
block:
1053
- name: Cridockerd | Copy cri-dockerd binary archive to the remote node
1154
copy:

builtin/core/roles/cri/docker/tasks/main.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
# Scenario 2: Create cluster or upgrading CRI together.
2+
# Perform full Docker installation/upgrade (backup, install, certs).
3+
# cri-dockerd is handled inside the block for K8s >= v1.24.0.
4+
- name: Docker | Full Docker installation or upgrade
5+
when:
6+
- or (.kubernetes_install_LoadState.stdout | default "" | ne "loaded") (.upgrade.cri)
7+
block:
8+
- name: Docker | Backup Docker configuration and binaries before upgrade
9+
when:
10+
- .kubernetes_install_LoadState.stdout | default "" | eq "loaded"
11+
command: |
12+
BACKUP_DIR="/etc/kubekey/backup/docker/$(date +%Y%m%d-%H%M%S)"
13+
mkdir -p "${BACKUP_DIR}"
14+
cp /etc/docker/daemon.json "${BACKUP_DIR}/" 2>/dev/null || true
15+
cp /usr/local/bin/docker "${BACKUP_DIR}/" 2>/dev/null || true
16+
cp /usr/local/bin/dockerd "${BACKUP_DIR}/" 2>/dev/null || true
17+
cp /usr/local/bin/ctr "${BACKUP_DIR}/" 2>/dev/null || true
18+
echo "✅ Docker backup saved to ${BACKUP_DIR}"
19+
120
---
221
# sync docker binary
322
- name: Docker | Sync Docker binaries

builtin/core/roles/defaults/defaults/main/01-cluster_require.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,21 @@ cluster_require:
8989
v1.33: v3.5.21-0
9090
v1.34.0: v3.5.21-0
9191
v1.34.1: v3.5.21-0
92-
v1.34: v3.5.24-0
92+
v1.34: v3.5.24-0
93+
94+
# Kubernetes upgrade path: the highest recommended patch version for each minor release.
95+
# When a user specifies a minor version (e.g., v1.23) for upgrade, the corresponding
96+
# patch version below will be used as the target version.
97+
kube_upgrade_path:
98+
v1.23: v1.23.17
99+
v1.24: v1.24.17
100+
v1.25: v1.25.16
101+
v1.26: v1.26.15
102+
v1.27: v1.27.16
103+
v1.28: v1.28.15
104+
v1.29: v1.29.15
105+
v1.30: v1.30.14
106+
v1.31: v1.31.14
107+
v1.32: v1.32.13
108+
v1.33: v1.33.7
109+
v1.34: v1.34.3

builtin/core/roles/defaults/defaults/main/01-main.yaml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,31 @@ delete:
4444
# This is typically used with --with-data flag or delete.data: true
4545
data: false
4646

47+
# Upgrade component toggles.
48+
# When running 'kk upgrade cluster', only kubelet/kubeadm are upgraded by default.
49+
# Set individual fields to true (or use --all / --set) to upgrade additional components.
4750
upgrade:
48-
# Whether to modify CRI configuration file
51+
# When upgrading the cluster, also upgrade the container runtime (CRI), such as Docker or containerd.
4952
cri: false
5053

54+
# When upgrading the cluster, also upgrade the etcd cluster.
55+
etcd: false
56+
57+
# When upgrading the cluster, also upgrade CoreDNS and NodeLocalDNS.
58+
dns: false
59+
60+
# When upgrading the cluster, also upgrade the private image registry (such as Harbor or registry).
61+
# This is typically used in conjunction with nodes defined in inventory.groups.image_registry.
62+
image_registry: false
63+
64+
# When upgrading the cluster, also upgrade the CNI plugin (e.g., Calico, Cilium, Flannel, Kube-OVN).
65+
cni: false
66+
67+
# When upgrading the cluster, also upgrade the storage class provisioner (e.g., localpv-provisioner, nfs-provisioner).
68+
storage_class: false
69+
70+
# When upgrading the cluster, also upgrade the NFS server.
71+
nfs: false
72+
5173
# image_manifests: List of container images to be synchronized to the private registry
5274
image_manifests: []

builtin/core/roles/defaults/tasks/main.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
ignore_errors: true
4444
command: kubelet --version
4545
register: kubernetes_install_version
46+
- name: Defaults | Get installed containerd version
47+
ignore_errors: true
48+
command: |
49+
containerd --version 2>/dev/null | awk '{print $3}' || true
50+
register: containerd_current_version
51+
- name: Defaults | Get installed docker version
52+
ignore_errors: true
53+
command: |
54+
docker version --format '{{ .Server.Version }}' 2>/dev/null || true
55+
register: docker_current_version
4656

4757
- name: Defaults | Gather ETCD service status
4858
when: .groups.etcd | has .inventory_hostname

builtin/core/roles/etcd/prepare/tasks/main.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@
3131
{{- end -}}
3232
{{ $needUnInstalled | toJson }}
3333
34-
- name: Prepare | Validate installed etcd version
35-
when: .etcd_install_LoadState.stdout | eq "loaded"
36-
block:
37-
- name: Prepare | Ensure target etcd version is not lower than installed version
38-
when: .etcd_install_LoadState.stdout | eq "loaded"
39-
assert:
40-
that: .etcd.etcd_version | semverCompare (printf ">=v%s" (index .etcd_install_version "stdout" "etcd Version"))
41-
fail_msg: >-
42-
Installed etcd version: {{ index .etcd_install_version "stdout" "etcd Version" }} is lower than target etcd version: {{ .etcd.etcd_version }}
43-
4434
- name: Prepare | Distribute etcd package for install or upgrade
4535
when: >-
4636
or
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
- name: UpgradeKubernetes | Backup Kubernetes configurations and binaries before upgrade
3+
command: |
4+
BACKUP_DIR="/etc/kubekey/backup/kubernetes/$(date +%Y%m%d-%H%M%S)"
5+
mkdir -p "${BACKUP_DIR}"
6+
cp /etc/kubernetes/kubeadm-config.yaml "${BACKUP_DIR}/" 2>/dev/null || true
7+
cp /usr/local/bin/kubeadm "${BACKUP_DIR}/" 2>/dev/null || true
8+
cp /usr/local/bin/kubelet "${BACKUP_DIR}/" 2>/dev/null || true
9+
cp /usr/local/bin/kubectl "${BACKUP_DIR}/" 2>/dev/null || true
10+
echo "✅ Kubernetes backup saved to ${BACKUP_DIR}"
11+
12+
- name: UpgradeKubernetes | Fail if kubelet service is not active
13+
assert:
14+
that: .kubernetes_install_ActiveState.stdout | eq "active"
15+
fail_msg: >-
16+
The kubelet service must be running and active before upgrade.
17+
18+
- name: UpgradeKubernetes | Ensure kubeadm configuration exists locally on control plane
19+
when: .groups.kube_control_plane | default list | has .inventory_hostname
20+
command: |
21+
if [ ! -f /etc/kubernetes/kubeadm-config.yaml ] || ! grep -q 'ClusterConfiguration' /etc/kubernetes/kubeadm-config.yaml 2>/dev/null; then
22+
mkdir -p /etc/kubernetes
23+
kubectl get cm kubeadm-config -n kube-system -o=jsonpath='{.data.ClusterConfiguration}' > /etc/kubernetes/kubeadm-config.yaml
24+
fi
25+
26+
- name: UpgradeKubernetes | Upgrade Kubernetes cluster on first control plane node
27+
when:
28+
- .groups.kube_control_plane | default list | has .inventory_hostname
29+
- .init_kubernetes_node | eq .inventory_hostname
30+
command: |
31+
# In Kubernetes, the ImagePullCheck for the pause image uses a hardcoded tag.
32+
# Adding --ignore-preflight-errors=ImagePull allows customization of the pause image reference.
33+
/usr/local/bin/kubeadm upgrade apply {{ .kubernetes.kube_version }} --config=/etc/kubernetes/kubeadm-config.yaml --yes --ignore-preflight-errors=ImagePull
34+
35+
- name: UpgradeKubernetes | Upgrade node configuration on other control plane nodes
36+
when:
37+
- .groups.kube_control_plane | default list | has .inventory_hostname
38+
- .init_kubernetes_node | eq .inventory_hostname | not
39+
command: |
40+
/usr/local/bin/kubeadm upgrade node --ignore-preflight-errors=ImagePull
41+
42+
- name: UpgradeKubernetes | Upgrade node configuration on worker nodes
43+
when: .groups.kube_worker | default list | has .inventory_hostname
44+
command: |
45+
/usr/local/bin/kubeadm upgrade node --ignore-preflight-errors=ImagePull
46+
47+
- name: UpgradeKubernetes | Restart kubelet service after upgrade
48+
command: |
49+
systemctl daemon-reload && systemctl restart kubelet.service
50+
51+
- name: UpgradeKubernetes | Wait for kubelet to be ready after restart
52+
command: |
53+
for ((i=1; i<=30; i++)); do
54+
if systemctl is-active kubelet.service >/dev/null 2>&1; then
55+
echo "✅ kubelet is active"
56+
exit 0
57+
fi
58+
sleep 5
59+
done
60+
echo "❌ kubelet is not active within 150 seconds"
61+
exit 1

0 commit comments

Comments
 (0)