2017-09-14 02:00:51 +08:00
|
|
|
---
|
2019-04-25 00:40:01 +08:00
|
|
|
- name: Install OIDC certificate
|
|
|
|
copy:
|
|
|
|
content: "{{ kube_oidc_ca_cert | b64decode }}"
|
|
|
|
dest: "{{ kube_oidc_ca_file }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0644"
|
|
|
|
when:
|
|
|
|
- kube_oidc_auth
|
|
|
|
- kube_oidc_ca_cert is defined
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Check if kubeadm has already run
|
2017-09-26 17:38:58 +08:00
|
|
|
stat:
|
2018-11-08 03:46:11 +08:00
|
|
|
path: "/var/lib/kubelet/config.yaml"
|
2021-02-10 21:36:59 +08:00
|
|
|
get_attributes: no
|
|
|
|
get_checksum: no
|
|
|
|
get_mime: no
|
2018-11-08 03:46:11 +08:00
|
|
|
register: kubeadm_already_run
|
2017-10-15 17:33:22 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Backup kubeadm certs / kubeconfig
|
2021-03-04 07:08:22 +08:00
|
|
|
import_tasks: kubeadm-backup.yml
|
|
|
|
when:
|
|
|
|
- kubeadm_already_run.stat.exists
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | aggregate all SANs
|
2017-09-14 02:00:51 +08:00
|
|
|
set_fact:
|
2022-07-20 04:11:28 +08:00
|
|
|
apiserver_sans: "{{ (sans_base + groups['kube_control_plane'] + sans_lb + sans_lb_ip + sans_supp + sans_access_ip + sans_ip + sans_address + sans_override + sans_hostname + sans_fqdn + sans_kube_vip_address) | unique }}"
|
2019-04-08 23:10:35 +08:00
|
|
|
vars:
|
|
|
|
sans_base:
|
|
|
|
- "kubernetes"
|
|
|
|
- "kubernetes.default"
|
|
|
|
- "kubernetes.default.svc"
|
|
|
|
- "kubernetes.default.svc.{{ dns_domain }}"
|
|
|
|
- "{{ kube_apiserver_ip }}"
|
|
|
|
- "localhost"
|
|
|
|
- "127.0.0.1"
|
|
|
|
sans_lb: "{{ [apiserver_loadbalancer_domain_name] if apiserver_loadbalancer_domain_name is defined else [] }}"
|
2019-05-16 16:23:42 +08:00
|
|
|
sans_lb_ip: "{{ [loadbalancer_apiserver.address] if loadbalancer_apiserver is defined and loadbalancer_apiserver.address is defined else [] }}"
|
2019-04-08 23:10:35 +08:00
|
|
|
sans_supp: "{{ supplementary_addresses_in_ssl_keys if supplementary_addresses_in_ssl_keys is defined else [] }}"
|
2021-03-24 08:26:05 +08:00
|
|
|
sans_access_ip: "{{ groups['kube_control_plane'] | map('extract', hostvars, 'access_ip') | list | select('defined') | list }}"
|
|
|
|
sans_ip: "{{ groups['kube_control_plane'] | map('extract', hostvars, 'ip') | list | select('defined') | list }}"
|
|
|
|
sans_address: "{{ groups['kube_control_plane'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list | select('defined') | list }}"
|
2019-06-25 23:00:37 +08:00
|
|
|
sans_override: "{{ [kube_override_hostname] if kube_override_hostname else [] }}"
|
2021-03-24 08:26:05 +08:00
|
|
|
sans_hostname: "{{ groups['kube_control_plane'] | map('extract', hostvars, ['ansible_hostname']) | list | select('defined') | list }}"
|
|
|
|
sans_fqdn: "{{ groups['kube_control_plane'] | map('extract', hostvars, ['ansible_fqdn']) | list | select('defined') | list }}"
|
2022-07-20 04:11:28 +08:00
|
|
|
sans_kube_vip_address: "{{ [kube_vip_address] if kube_vip_address is defined and kube_vip_address else [] }}"
|
2017-10-17 18:06:07 +08:00
|
|
|
tags: facts
|
2017-09-14 02:00:51 +08:00
|
|
|
|
2018-08-15 16:41:13 +08:00
|
|
|
- name: Create audit-policy directory
|
2018-08-16 18:51:09 +08:00
|
|
|
file:
|
|
|
|
path: "{{ audit_policy_file | dirname }}"
|
|
|
|
state: directory
|
2021-07-12 15:00:47 +08:00
|
|
|
mode: 0640
|
2023-07-05 11:36:54 +08:00
|
|
|
when: kubernetes_audit | default(false) or kubernetes_audit_webhook | default(false)
|
2018-08-15 16:41:13 +08:00
|
|
|
|
|
|
|
- name: Write api audit policy yaml
|
|
|
|
template:
|
|
|
|
src: apiserver-audit-policy.yaml.j2
|
2018-08-16 18:51:09 +08:00
|
|
|
dest: "{{ audit_policy_file }}"
|
2021-07-12 15:00:47 +08:00
|
|
|
mode: 0640
|
2023-07-05 11:36:54 +08:00
|
|
|
when: kubernetes_audit | default(false) or kubernetes_audit_webhook | default(false)
|
2020-07-20 16:32:54 +08:00
|
|
|
|
|
|
|
- name: Write api audit webhook config yaml
|
|
|
|
template:
|
|
|
|
src: apiserver-audit-webhook-config.yaml.j2
|
|
|
|
dest: "{{ audit_webhook_config_file }}"
|
2021-07-12 15:00:47 +08:00
|
|
|
mode: 0640
|
2023-07-05 11:36:54 +08:00
|
|
|
when: kubernetes_audit_webhook | default(false)
|
2018-08-15 16:41:13 +08:00
|
|
|
|
2018-08-18 22:05:35 +08:00
|
|
|
# Nginx LB(default), If kubeadm_config_api_fqdn is defined, use other LB by kubeadm controlPlaneEndpoint.
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Set kubeadm_config_api_fqdn define
|
2018-08-18 22:05:35 +08:00
|
|
|
set_fact:
|
2023-07-05 11:36:54 +08:00
|
|
|
kubeadm_config_api_fqdn: "{{ apiserver_loadbalancer_domain_name | default('lb-apiserver.kubernetes.local') }}"
|
2018-08-18 22:05:35 +08:00
|
|
|
when: loadbalancer_apiserver is defined
|
|
|
|
|
2022-06-07 03:25:57 +08:00
|
|
|
- name: Set kubeadm api version to v1beta3
|
2021-03-04 07:45:00 +08:00
|
|
|
set_fact:
|
2022-06-07 03:25:57 +08:00
|
|
|
kubeadmConfig_api_version: v1beta3
|
2021-03-04 07:45:00 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Create kubeadm config
|
2021-03-04 07:45:00 +08:00
|
|
|
template:
|
|
|
|
src: "kubeadm-config.{{ kubeadmConfig_api_version }}.yaml.j2"
|
|
|
|
dest: "{{ kube_config_dir }}/kubeadm-config.yaml"
|
|
|
|
mode: 0640
|
2017-09-14 02:00:51 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Create directory to store admission control configurations
|
2022-05-03 02:03:15 +08:00
|
|
|
file:
|
|
|
|
path: "{{ kube_config_dir }}/admission-controls"
|
|
|
|
state: directory
|
|
|
|
mode: 0640
|
|
|
|
when: kube_apiserver_admission_control_config_file
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Push admission control config file
|
2022-05-03 02:03:15 +08:00
|
|
|
template:
|
2022-06-07 03:25:57 +08:00
|
|
|
src: "admission-controls.yaml.j2"
|
2022-05-03 02:03:15 +08:00
|
|
|
dest: "{{ kube_config_dir }}/admission-controls/admission-controls.yaml"
|
|
|
|
mode: 0640
|
|
|
|
when: kube_apiserver_admission_control_config_file
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Push admission control config files
|
2022-05-03 02:03:15 +08:00
|
|
|
template:
|
2023-07-05 11:36:54 +08:00
|
|
|
src: "{{ item | lower }}.yaml.j2"
|
|
|
|
dest: "{{ kube_config_dir }}/admission-controls/{{ item | lower }}.yaml"
|
2022-05-03 02:03:15 +08:00
|
|
|
mode: 0640
|
|
|
|
when:
|
|
|
|
- kube_apiserver_admission_control_config_file
|
|
|
|
- item in kube_apiserver_admission_plugins_needs_configuration
|
2022-10-26 15:28:37 +08:00
|
|
|
loop: "{{ kube_apiserver_enable_admission_plugins }}"
|
2022-05-03 02:03:15 +08:00
|
|
|
|
2023-12-12 21:51:26 +08:00
|
|
|
- name: Kubeadm | Configure default cluster podnodeslector
|
|
|
|
template:
|
|
|
|
src: "podnodeselector.yaml.j2"
|
|
|
|
dest: "{{ kube_config_dir }}/admission-controls/podnodeselector.yaml"
|
|
|
|
mode: 0640
|
|
|
|
when:
|
|
|
|
- kube_apiserver_admission_plugins_podnodeselector_default_node_selector is defined
|
|
|
|
- kube_apiserver_admission_plugins_podnodeselector_default_node_selector | length > 0
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Check apiserver.crt SANs
|
2023-08-10 15:57:27 +08:00
|
|
|
vars:
|
2023-10-17 15:45:11 +08:00
|
|
|
apiserver_ips: "{{ apiserver_sans | map('ansible.utils.ipaddr') | reject('equalto', False) | list }}"
|
2023-08-10 15:57:27 +08:00
|
|
|
apiserver_hosts: "{{ apiserver_sans | difference(apiserver_ips) }}"
|
|
|
|
when:
|
|
|
|
- kubeadm_already_run.stat.exists
|
|
|
|
- not kube_external_ca_mode
|
2023-03-27 21:36:30 +08:00
|
|
|
block:
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Check apiserver.crt SAN IPs
|
2023-03-27 21:36:30 +08:00
|
|
|
command:
|
|
|
|
cmd: "openssl x509 -noout -in {{ kube_cert_dir }}/apiserver.crt -checkip {{ item }}"
|
|
|
|
loop: "{{ apiserver_ips }}"
|
|
|
|
register: apiserver_sans_ip_check
|
|
|
|
changed_when: apiserver_sans_ip_check.stdout is not search('does match certificate')
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Check apiserver.crt SAN hosts
|
2023-03-27 21:36:30 +08:00
|
|
|
command:
|
|
|
|
cmd: "openssl x509 -noout -in {{ kube_cert_dir }}/apiserver.crt -checkhost {{ item }}"
|
|
|
|
loop: "{{ apiserver_hosts }}"
|
|
|
|
register: apiserver_sans_host_check
|
|
|
|
changed_when: apiserver_sans_host_check.stdout is not search('does match certificate')
|
2020-04-08 16:27:43 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | regenerate apiserver cert 1/2
|
2020-04-08 16:27:43 +08:00
|
|
|
file:
|
|
|
|
state: absent
|
|
|
|
path: "{{ kube_cert_dir }}/{{ item }}"
|
|
|
|
with_items:
|
|
|
|
- apiserver.crt
|
|
|
|
- apiserver.key
|
|
|
|
when:
|
|
|
|
- kubeadm_already_run.stat.exists
|
2023-03-27 21:36:30 +08:00
|
|
|
- apiserver_sans_ip_check.changed or apiserver_sans_host_check.changed
|
2022-04-12 20:47:23 +08:00
|
|
|
- not kube_external_ca_mode
|
2020-04-08 16:27:43 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | regenerate apiserver cert 2/2
|
2020-04-08 16:27:43 +08:00
|
|
|
command: >-
|
|
|
|
{{ bin_dir }}/kubeadm
|
|
|
|
init phase certs apiserver
|
|
|
|
--config={{ kube_config_dir }}/kubeadm-config.yaml
|
|
|
|
when:
|
|
|
|
- kubeadm_already_run.stat.exists
|
2023-03-27 21:36:30 +08:00
|
|
|
- apiserver_sans_ip_check.changed or apiserver_sans_host_check.changed
|
2022-04-12 20:47:23 +08:00
|
|
|
- not kube_external_ca_mode
|
2020-04-08 16:27:43 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Create directory to store kubeadm patches
|
2022-10-06 15:39:52 +08:00
|
|
|
file:
|
|
|
|
path: "{{ kubeadm_patches.dest_dir }}"
|
|
|
|
state: directory
|
|
|
|
mode: 0640
|
|
|
|
when: kubeadm_patches is defined and kubeadm_patches.enabled
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Copy kubeadm patches from inventory files
|
2022-10-06 15:39:52 +08:00
|
|
|
copy:
|
|
|
|
src: "{{ kubeadm_patches.source_dir }}/"
|
|
|
|
dest: "{{ kubeadm_patches.dest_dir }}"
|
|
|
|
owner: "root"
|
|
|
|
mode: 0644
|
|
|
|
when: kubeadm_patches is defined and kubeadm_patches.enabled
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Initialize first master
|
2019-04-19 21:01:54 +08:00
|
|
|
command: >-
|
2022-12-27 16:53:28 +08:00
|
|
|
timeout -k {{ kubeadm_init_timeout }} {{ kubeadm_init_timeout }}
|
2019-04-19 21:01:54 +08:00
|
|
|
{{ bin_dir }}/kubeadm init
|
|
|
|
--config={{ kube_config_dir }}/kubeadm-config.yaml
|
|
|
|
--ignore-preflight-errors=all
|
2020-09-17 19:30:45 +08:00
|
|
|
--skip-phases={{ kubeadm_init_phases_skip | join(',') }}
|
2022-04-12 20:47:23 +08:00
|
|
|
{{ kube_external_ca_mode | ternary('', '--upload-certs') }}
|
2017-09-16 05:28:15 +08:00
|
|
|
register: kubeadm_init
|
2017-09-27 21:47:47 +08:00
|
|
|
# Retry is because upload config sometimes fails
|
2017-09-16 05:28:15 +08:00
|
|
|
retries: 3
|
2019-04-09 15:21:04 +08:00
|
|
|
until: kubeadm_init is succeeded or "field is immutable" in kubeadm_init.stderr
|
2021-12-06 16:16:32 +08:00
|
|
|
when: inventory_hostname == first_kube_control_plane and not kubeadm_already_run.stat.exists
|
2017-09-26 17:38:58 +08:00
|
|
|
failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
|
2021-02-24 01:44:02 +08:00
|
|
|
environment:
|
|
|
|
PATH: "{{ bin_dir }}:{{ ansible_env.PATH }}"
|
2017-09-26 17:38:58 +08:00
|
|
|
notify: Master | restart kubelet
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Set kubeadm certificate key
|
2019-04-19 21:01:54 +08:00
|
|
|
set_fact:
|
2023-07-05 11:36:54 +08:00
|
|
|
kubeadm_certificate_key: "{{ item | regex_search('--certificate-key ([^ ]+)', '\\1') | first }}"
|
2021-03-24 08:26:05 +08:00
|
|
|
with_items: "{{ hostvars[groups['kube_control_plane'][0]]['kubeadm_init'].stdout_lines | default([]) }}"
|
2019-04-19 21:01:54 +08:00
|
|
|
when:
|
|
|
|
- kubeadm_certificate_key is not defined
|
2019-12-05 23:24:32 +08:00
|
|
|
- (item | trim) is match('.*--certificate-key.*')
|
2019-04-19 21:01:54 +08:00
|
|
|
|
2019-04-24 05:37:59 +08:00
|
|
|
- name: Create hardcoded kubeadm token for joining nodes with 24h expiration (if defined)
|
|
|
|
shell: >-
|
2021-09-24 01:19:13 +08:00
|
|
|
{{ bin_dir }}/kubeadm --kubeconfig {{ kube_config_dir }}/admin.conf token delete {{ kubeadm_token }} || :;
|
|
|
|
{{ bin_dir }}/kubeadm --kubeconfig {{ kube_config_dir }}/admin.conf token create {{ kubeadm_token }}
|
2020-06-16 15:24:03 +08:00
|
|
|
changed_when: false
|
2019-04-24 05:37:59 +08:00
|
|
|
when:
|
2021-12-06 16:16:32 +08:00
|
|
|
- inventory_hostname == first_kube_control_plane
|
2019-04-24 05:37:59 +08:00
|
|
|
- kubeadm_token is defined
|
2019-08-06 15:59:53 +08:00
|
|
|
- kubeadm_refresh_token
|
2019-04-24 05:37:59 +08:00
|
|
|
tags:
|
|
|
|
- kubeadm_token
|
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: Create kubeadm token for joining nodes with 24h expiration (default)
|
2021-09-24 01:19:13 +08:00
|
|
|
command: "{{ bin_dir }}/kubeadm --kubeconfig {{ kube_config_dir }}/admin.conf token create"
|
2020-06-16 15:24:03 +08:00
|
|
|
changed_when: false
|
2019-04-19 21:01:54 +08:00
|
|
|
register: temp_token
|
|
|
|
retries: 5
|
|
|
|
delay: 5
|
|
|
|
until: temp_token is succeeded
|
2021-12-06 16:16:32 +08:00
|
|
|
delegate_to: "{{ first_kube_control_plane }}"
|
2019-04-19 21:01:54 +08:00
|
|
|
when: kubeadm_token is not defined
|
|
|
|
tags:
|
|
|
|
- kubeadm_token
|
2018-12-28 15:27:27 +08:00
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: Set kubeadm_token
|
|
|
|
set_fact:
|
|
|
|
kubeadm_token: "{{ temp_token.stdout }}"
|
|
|
|
when: temp_token.stdout is defined
|
|
|
|
tags:
|
|
|
|
- kubeadm_token
|
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Join other masters
|
2020-09-17 19:34:45 +08:00
|
|
|
include_tasks: kubeadm-secondary.yml
|
2019-04-19 21:01:54 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | upgrade kubernetes cluster
|
2019-11-14 18:05:34 +08:00
|
|
|
include_tasks: kubeadm-upgrade.yml
|
|
|
|
when:
|
|
|
|
- upgrade_cluster_setup
|
|
|
|
- kubeadm_already_run.stat.exists
|
2017-09-26 17:38:58 +08:00
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
# FIXME(mattymo): from docs: If you don't want to taint your control-plane node, set this field to an empty slice, i.e. `taints: {}` in the YAML file.
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Kubeadm | Remove taint for master with node role
|
2022-01-05 18:26:32 +08:00
|
|
|
command: "{{ kubectl }} taint node {{ inventory_hostname }} {{ item }}"
|
2021-12-06 16:16:32 +08:00
|
|
|
delegate_to: "{{ first_kube_control_plane }}"
|
2021-03-03 21:35:20 +08:00
|
|
|
with_items:
|
|
|
|
- "node-role.kubernetes.io/control-plane:NoSchedule-"
|
2021-04-29 20:20:50 +08:00
|
|
|
when: inventory_hostname in groups['kube_node']
|
2018-10-19 15:38:12 +08:00
|
|
|
failed_when: false
|