fix2: set /etc/hosts entries for k8s_nodename

pull/1292/head
gjmzj 2023-06-24 17:57:50 +08:00
parent 69da3353a1
commit d50e9becad
4 changed files with 28 additions and 37 deletions

View File

@ -1,15 +1,4 @@
# Note: this playbook can not run independently # Note: this playbook can not run independently
- hosts: "{{ NODE_TO_ADD }}"
tasks:
- name: 设置 k8s_nodename 在 master、node 节点 /etc/hosts 地址解析
lineinfile:
dest: /etc/hosts
state: present
regexp: "{{ K8S_NODENAME }} .* generated by kubeasz"
line: "{{ inventory_hostname }} {{ K8S_NODENAME }} # generated by kubeasz"
delegate_to: "{{ item }}"
with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"]
when: "inventory_hostname != K8S_NODENAME"
- hosts: "{{ NODE_TO_ADD }}" - hosts: "{{ NODE_TO_ADD }}"
roles: roles:

View File

@ -1,15 +1,4 @@
# Note: this playbook cann't run independently # Note: this playbook cann't run independently
- hosts: "{{ NODE_TO_ADD }}"
tasks:
- name: 设置 k8s_nodename 在 master、node 节点 /etc/hosts 地址解析
lineinfile:
dest: /etc/hosts
state: present
regexp: "{{ K8S_NODENAME }} .* generated by kubeasz"
line: "{{ inventory_hostname }} {{ K8S_NODENAME }} # generated by kubeasz"
delegate_to: "{{ item }}"
with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"]
when: "inventory_hostname != K8S_NODENAME"
- hosts: "{{ NODE_TO_ADD }}" - hosts: "{{ NODE_TO_ADD }}"
roles: roles:

View File

@ -27,6 +27,12 @@
- 'HELM_TLS_ENABLE' - 'HELM_TLS_ENABLE'
when: 'DEL_ENV == "yes"' when: 'DEL_ENV == "yes"'
- name: 删除 k8s_nodename 在节点的 /etc/hosts 地址解析
blockinfile:
path: /etc/hosts
state: absent
marker: "### {mark} KUBEASZ MANAGED BLOCK"
#- name: remove binaries #- name: remove binaries
# file: name={{ item }} state=absent # file: name={{ item }} state=absent
# with_items: # with_items:

View File

@ -44,7 +44,6 @@
# 设置节点名称{{ K8S_NODENAME }} /etc/hosts 解析 # 设置节点名称{{ K8S_NODENAME }} /etc/hosts 解析
# 1.先确保第一个主节点做好解析 # 1.先确保第一个主节点做好解析
# 2.然后复制给集群所有节点
- name: 设置 k8s_nodename 在 master[0] 节点 /etc/hosts 地址解析 - name: 设置 k8s_nodename 在 master[0] 节点 /etc/hosts 地址解析
lineinfile: lineinfile:
dest: /etc/hosts dest: /etc/hosts
@ -54,18 +53,26 @@
delegate_to: "{{ groups.kube_master[0] }}" delegate_to: "{{ groups.kube_master[0] }}"
when: "inventory_hostname != K8S_NODENAME" when: "inventory_hostname != K8S_NODENAME"
- name: 获取 master[0] 节点由kubeasz 创建的 /etc/hosts 地址解析 # 2.然后复制给集群所有节点
shell: 'grep "generated by kubeasz" /etc/hosts' - block:
register: RESOLVE_K8S_NODENAME - name: 获取 master[0] 节点由kubeasz 创建的 /etc/hosts 地址解析
delegate_to: "{{ groups.kube_master[0] }}" shell: 'grep "generated by kubeasz" /etc/hosts|sort|uniq'
register: RESOLVE_K8S_NODENAME
delegate_to: "{{ groups.kube_master[0] }}"
- name: 删除 master[0] 节点由kubeasz 创建的 /etc/hosts 地址解析
lineinfile:
dest: /etc/hosts
state: absent
regexp: "generated by kubeasz"
delegate_to: "{{ groups.kube_master[0] }}"
- name: 设置 k8s_nodename 在所有节点的 /etc/hosts 地址解析
blockinfile:
path: /etc/hosts
block: |
{{ RESOLVE_K8S_NODENAME.stdout }}
marker: "### {mark} KUBEASZ MANAGED BLOCK"
delegate_to: "{{ item }}"
with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"]
run_once: true run_once: true
#- debug: var="RESOLVE_K8S_NODENAME.stdout"
# run_once: true
- name: 设置 /etc/hosts 地址解析
blockinfile:
path: /etc/hosts
block: |
{{ RESOLVE_K8S_NODENAME.stdout }}
when: "inventory_hostname != groups.kube_master[0]"