feat: More detailed init-os rules (#2563)

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-05-13 17:25:01 +08:00 committed by GitHub
parent fb474bde50
commit eeb4237e8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 83 additions and 77 deletions

View File

@ -31,7 +31,10 @@
# init os
- hosts:
- etcd
- k8s_cluster
- registry
- nfs
roles:
- init/init-os

View File

@ -0,0 +1,49 @@
- name: Set local DNS
command: |
# clear old dns configuration
sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' /etc/hosts
sed -i '/^$/N;/\n$/N;//D' /etc/hosts
# defined new dns configuration
cat >>/etc/hosts<<EOF
# kubekey hosts BEGIN
# kubernetes hosts
{{- range .groups.k8s_cluster | default list }}
{{- $hostname := index $.hostvars . "hostname" -}}
{{- $clusterName := $.kubernetes.cluster_name | default "kubekey" -}}
{{- $dnsDomain := $.kubernetes.networking.dns_domain | default "cluster.local" -}}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ $hostname }} {{ printf "%s.%s" $hostname $clusterName }} {{ printf "%s.%s.%s" $hostname $clusterName $dnsDomain }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv6" }} {{ $hostname }} {{ printf "%s.%s" $hostname $clusterName }} {{ printf "%s.%s.%s" $hostname $clusterName $dnsDomain }}
{{- end }}
{{- end }}
# etcd hosts
{{- range .groups.etcd | default list }}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv6" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- end }}
# image registry hosts
{{- range .groups.image_registry | default list }}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv6" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- end }}
# nfs hosts
{{- range .groups.nfs | default list }}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- end }}
# kubekey hosts END
EOF

View File

@ -1,15 +1,19 @@
---
- include_tasks: init_repository.yaml
- name: Init for all nodes always
block:
- include_tasks: init_ntpserver.yaml
- include_tasks: init_localdns.yaml
- name: Init for new kubernetes nodes
when: .kubernetes_install_service.stdout | eq "inactive"
block:
- include_tasks: init_repository.yaml
- name: Reset tmp dir
command: |
if [ -d {{ .tmp_dir }} ]; then
rm -rf {{ .tmp_dir }}
fi
mkdir -m 777 -p {{ .tmp_dir }}
- name: Set hostname
command: |
hostnamectl set-hostname {{ .inventory_hostname }} \
@ -17,13 +21,12 @@
when:
- .set_hostname
- .inventory_hostname | ne "localhost"
- name: Sync init os to remote
template:
src: init-os.sh
dest: /etc/kubekey/scripts/init-os.sh
mode: 0755
- name: Execute init os script
command: |
/etc/kubekey/scripts/init-os.sh

View File

@ -201,55 +201,6 @@ else
fi
sysctl -p
# ------------------------ 8. Local Host DNS Configuration ---------------------
sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' /etc/hosts
sed -i '/^$/N;/\n$/N;//D' /etc/hosts
cat >>/etc/hosts<<EOF
# kubekey hosts BEGIN
# kubernetes hosts
{{- range .groups.k8s_cluster | default list }}
{{- $hostname := index $.hostvars . "hostname" -}}
{{- $clusterName := $.kubernetes.cluster_name | default "kubekey" -}}
{{- $dnsDomain := $.kubernetes.networking.dns_domain | default "cluster.local" -}}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ $hostname }} {{ printf "%s.%s" $hostname $clusterName }} {{ printf "%s.%s.%s" $hostname $clusterName $dnsDomain }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv6" }} {{ $hostname }} {{ printf "%s.%s" $hostname $clusterName }} {{ printf "%s.%s.%s" $hostname $clusterName $dnsDomain }}
{{- end }}
{{- end }}
# etcd hosts
{{- range .groups.etcd | default list }}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv6" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- end }}
# image registry hosts
{{- range .groups.image_registry | default list }}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv6" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- end }}
# nfs hosts
{{- range .groups.nfs | default list }}
{{- if and (index $.hostvars . "internal_ipv4") (ne (index $.hostvars . "internal_ipv4") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- if and (index $.hostvars . "internal_ipv6") (ne (index $.hostvars . "internal_ipv6") "") }}
{{ index $.hostvars . "internal_ipv4" }} {{ index $.hostvars . "hostname" }}
{{- end }}
{{- end }}
# kubekey hosts END
EOF
sync
echo 3 > /proc/sys/vm/drop_caches

View File

@ -19,8 +19,8 @@
etcdctl snapshot restore /tmp/kubekey/etcd/snapshot.db \
--name={{ .inventory_hostname }} --endpoints=https://{{ .internal_ipv4 }}:2379 \
{{- $ips := list -}}
{{- range $element := .groups.etcd -}}
{{- $ips = append $ips (printf "%s=https://%s:2380" (index .hostvars $element "inventory_hostname") (index .hostvars $element "internal_ipv4") -}}
{{- range .groups.etcd -}}
{{- $ips = append $ips (printf "%s=https://%s:2380" . (index $.hostvars . "internal_ipv4") -}}
{{- end -}}
--initial-cluster={{ $ips | join "," }} \
--initial-advertise-peer-urls=https://{{ .internal_ipv4 }}:2380\