diff --git a/builtin/core/playbooks/delete_cluster.yaml b/builtin/core/playbooks/delete_cluster.yaml index f49f456a..28155216 100644 --- a/builtin/core/playbooks/delete_cluster.yaml +++ b/builtin/core/playbooks/delete_cluster.yaml @@ -14,12 +14,13 @@ - role: uninstall/cri when: .deleteCRI post_tasks: - - name: delete etc/hosts + - name: delete localDNS file ignore_errors: true command: | - sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' /etc/hosts - sed -i ':a;$!{N;ba};s@# kubekey custom BEGIN.*# kubekey custom END@@' /etc/hosts + sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' {{ .item }} + sed -i ':a;$!{N;ba};s@# kubekey custom BEGIN.*# kubekey custom END@@' {{ .item }} when: .deleteDNS + loop: "{{ .localDNS | toJson }}" - hosts: - etcd diff --git a/builtin/core/playbooks/delete_nodes.yaml b/builtin/core/playbooks/delete_nodes.yaml index 17746ad1..f373e667 100644 --- a/builtin/core/playbooks/delete_nodes.yaml +++ b/builtin/core/playbooks/delete_nodes.yaml @@ -57,14 +57,15 @@ - .deleteCRI - .delete_nodes | default list | has .inventory_hostname post_tasks: - - name: delete etc/hosts + - name: delete localDNS file ignore_errors: true command: | - sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' /etc/hosts - sed -i ':a;$!{N;ba};s@# kubekey custom BEGIN.*# kubekey custom END@@' /etc/hosts + sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' {{ .item }} + sed -i ':a;$!{N;ba};s@# kubekey custom BEGIN.*# kubekey custom END@@' {{ .item }} when: - .deleteDNS - .delete_nodes | default list | has .inventory_hostname + loop: "{{ .localDNS | toJson }}" - hosts: - etcd diff --git a/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml b/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml index 8b828851..a1543474 100644 --- a/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml +++ b/builtin/core/playbooks/vars/create_cluster_kubernetes.yaml @@ -149,7 +149,7 @@ kubernetes: # 1. When a DNS domain name is available: # Set control_plane_endpoint to the DNS domain name, and configure the domain name to point to all control_plane node IPs. # 2. When a DNS domain name is not available: - # Set control_plane_endpoint to a DNS domain name that can be extended later. Add the DNS domain name resolution to the local /etc/hosts file on each node with the format: + # Set control_plane_endpoint to a DNS domain name that can be extended later. Add the DNS domain name resolution to the localDNS file on each node with the format: #{{ vip }} {{ control_plane_endpoint }} # - When a VIP is available: # Deploy kube-vip on the control_plane nodes to map the VIP to the actual control_plane node IPs. @@ -162,7 +162,7 @@ kubernetes: host: lb.kubesphere.local port: "{{ .kubernetes.apiserver.port }}" # support local, kube_vip, haproxy - # when type is local, # if set will write in /etc/hosts. + # when type is local, # if set will write in localDNS file. # - for control-plane: will use 127.0.0.1 {{ .kubernetes.control_plane_endpoint.host }} # - for worker: will use {{ .init_kubernetes_node }} {{ .kubernetes.control_plane_endpoint.host }} type: local @@ -191,3 +191,6 @@ kubernetes: # if auto renew kubernetes certs renew_certs: enabled: true + +localDNS: + - /etc/hosts \ No newline at end of file diff --git a/builtin/core/playbooks/vars/delete_cluster.yaml b/builtin/core/playbooks/vars/delete_cluster.yaml index 146b4f74..a67f797e 100644 --- a/builtin/core/playbooks/vars/delete_cluster.yaml +++ b/builtin/core/playbooks/vars/delete_cluster.yaml @@ -4,5 +4,7 @@ kubernetes: deleteCRI: false # if deleteETCD deleteETCD: false -# if true. will delete local dns in /etc/hosts which create by kubekey -deleteDNS: false \ No newline at end of file +# if true. will delete local dns in localDNS file which create by kubekey +deleteDNS: false +localDNS: + - /etc/hosts \ No newline at end of file diff --git a/builtin/core/roles/init/init-os/tasks/init_localdns.yaml b/builtin/core/roles/init/init-os/tasks/init_localdns.yaml index 822e2d9f..3d98737f 100644 --- a/builtin/core/roles/init/init-os/tasks/init_localdns.yaml +++ b/builtin/core/roles/init/init-os/tasks/init_localdns.yaml @@ -1,10 +1,11 @@ - name: Set local DNS + loop: "{{ .localDNS | toJson }}" 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 + sed -i ':a;$!{N;ba};s@# kubekey hosts BEGIN.*# kubekey hosts END@@' {{ .item }} + sed -i '/^$/N;/\n$/N;//D' {{ .item }} # defined new dns configuration - cat >>/etc/hosts<>{{ .item }}<> /etc/hosts << EOF + cat >> {{ .item }} << EOF # haproxy dns for kubekey begin {{- if ne .internal_ipv4 "" }} {{ .internal_ipv4 }} {{ .kubernetes.control_plane_endpoint.host }} @@ -48,6 +48,7 @@ EOF when: - .kubernetes.control_plane_endpoint.type | eq "haproxy" + loop: "{{ .localDNS | toJson }}" - name: Init kubernetes cluster command: | diff --git a/builtin/core/roles/kubernetes/init-kubernetes/tasks/main.yaml b/builtin/core/roles/kubernetes/init-kubernetes/tasks/main.yaml index 8f70a8e8..81be6527 100644 --- a/builtin/core/roles/kubernetes/init-kubernetes/tasks/main.yaml +++ b/builtin/core/roles/kubernetes/init-kubernetes/tasks/main.yaml @@ -22,15 +22,16 @@ {{ index $notInitNodes (randInt 0 ((sub ($notInitNodes | len) 1) | int)) }} {{- end -}} -- name: Set custom hosts to /etc/hosts +- name: Set custom hosts to localDNS file when: eq .kubernetes.control_plane_endpoint.type "local" command: | - sed -i ':a;$!{N;ba};s@# kubekey custom BEGIN.*# kubekey custom END@@' /etc/hosts - cat >> /etc/hosts <> {{ .item }} <> /etc/hosts <> {{ .item }} <> /etc/hosts <> {{ .item }} <> /etc/hosts << EOF + cat >> {{ .item }} << EOF # haproxy dns for kubekey begin {{- $internalIPv4 := index .hostvars (.groups.kube_control_plane | default list | first) "internal_ipv4" | default "" }} {{- $internalIPv6 := index .hostvars (.groups.kube_control_plane | default list | first) "internal_ipv6" | default "" }} @@ -17,9 +17,10 @@ # haproxy dns for kubekey end EOF when: .kubernetes.control_plane_endpoint.type | eq "haproxy" + loop: "{{ .localDNS | toJson }}" - include_tasks: join_kubernetes.yaml when: .kubernetes_install_service.stdout | eq "inactive" - - name: Replace haproxy dns to /etc/hosts + - name: Replace haproxy dns to localDNS file when: .kubernetes.control_plane_endpoint.type | eq "haproxy" block: - name: Replace control_plane by local hosts @@ -32,11 +33,13 @@ {{ .internal_ipv6 }} {{ .kubernetes.control_plane_endpoint.host }} {{- end }} # haproxy dns for kubekey end" - sed -i '/# haproxy dns for kubekey begin/,/# haproxy dns for kubekey end/c\'"$new_dns" /etc/hosts + sed -i '/# haproxy dns for kubekey begin/,/# haproxy dns for kubekey end/c\'"$new_dns" {{ .item }} + loop: "{{ .localDNS | toJson }}" - name: Replace worker by haproxy hosts when: .groups.worker | default list | has .inventory_hostname command: | new_dns="# haproxy dns for kubekey begin {{ .kubernetes.control_plane_endpoint.haproxy.address }} {{ .kubernetes.control_plane_endpoint.host }} # haproxy dns for kubekey end" - sed -i '/# haproxy dns for kubekey begin/,/# haproxy dns for kubekey end/c\'"$new_dns" /etc/hosts + sed -i '/# haproxy dns for kubekey begin/,/# haproxy dns for kubekey end/c\'"$new_dns" {{ .item }} + loop: "{{ .localDNS | toJson }}"