2017-09-14 02:00:51 +08:00
|
|
|
---
|
2017-09-26 17:38:58 +08:00
|
|
|
- name: kubeadm | Check if old apiserver cert exists on host
|
|
|
|
stat:
|
|
|
|
path: "{{ kube_cert_dir }}/apiserver.pem"
|
|
|
|
register: old_apiserver_cert
|
2019-05-03 05:24:21 +08:00
|
|
|
delegate_to: "{{ groups['kube-master'] | first }}"
|
2017-09-26 17:38:58 +08:00
|
|
|
run_once: true
|
|
|
|
|
2018-11-21 17:35:40 +08:00
|
|
|
- name: kubeadm | Migrate old certs if necessary
|
|
|
|
import_tasks: kubeadm-migrate-certs.yml
|
|
|
|
when: old_apiserver_cert.stat.exists
|
|
|
|
|
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
|
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: kubeadm | Check serviceaccount key
|
2017-09-26 17:38:58 +08:00
|
|
|
stat:
|
2019-04-19 21:01:54 +08:00
|
|
|
path: "{{ kube_cert_dir }}/sa.key"
|
|
|
|
register: sa_key_before
|
2017-09-26 17:38:58 +08:00
|
|
|
run_once: true
|
|
|
|
|
|
|
|
- name: kubeadm | Check if kubeadm has already run
|
|
|
|
stat:
|
2018-11-08 03:46:11 +08:00
|
|
|
path: "/var/lib/kubelet/config.yaml"
|
|
|
|
register: kubeadm_already_run
|
2017-10-15 17:33:22 +08:00
|
|
|
|
|
|
|
- name: kubeadm | Delete old admin.conf
|
|
|
|
file:
|
2017-09-26 17:38:58 +08:00
|
|
|
path: "{{ kube_config_dir }}/admin.conf"
|
2017-10-15 17:33:22 +08:00
|
|
|
state: absent
|
2018-12-06 18:33:38 +08:00
|
|
|
when:
|
|
|
|
- not kubeadm_already_run.stat.exists
|
2017-09-26 17:38:58 +08:00
|
|
|
|
|
|
|
- name: kubeadm | Delete old static pods
|
|
|
|
file:
|
2019-05-03 05:24:21 +08:00
|
|
|
path: "{{ kube_config_dir }}/manifests/{{ item }}.manifest"
|
2017-09-26 17:38:58 +08:00
|
|
|
state: absent
|
|
|
|
with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler", "kube-proxy"]
|
2018-12-06 18:33:38 +08:00
|
|
|
when:
|
|
|
|
- old_apiserver_cert.stat.exists
|
2017-09-26 17:38:58 +08:00
|
|
|
|
|
|
|
- name: kubeadm | Forcefully delete old static pods
|
2019-05-03 05:24:21 +08:00
|
|
|
shell: "docker ps -f name=k8s_{{ item }} -q | xargs --no-run-if-empty docker rm -f"
|
2017-09-26 17:38:58 +08:00
|
|
|
with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler"]
|
2018-12-06 18:33:38 +08:00
|
|
|
when:
|
|
|
|
- old_apiserver_cert.stat.exists
|
2017-09-26 17:38:58 +08:00
|
|
|
|
2017-09-14 02:00:51 +08:00
|
|
|
- name: kubeadm | aggregate all SANs
|
|
|
|
set_fact:
|
2019-07-22 15:48:53 +08:00
|
|
|
apiserver_sans: "{{ (sans_base + groups['kube-master'] + sans_lb + sans_lb_ip + sans_supp + sans_access_ip + sans_ip + sans_address + sans_override + sans_hostname + sans_fqdn) | 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 [] }}"
|
|
|
|
sans_access_ip: "{{ groups['kube-master'] | map('extract', hostvars, 'access_ip') | list | select('defined') | list }}"
|
|
|
|
sans_ip: "{{ groups['kube-master'] | map('extract', hostvars, 'ip') | list | select('defined') | list }}"
|
|
|
|
sans_address: "{{ groups['kube-master'] | 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 [] }}"
|
2019-07-22 15:48:53 +08:00
|
|
|
sans_hostname: "{{ groups['kube-master'] | map('extract', hostvars, ['ansible_hostname']) | list | select('defined') | list }}"
|
|
|
|
sans_fqdn: "{{ groups['kube-master'] | map('extract', hostvars, ['ansible_fqdn']) | list | select('defined') | list }}"
|
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
|
2018-08-15 16:41:13 +08:00
|
|
|
when: kubernetes_audit|default(false)
|
|
|
|
|
|
|
|
- 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 }}"
|
2018-08-15 16:41:13 +08:00
|
|
|
when: kubernetes_audit|default(false)
|
|
|
|
|
2018-08-18 22:05:35 +08:00
|
|
|
# Nginx LB(default), If kubeadm_config_api_fqdn is defined, use other LB by kubeadm controlPlaneEndpoint.
|
|
|
|
- name: set kubeadm_config_api_fqdn define
|
|
|
|
set_fact:
|
|
|
|
kubeadm_config_api_fqdn: "{{ apiserver_loadbalancer_domain_name|default('lb-apiserver.kubernetes.local') }}"
|
|
|
|
when: loadbalancer_apiserver is defined
|
|
|
|
|
2018-12-25 15:06:27 +08:00
|
|
|
- name: kubeadm | set kubeadm version
|
|
|
|
import_tasks: kubeadm-version.yml
|
2017-09-14 02:00:51 +08:00
|
|
|
|
2018-12-28 15:27:27 +08:00
|
|
|
- name: kubeadm | Certificate management with kubeadm
|
|
|
|
import_tasks: kubeadm-certificate.yml
|
|
|
|
when:
|
|
|
|
- not upgrade_cluster_setup
|
|
|
|
- kubeadm_already_run.stat.exists
|
|
|
|
|
2017-09-16 05:28:15 +08:00
|
|
|
- name: kubeadm | Initialize first master
|
2019-04-19 21:01:54 +08:00
|
|
|
command: >-
|
2019-07-09 20:41:59 +08:00
|
|
|
timeout -k 300s 300s
|
2019-04-19 21:01:54 +08:00
|
|
|
{{ bin_dir }}/kubeadm init
|
|
|
|
--config={{ kube_config_dir }}/kubeadm-config.yaml
|
|
|
|
--ignore-preflight-errors=all
|
2019-05-22 17:06:11 +08:00
|
|
|
--skip-phases=addon/coredns
|
2019-04-19 21:01:54 +08:00
|
|
|
{% if kubeadm_version is version('v1.14.0', '>=') %}
|
2019-07-02 16:51:08 +08:00
|
|
|
{% if kubeadm_version is version('v1.15.0', '<') %}
|
2019-04-19 21:01:54 +08:00
|
|
|
--experimental-upload-certs
|
|
|
|
{% if kubeadm_certificate_key is defined %}
|
|
|
|
--certificate-key={{ kubeadm_certificate_key }}
|
|
|
|
{% endif %}
|
2019-07-02 16:51:08 +08:00
|
|
|
{% else %}
|
|
|
|
--upload-certs
|
|
|
|
{% endif %}
|
2019-05-22 17:06:11 +08:00
|
|
|
{% endif %}
|
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
|
2018-11-08 03:46:11 +08:00
|
|
|
when: inventory_hostname == groups['kube-master']|first 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
|
2019-01-10 08:38:02 +08:00
|
|
|
environment:
|
|
|
|
PATH: "{{ bin_dir }}:{{ ansible_env.PATH }}"
|
2017-09-26 17:38:58 +08:00
|
|
|
notify: Master | restart kubelet
|
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: set kubeadm certificate key
|
|
|
|
set_fact:
|
|
|
|
kubeadm_certificate_key: "{{ item | regex_search('--certificate-key ([^ ]+)','\\1') | first }}"
|
2019-04-24 05:37:59 +08:00
|
|
|
with_items: "{{ hostvars[groups['kube-master'][0]]['kubeadm_init'].stdout_lines | default([]) }}"
|
2019-04-19 21:01:54 +08:00
|
|
|
when:
|
|
|
|
- kubeadm_version is version('v1.14.0', '>=')
|
|
|
|
- kubeadm_certificate_key is not defined
|
|
|
|
- item | trim | match('.*--certificate-key .*')
|
|
|
|
|
2019-04-24 05:37:59 +08:00
|
|
|
- name: Create hardcoded kubeadm token for joining nodes with 24h expiration (if defined)
|
|
|
|
shell: >-
|
|
|
|
{{ bin_dir }}/kubeadm --kubeconfig /etc/kubernetes/admin.conf token delete {{ kubeadm_token }} || :;
|
|
|
|
{{ bin_dir }}/kubeadm --kubeconfig /etc/kubernetes/admin.conf token create {{ kubeadm_token }}
|
|
|
|
when:
|
|
|
|
- inventory_hostname == groups['kube-master']|first
|
|
|
|
- kubeadm_token is defined
|
|
|
|
tags:
|
|
|
|
- kubeadm_token
|
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: Create kubeadm token for joining nodes with 24h expiration (default)
|
|
|
|
command: "{{ bin_dir }}/kubeadm --kubeconfig /etc/kubernetes/admin.conf token create"
|
|
|
|
register: temp_token
|
|
|
|
retries: 5
|
|
|
|
delay: 5
|
|
|
|
until: temp_token is succeeded
|
2019-05-03 05:24:21 +08:00
|
|
|
delegate_to: "{{ groups['kube-master'] | first }}"
|
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
|
|
|
|
|
|
|
|
- name: kubeadm | Initialize other masters (experimental control plane)
|
|
|
|
include: kubeadm-secondary-experimental.yml
|
|
|
|
when: kubeadm_control_plane
|
2017-09-26 17:38:58 +08:00
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: kubeadm | Initialize other masters (experimental control plane)
|
|
|
|
include: kubeadm-secondary-legacy.yml
|
|
|
|
when: not kubeadm_control_plane
|
|
|
|
|
|
|
|
- name: kubeadm | upgrade kubernetes cluster
|
2018-12-25 15:06:27 +08:00
|
|
|
import_tasks: kubeadm-upgrade.yml
|
|
|
|
when: upgrade_cluster_setup
|
2017-09-26 17:38:58 +08:00
|
|
|
|
2019-04-19 21:01:54 +08:00
|
|
|
- name: kubeadm | Check serviceaccount key again
|
2017-09-26 17:38:58 +08:00
|
|
|
stat:
|
2019-04-19 21:01:54 +08:00
|
|
|
path: "{{ kube_cert_dir }}/sa.key"
|
|
|
|
register: sa_key_after
|
2017-09-26 17:38:58 +08:00
|
|
|
run_once: true
|
|
|
|
|
|
|
|
- name: kubeadm | Set secret_changed if service account key was updated
|
|
|
|
command: /bin/true
|
|
|
|
notify: Master | set secret_changed
|
2019-04-19 21:01:54 +08:00
|
|
|
when: sa_key_before.stat.checksum|default("") != sa_key_after.stat.checksum
|
2017-09-26 17:38:58 +08:00
|
|
|
|
|
|
|
- name: kubeadm | cleanup old certs if necessary
|
2018-01-29 19:37:48 +08:00
|
|
|
import_tasks: kubeadm-cleanup-old-certs.yml
|
2018-12-06 18:33:38 +08:00
|
|
|
when:
|
|
|
|
- old_apiserver_cert.stat.exists
|
2018-10-09 16:40:43 +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.
|
2018-10-09 16:40:43 +08:00
|
|
|
- name: kubeadm | Remove taint for master with node role
|
2019-04-17 22:58:06 +08:00
|
|
|
command: "{{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf taint node {{ inventory_hostname }} node-role.kubernetes.io/master:NoSchedule-"
|
2019-05-03 05:24:21 +08:00
|
|
|
delegate_to: "{{ groups['kube-master'] | first }}"
|
2018-10-09 16:40:43 +08:00
|
|
|
when: inventory_hostname in groups['kube-node']
|
2018-10-19 15:38:12 +08:00
|
|
|
failed_when: false
|