mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-25 17:12:50 +00:00
feat: More detailed init-os rules (#2563)
Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
parent
fb474bde50
commit
eeb4237e8a
|
|
@ -31,7 +31,10 @@
|
||||||
|
|
||||||
# init os
|
# init os
|
||||||
- hosts:
|
- hosts:
|
||||||
|
- etcd
|
||||||
- k8s_cluster
|
- k8s_cluster
|
||||||
|
- registry
|
||||||
|
- nfs
|
||||||
roles:
|
roles:
|
||||||
- init/init-os
|
- init/init-os
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -1,29 +1,32 @@
|
||||||
---
|
---
|
||||||
- include_tasks: init_repository.yaml
|
- name: Init for all nodes always
|
||||||
|
block:
|
||||||
|
- include_tasks: init_ntpserver.yaml
|
||||||
|
- include_tasks: init_localdns.yaml
|
||||||
|
|
||||||
- include_tasks: init_ntpserver.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 }} \
|
||||||
|
&& sed -i '/^127.0.1.1/s/.*/127.0.1.1 {{ .inventory_hostname }}/g' /etc/hosts
|
||||||
|
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
|
||||||
|
|
||||||
- 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 }} \
|
|
||||||
&& sed -i '/^127.0.1.1/s/.*/127.0.1.1 {{ .inventory_hostname }}/g' /etc/hosts
|
|
||||||
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
|
|
||||||
|
|
|
||||||
|
|
@ -201,55 +201,6 @@ else
|
||||||
fi
|
fi
|
||||||
sysctl -p
|
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
|
sync
|
||||||
echo 3 > /proc/sys/vm/drop_caches
|
echo 3 > /proc/sys/vm/drop_caches
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@
|
||||||
etcdctl snapshot restore /tmp/kubekey/etcd/snapshot.db \
|
etcdctl snapshot restore /tmp/kubekey/etcd/snapshot.db \
|
||||||
--name={{ .inventory_hostname }} --endpoints=https://{{ .internal_ipv4 }}:2379 \
|
--name={{ .inventory_hostname }} --endpoints=https://{{ .internal_ipv4 }}:2379 \
|
||||||
{{- $ips := list -}}
|
{{- $ips := list -}}
|
||||||
{{- range $element := .groups.etcd -}}
|
{{- range .groups.etcd -}}
|
||||||
{{- $ips = append $ips (printf "%s=https://%s:2380" (index .hostvars $element "inventory_hostname") (index .hostvars $element "internal_ipv4") -}}
|
{{- $ips = append $ips (printf "%s=https://%s:2380" . (index $.hostvars . "internal_ipv4") -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
--initial-cluster={{ $ips | join "," }} \
|
--initial-cluster={{ $ips | join "," }} \
|
||||||
--initial-advertise-peer-urls=https://{{ .internal_ipv4 }}:2380\
|
--initial-advertise-peer-urls=https://{{ .internal_ipv4 }}:2380\
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue