Created
September 7, 2023 03:18
-
-
Save wu0407/946354660972f81613417ce5a5904a4d to your computer and use it in GitHub Desktop.
ansible replace kuberentes cluster dns server ip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: replace coredns | |
gather_facts: false | |
hosts: coredns | |
tasks: | |
# echo "[coredns]" >> /tmp/coredns-host; kubectl get pod -n kube-system -l k8s-app=kube-dns --no-headers -o custom-columns=:spec.nodeName >> /tmp/coredns-host | |
# ansible-playbook -i /tmp/coredns-host replace-coredns.yaml | |
- name: detect docker container runtime | |
stat: | |
path: /var/run/dockershim.sock | |
register: docker_stat | |
- name: replace coredns /etc/resolv.conf use docker | |
shell: docker inspect $(docker ps |grep k8s_coredns_coredns | awk '{print $1}') --format {% raw %} '{{ .ResolvConfPath }}' {% endraw %} | xargs -i bash -c "cat /etc/resolv.conf > {}" | |
when: docker_stat.stat.exists | |
- name: replace coredns /etc/resolv.conf use crictl | |
shell: crictl inspect -o go-template --template {% raw %} '{{- range $mount := .info.runtimeSpec.mounts -}} {{- if eq $mount.destination "/etc/resolv.conf" -}} {{- $mount.source -}} {{- end -}} {{- end -}}' {% endraw %} $(crictl ps |grep coredns |awk '{print $1}') | xargs -i bash -c "cat /etc/resolv.conf > {}" | |
when: not docker_stat.stat.exists |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: update dns | |
hosts: all | |
tasks: | |
# ansible-playbook replace-host-local-dns.yaml | |
- name: copy /etc/resolv.conf | |
copy: | |
src: /etc/resolv.conf | |
dest: /etc/resolv.conf | |
- name: detect node local dns | |
shell: pgrep node-cache | |
register: node_local_dns | |
ignore_errors: yes | |
- name: detect docker container runtime | |
stat: | |
path: /var/run/dockershim.sock | |
register: docker_stat | |
- name: change /etc/resolv.conf for local dns pod use docker | |
shell: cat /etc/resolv.conf > $(docker inspect $(docker ps |grep k8s_node-cache_node-local-dns | awk '{print $1}') --format {% raw %} '{{ .ResolvConfPath }}' {% endraw %}) | |
when: docker_stat.stat.exists == True and node_local_dns.rc == 0 | |
- name: change /etc/resolv.conf for local dns pod use crictl | |
shell: cat /etc/resolv.conf > $(crictl inspect -o go-template --template {% raw %} '{{- range $mount := .info.runtimeSpec.mounts -}} {{- if eq $mount.destination "/etc/resolv.conf" -}} {{- $mount.source -}} {{- end -}} {{- end -}}' {% endraw %} $(crictl ps |grep node-local-dns |awk '{print $1}')) | |
when: docker_stat.stat.exists == False and node_local_dns.rc == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add break line in configmap for node-local-dns and coredns
more details in Gracefully Changing the DNS Server IP for node on a Kubernetes Cluster Without Impacting Applications