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.

185 lines
6.4 KiB

6 years ago
6 years ago
6 years ago
  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 | Migrate old certs if necessary
  9. import_tasks: kubeadm-migrate-certs.yml
  10. when: old_apiserver_cert.stat.exists
  11. - name: kubeadm | Check apiserver key
  12. stat:
  13. path: "{{ kube_cert_dir }}/apiserver.key"
  14. register: apiserver_key_before
  15. delegate_to: "{{groups['kube-master']|first}}"
  16. run_once: true
  17. - name: kubeadm | Check if kubeadm has already run
  18. stat:
  19. path: "/var/lib/kubelet/config.yaml"
  20. register: kubeadm_already_run
  21. - name: kubeadm | Delete old admin.conf
  22. file:
  23. path: "{{ kube_config_dir }}/admin.conf"
  24. state: absent
  25. when:
  26. - not kubeadm_already_run.stat.exists
  27. - name: kubeadm | Delete old static pods
  28. file:
  29. path: "{{ kube_config_dir }}/manifests/{{item}}.manifest"
  30. state: absent
  31. with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler", "kube-proxy"]
  32. when:
  33. - old_apiserver_cert.stat.exists
  34. - name: kubeadm | Forcefully delete old static pods
  35. shell: "docker ps -f name=k8s_{{item}} -q | xargs --no-run-if-empty docker rm -f"
  36. with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler"]
  37. when:
  38. - old_apiserver_cert.stat.exists
  39. - name: kubeadm | aggregate all SANs
  40. set_fact:
  41. apiserver_sans: >-
  42. kubernetes
  43. kubernetes.default
  44. kubernetes.default.svc
  45. kubernetes.default.svc.{{ dns_domain }}
  46. {{ kube_apiserver_ip }}
  47. localhost
  48. 127.0.0.1
  49. {{ ' '.join(groups['kube-master']) }}
  50. {%- if loadbalancer_apiserver is defined %}
  51. {{ apiserver_loadbalancer_domain_name }}
  52. {%- endif %}
  53. {% for host in groups['kube-master'] -%}
  54. {%- if hostvars[host]['access_ip'] is defined -%}
  55. {{ hostvars[host]['access_ip'] }}
  56. {%- endif %}
  57. {{ hostvars[host]['ip'] | default(hostvars[host]['ansible_default_ipv4']['address']) }}
  58. {%- endfor %}
  59. {%- if supplementary_addresses_in_ssl_keys is defined -%}
  60. {% for addr in supplementary_addresses_in_ssl_keys %}
  61. {{ addr }}
  62. {%- endfor %}
  63. {%- endif %}
  64. tags: facts
  65. - name: kubeadm | Copy etcd cert dir under k8s cert dir
  66. command: "cp -TR {{ etcd_cert_dir }} {{ kube_config_dir }}/ssl/etcd"
  67. changed_when: false
  68. - name: Create audit-policy directory
  69. file:
  70. path: "{{ audit_policy_file | dirname }}"
  71. state: directory
  72. when: kubernetes_audit|default(false)
  73. - name: Write api audit policy yaml
  74. template:
  75. src: apiserver-audit-policy.yaml.j2
  76. dest: "{{ audit_policy_file }}"
  77. when: kubernetes_audit|default(false)
  78. # Nginx LB(default), If kubeadm_config_api_fqdn is defined, use other LB by kubeadm controlPlaneEndpoint.
  79. - name: set kubeadm_config_api_fqdn define
  80. set_fact:
  81. kubeadm_config_api_fqdn: "{{ apiserver_loadbalancer_domain_name|default('lb-apiserver.kubernetes.local') }}"
  82. when: loadbalancer_apiserver is defined
  83. - name: kubeadm | set kubeadm version
  84. import_tasks: kubeadm-version.yml
  85. - name: kubeadm | Certificate management with kubeadm
  86. import_tasks: kubeadm-certificate.yml
  87. when:
  88. - not upgrade_cluster_setup
  89. - kubeadm_already_run.stat.exists
  90. - name: kubeadm | Initialize first master
  91. command: timeout -k 600s 600s {{ bin_dir }}/kubeadm init --config={{ kube_config_dir }}/kubeadm-config.yaml --ignore-preflight-errors=all
  92. register: kubeadm_init
  93. # Retry is because upload config sometimes fails
  94. retries: 3
  95. when: inventory_hostname == groups['kube-master']|first and not kubeadm_already_run.stat.exists
  96. failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
  97. notify: Master | restart kubelet
  98. - name: slurp kubeadm certs
  99. slurp:
  100. src: "{{ item }}"
  101. with_items:
  102. - "{{ kube_cert_dir }}/apiserver.crt"
  103. - "{{ kube_cert_dir }}/apiserver.key"
  104. - "{{ kube_cert_dir }}/apiserver-kubelet-client.crt"
  105. - "{{ kube_cert_dir }}/apiserver-kubelet-client.key"
  106. - "{{ kube_cert_dir }}/ca.crt"
  107. - "{{ kube_cert_dir }}/ca.key"
  108. - "{{ kube_cert_dir }}/front-proxy-ca.crt"
  109. - "{{ kube_cert_dir }}/front-proxy-ca.key"
  110. - "{{ kube_cert_dir }}/front-proxy-client.crt"
  111. - "{{ kube_cert_dir }}/front-proxy-client.key"
  112. - "{{ kube_cert_dir }}/sa.key"
  113. - "{{ kube_cert_dir }}/sa.pub"
  114. register: kubeadm_certs
  115. delegate_to: "{{ groups['kube-master']|first }}"
  116. run_once: true
  117. - name: kubeadm | write out kubeadm certs
  118. copy:
  119. dest: "{{ item.item }}"
  120. content: "{{ item.content | b64decode }}"
  121. owner: root
  122. group: root
  123. mode: 0600
  124. no_log: true
  125. register: copy_kubeadm_certs
  126. with_items: "{{ kubeadm_certs.results }}"
  127. when: inventory_hostname != groups['kube-master']|first
  128. - name: kubeadm | Kubeconfig management with kubeadm
  129. import_tasks: kubeadm-kubeconfig.yml
  130. when:
  131. - not upgrade_cluster_setup
  132. - kubeadm_already_run.stat.exists
  133. - name: kubeadm | Init other uninitialized masters
  134. command: timeout -k 600s 600s {{ bin_dir }}/kubeadm init --config={{ kube_config_dir }}/kubeadm-config.yaml --ignore-preflight-errors=all
  135. register: kubeadm_init
  136. retries: 10
  137. until: kubeadm_init is succeeded or "field is immutable" in kubeadm_init.stderr
  138. when: inventory_hostname != groups['kube-master']|first and not kubeadm_already_run.stat.exists
  139. failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
  140. notify: Master | restart kubelet
  141. - name: kubeadm | upgrage kubernetes cluster
  142. import_tasks: kubeadm-upgrade.yml
  143. when: upgrade_cluster_setup
  144. - name: kubeadm | Check apiserver key again
  145. stat:
  146. path: "{{ kube_cert_dir }}/apiserver.key"
  147. register: apiserver_key_after
  148. delegate_to: "{{groups['kube-master']|first}}"
  149. run_once: true
  150. - name: kubeadm | Set secret_changed if service account key was updated
  151. command: /bin/true
  152. notify: Master | set secret_changed
  153. when: apiserver_key_before.stat.checksum|default("") != apiserver_key_after.stat.checksum
  154. - name: kubeadm | cleanup old certs if necessary
  155. import_tasks: kubeadm-cleanup-old-certs.yml
  156. when:
  157. - old_apiserver_cert.stat.exists
  158. - name: kubeadm | Remove taint for master with node role
  159. command: "{{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf taint node {{ inventory_hostname }} node-role.kubernetes.io/master:NoSchedule-"
  160. delegate_to: "{{groups['kube-master']|first}}"
  161. when: inventory_hostname in groups['kube-node']
  162. failed_when: false