You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.2 KiB

  1. ---
  2. - name: kubeadm | Check if old apiserver cert exists on host
  3. stat:
  4. path: "{{ kube_cert_dir }}/apiserver.pem"
  5. register: old_apiserver_cert
  6. delegate_to: "{{groups['kube-master']|first}}"
  7. run_once: true
  8. - name: kubeadm | Check service account key
  9. stat:
  10. path: "{{ kube_cert_dir }}/sa.key"
  11. register: sa_key_before
  12. delegate_to: "{{groups['kube-master']|first}}"
  13. run_once: true
  14. - name: kubeadm | Check if kubeadm has already run
  15. stat:
  16. path: "{{ kube_cert_dir }}/ca.key"
  17. register: kubeadm_ca
  18. - name: kubeadm | Delete old admin.conf
  19. file:
  20. path: "{{ kube_config_dir }}/admin.conf"
  21. state: absent
  22. when: not kubeadm_ca.stat.exists
  23. - name: kubeadm | Delete old static pods
  24. file:
  25. path: "{{ kube_config_dir }}/manifests/{{item}}.manifest"
  26. state: absent
  27. with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler", "kube-proxy"]
  28. when: old_apiserver_cert.stat.exists
  29. - name: kubeadm | Forcefully delete old static pods
  30. shell: "docker ps -f name=k8s_{{item}} -q | xargs --no-run-if-empty docker rm -f"
  31. with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler"]
  32. when: old_apiserver_cert.stat.exists
  33. - name: kubeadm | aggregate all SANs
  34. set_fact:
  35. apiserver_sans: >-
  36. kubernetes
  37. kubernetes.default
  38. kubernetes.default.svc
  39. kubernetes.default.svc.{{ dns_domain }}
  40. {{ kube_apiserver_ip }}
  41. localhost
  42. 127.0.0.1
  43. {{ ' '.join(groups['kube-master']) }}
  44. {%- if loadbalancer_apiserver is defined %}
  45. {{ apiserver_loadbalancer_domain_name }}
  46. {%- endif %}
  47. {%- for host in groups['kube-master'] -%}
  48. {%- if hostvars[host]['access_ip'] is defined %}{{ hostvars[host]['access_ip'] }}{% endif %}
  49. {{ hostvars[host]['ip'] | default(hostvars[host]['ansible_default_ipv4']['address']) }}
  50. {%- endfor %}
  51. {%- if supplementary_addresses_in_ssl_keys is defined %}
  52. {%- for addr in supplementary_addresses_in_ssl_keys %}
  53. {{ addr }}
  54. {%- endfor %}
  55. {%- endif %}
  56. tags: facts
  57. - name: kubeadm | Copy etcd cert dir under k8s cert dir
  58. command: "cp -TR {{ etcd_cert_dir }} {{ kube_config_dir }}/ssl/etcd"
  59. changed_when: false
  60. - name: kubeadm | Create kubeadm config
  61. template:
  62. src: kubeadm-config.yaml.j2
  63. dest: "{{ kube_config_dir }}/kubeadm-config.yaml"
  64. register: kubeadm_config
  65. - name: kubeadm | Initialize first master
  66. command: timeout -k 240s 240s {{ bin_dir }}/kubeadm init --config={{ kube_config_dir }}/kubeadm-config.yaml --ignore-preflight-errors=all
  67. register: kubeadm_init
  68. # Retry is because upload config sometimes fails
  69. retries: 3
  70. when: inventory_hostname == groups['kube-master']|first and not kubeadm_ca.stat.exists
  71. failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
  72. notify: Master | restart kubelet
  73. - name: kubeadm | Upgrade first master
  74. command: >-
  75. timeout -k 240s 240s
  76. {{ bin_dir }}/kubeadm
  77. upgrade apply -y {{ kube_version }}
  78. --config={{ kube_config_dir }}/kubeadm-config.yaml
  79. --ignore-preflight-errors=all
  80. --allow-experimental-upgrades
  81. --allow-release-candidate-upgrades
  82. register: kubeadm_upgrade
  83. # Retry is because upload config sometimes fails
  84. retries: 3
  85. when: inventory_hostname == groups['kube-master']|first and (kubeadm_config.changed and kubeadm_ca.stat.exists)
  86. failed_when: kubeadm_upgrade.rc != 0 and "field is immutable" not in kubeadm_upgrade.stderr
  87. notify: Master | restart kubelet
  88. # FIXME(mattymo): remove when https://github.com/kubernetes/kubeadm/issues/433 is fixed
  89. - name: kubeadm | Enable kube-proxy
  90. command: "{{ bin_dir }}/kubeadm alpha phase addon kube-proxy --config={{ kube_config_dir }}/kubeadm-config.yaml"
  91. when: inventory_hostname == groups['kube-master']|first
  92. changed_when: false
  93. - name: slurp kubeadm certs
  94. slurp:
  95. src: "{{ item }}"
  96. with_items:
  97. - "{{ kube_cert_dir }}/apiserver.crt"
  98. - "{{ kube_cert_dir }}/apiserver.key"
  99. - "{{ kube_cert_dir }}/apiserver-kubelet-client.crt"
  100. - "{{ kube_cert_dir }}/apiserver-kubelet-client.key"
  101. - "{{ kube_cert_dir }}/ca.crt"
  102. - "{{ kube_cert_dir }}/ca.key"
  103. - "{{ kube_cert_dir }}/front-proxy-ca.crt"
  104. - "{{ kube_cert_dir }}/front-proxy-ca.key"
  105. - "{{ kube_cert_dir }}/front-proxy-client.crt"
  106. - "{{ kube_cert_dir }}/front-proxy-client.key"
  107. - "{{ kube_cert_dir }}/sa.key"
  108. - "{{ kube_cert_dir }}/sa.pub"
  109. register: kubeadm_certs
  110. delegate_to: "{{ groups['kube-master']|first }}"
  111. run_once: true
  112. - name: kubeadm | write out kubeadm certs
  113. copy:
  114. dest: "{{ item.item }}"
  115. content: "{{ item.content | b64decode }}"
  116. owner: root
  117. group: root
  118. mode: 0700
  119. no_log: true
  120. register: copy_kubeadm_certs
  121. with_items: "{{ kubeadm_certs.results }}"
  122. when: inventory_hostname != groups['kube-master']|first
  123. - name: kubeadm | Init other uninitialized masters
  124. command: timeout -k 240s 240s {{ bin_dir }}/kubeadm init --config={{ kube_config_dir }}/kubeadm-config.yaml --ignore-preflight-errors=all
  125. register: kubeadm_init
  126. when: inventory_hostname != groups['kube-master']|first and not kubeadm_ca.stat.exists
  127. failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
  128. notify: Master | restart kubelet
  129. - name: kubeadm | Upgrade other masters
  130. command: >-
  131. timeout -k 240s 240s
  132. {{ bin_dir }}/kubeadm
  133. upgrade apply -y {{ kube_version }}
  134. --config={{ kube_config_dir }}/kubeadm-config.yaml
  135. --ignore-preflight-errors=all
  136. --allow-experimental-upgrades
  137. --allow-release-candidate-upgrades
  138. register: kubeadm_upgrade
  139. when: inventory_hostname != groups['kube-master']|first and (kubeadm_config.changed and kubeadm_ca.stat.exists)
  140. failed_when: kubeadm_upgrade.rc != 0 and "field is immutable" not in kubeadm_upgrade.stderr
  141. notify: Master | restart kubelet
  142. - name: kubeadm | Check service account key again
  143. stat:
  144. path: "{{ kube_cert_dir }}/sa.key"
  145. register: sa_key_after
  146. delegate_to: "{{groups['kube-master']|first}}"
  147. run_once: true
  148. - name: kubeadm | Set secret_changed if service account key was updated
  149. command: /bin/true
  150. notify: Master | set secret_changed
  151. when: sa_key_before.stat.checksum|default("") != sa_key_after.stat.checksum
  152. - name: kubeadm | cleanup old certs if necessary
  153. import_tasks: kubeadm-cleanup-old-certs.yml
  154. when: old_apiserver_cert.stat.exists