2017-09-13 18:00:51 +00:00
|
|
|
---
|
2017-09-26 09:38:58 +00:00
|
|
|
- name: kubeadm | Check if old apiserver cert exists on host
|
|
|
|
stat:
|
|
|
|
path: "{{ kube_cert_dir }}/apiserver.pem"
|
|
|
|
register: old_apiserver_cert
|
|
|
|
delegate_to: "{{groups['kube-master']|first}}"
|
|
|
|
run_once: true
|
|
|
|
|
2018-11-21 09:35:40 +00:00
|
|
|
- name: kubeadm | Migrate old certs if necessary
|
|
|
|
import_tasks: kubeadm-migrate-certs.yml
|
|
|
|
when: old_apiserver_cert.stat.exists
|
|
|
|
|
2018-12-28 07:27:27 +00:00
|
|
|
- name: kubeadm | Check apiserver key
|
2017-09-26 09:38:58 +00:00
|
|
|
stat:
|
2018-12-28 07:27:27 +00:00
|
|
|
path: "{{ kube_cert_dir }}/apiserver.key"
|
|
|
|
register: apiserver_key_before
|
2017-09-26 09:38:58 +00:00
|
|
|
delegate_to: "{{groups['kube-master']|first}}"
|
|
|
|
run_once: true
|
|
|
|
|
|
|
|
- name: kubeadm | Check if kubeadm has already run
|
|
|
|
stat:
|
2018-11-07 19:46:11 +00:00
|
|
|
path: "/var/lib/kubelet/config.yaml"
|
|
|
|
register: kubeadm_already_run
|
2017-10-15 09:33:22 +00:00
|
|
|
|
|
|
|
- name: kubeadm | Delete old admin.conf
|
|
|
|
file:
|
2017-09-26 09:38:58 +00:00
|
|
|
path: "{{ kube_config_dir }}/admin.conf"
|
2017-10-15 09:33:22 +00:00
|
|
|
state: absent
|
2018-12-06 10:33:38 +00:00
|
|
|
when:
|
|
|
|
- not kubeadm_already_run.stat.exists
|
2017-09-26 09:38:58 +00:00
|
|
|
|
|
|
|
- name: kubeadm | Delete old static pods
|
|
|
|
file:
|
|
|
|
path: "{{ kube_config_dir }}/manifests/{{item}}.manifest"
|
|
|
|
state: absent
|
|
|
|
with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler", "kube-proxy"]
|
2018-12-06 10:33:38 +00:00
|
|
|
when:
|
|
|
|
- old_apiserver_cert.stat.exists
|
2017-09-26 09:38:58 +00:00
|
|
|
|
|
|
|
- name: kubeadm | Forcefully delete old static pods
|
|
|
|
shell: "docker ps -f name=k8s_{{item}} -q | xargs --no-run-if-empty docker rm -f"
|
|
|
|
with_items: ["kube-apiserver", "kube-controller-manager", "kube-scheduler"]
|
2018-12-06 10:33:38 +00:00
|
|
|
when:
|
|
|
|
- old_apiserver_cert.stat.exists
|
2017-09-26 09:38:58 +00:00
|
|
|
|
2017-09-13 18:00:51 +00:00
|
|
|
- name: kubeadm | aggregate all SANs
|
|
|
|
set_fact:
|
|
|
|
apiserver_sans: >-
|
|
|
|
kubernetes
|
|
|
|
kubernetes.default
|
|
|
|
kubernetes.default.svc
|
|
|
|
kubernetes.default.svc.{{ dns_domain }}
|
|
|
|
{{ kube_apiserver_ip }}
|
|
|
|
localhost
|
|
|
|
127.0.0.1
|
|
|
|
{{ ' '.join(groups['kube-master']) }}
|
2017-11-23 16:15:48 +00:00
|
|
|
{%- if loadbalancer_apiserver is defined %}
|
2017-09-13 18:00:51 +00:00
|
|
|
{{ apiserver_loadbalancer_domain_name }}
|
2019-01-16 13:30:50 +00:00
|
|
|
{% endif %}
|
2018-12-19 10:47:31 +00:00
|
|
|
{% for host in groups['kube-master'] -%}
|
2019-02-02 04:10:34 +00:00
|
|
|
{%- if hostvars[host]['access_ip'] is defined %}
|
2018-12-07 08:11:08 +00:00
|
|
|
{{ hostvars[host]['access_ip'] }}
|
2019-01-16 13:30:50 +00:00
|
|
|
{% endif %}
|
2019-03-05 11:04:39 +00:00
|
|
|
{{ hostvars[host]['ip'] | default(fallback_ips[host]) }}
|
2017-09-13 18:00:51 +00:00
|
|
|
{%- endfor %}
|
2019-03-29 07:26:13 +00:00
|
|
|
{% if supplementary_addresses_in_ssl_keys is defined -%}
|
2019-01-03 08:12:00 +00:00
|
|
|
{% for addr in supplementary_addresses_in_ssl_keys %}
|
2017-10-17 10:06:07 +00:00
|
|
|
{{ addr }}
|
2019-03-29 07:26:13 +00:00
|
|
|
{% endfor %}
|
2017-10-18 11:04:13 +00:00
|
|
|
{%- endif %}
|
2017-10-17 10:06:07 +00:00
|
|
|
tags: facts
|
2017-09-13 18:00:51 +00:00
|
|
|
|
|
|
|
- name: kubeadm | Copy etcd cert dir under k8s cert dir
|
2019-03-20 12:56:57 +00:00
|
|
|
command: "cp -TR {{ etcd_cert_dir }} {{ kube_config_dir }}/ssl/etcd"
|
2017-09-13 18:00:51 +00:00
|
|
|
changed_when: false
|
|
|
|
|
2018-08-15 08:41:13 +00:00
|
|
|
- name: Create audit-policy directory
|
2018-08-16 10:51:09 +00:00
|
|
|
file:
|
|
|
|
path: "{{ audit_policy_file | dirname }}"
|
|
|
|
state: directory
|
2018-08-15 08:41:13 +00:00
|
|
|
when: kubernetes_audit|default(false)
|
|
|
|
|
|
|
|
- name: Write api audit policy yaml
|
|
|
|
template:
|
|
|
|
src: apiserver-audit-policy.yaml.j2
|
2018-08-16 10:51:09 +00:00
|
|
|
dest: "{{ audit_policy_file }}"
|
2018-08-15 08:41:13 +00:00
|
|
|
when: kubernetes_audit|default(false)
|
|
|
|
|
2018-08-18 14:05:35 +00: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 07:06:27 +00:00
|
|
|
- name: kubeadm | set kubeadm version
|
|
|
|
import_tasks: kubeadm-version.yml
|
2017-09-13 18:00:51 +00:00
|
|
|
|
2018-12-28 07:27:27 +00:00
|
|
|
- name: kubeadm | Certificate management with kubeadm
|
|
|
|
import_tasks: kubeadm-certificate.yml
|
|
|
|
when:
|
|
|
|
- not upgrade_cluster_setup
|
|
|
|
- kubeadm_already_run.stat.exists
|
|
|
|
|
2017-09-15 21:28:15 +00:00
|
|
|
- name: kubeadm | Initialize first master
|
2018-12-19 13:16:14 +00:00
|
|
|
command: timeout -k 600s 600s {{ bin_dir }}/kubeadm init --config={{ kube_config_dir }}/kubeadm-config.yaml --ignore-preflight-errors=all
|
2017-09-15 21:28:15 +00:00
|
|
|
register: kubeadm_init
|
2017-09-27 13:47:47 +00:00
|
|
|
# Retry is because upload config sometimes fails
|
2017-09-15 21:28:15 +00:00
|
|
|
retries: 3
|
2018-11-07 19:46:11 +00:00
|
|
|
when: inventory_hostname == groups['kube-master']|first and not kubeadm_already_run.stat.exists
|
2017-09-26 09:38:58 +00:00
|
|
|
failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
|
2019-01-10 00:38:02 +00:00
|
|
|
environment:
|
|
|
|
PATH: "{{ bin_dir }}:{{ ansible_env.PATH }}"
|
2017-09-26 09:38:58 +00:00
|
|
|
notify: Master | restart kubelet
|
|
|
|
|
2017-09-15 21:28:15 +00:00
|
|
|
- name: slurp kubeadm certs
|
|
|
|
slurp:
|
|
|
|
src: "{{ item }}"
|
|
|
|
with_items:
|
|
|
|
- "{{ kube_cert_dir }}/apiserver.crt"
|
|
|
|
- "{{ kube_cert_dir }}/apiserver.key"
|
|
|
|
- "{{ kube_cert_dir }}/apiserver-kubelet-client.crt"
|
|
|
|
- "{{ kube_cert_dir }}/apiserver-kubelet-client.key"
|
|
|
|
- "{{ kube_cert_dir }}/ca.crt"
|
|
|
|
- "{{ kube_cert_dir }}/ca.key"
|
|
|
|
- "{{ kube_cert_dir }}/front-proxy-ca.crt"
|
|
|
|
- "{{ kube_cert_dir }}/front-proxy-ca.key"
|
|
|
|
- "{{ kube_cert_dir }}/front-proxy-client.crt"
|
|
|
|
- "{{ kube_cert_dir }}/front-proxy-client.key"
|
|
|
|
- "{{ kube_cert_dir }}/sa.key"
|
|
|
|
- "{{ kube_cert_dir }}/sa.pub"
|
|
|
|
register: kubeadm_certs
|
|
|
|
delegate_to: "{{ groups['kube-master']|first }}"
|
|
|
|
run_once: true
|
|
|
|
|
2017-09-26 09:38:58 +00:00
|
|
|
- name: kubeadm | write out kubeadm certs
|
2017-09-15 21:28:15 +00:00
|
|
|
copy:
|
|
|
|
dest: "{{ item.item }}"
|
|
|
|
content: "{{ item.content | b64decode }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
2018-05-15 07:54:32 +00:00
|
|
|
mode: 0600
|
2017-09-15 21:28:15 +00:00
|
|
|
no_log: true
|
|
|
|
register: copy_kubeadm_certs
|
|
|
|
with_items: "{{ kubeadm_certs.results }}"
|
|
|
|
when: inventory_hostname != groups['kube-master']|first
|
|
|
|
|
2018-12-28 07:27:27 +00:00
|
|
|
- name: kubeadm | Kubeconfig management with kubeadm
|
|
|
|
import_tasks: kubeadm-kubeconfig.yml
|
|
|
|
when:
|
|
|
|
- not upgrade_cluster_setup
|
|
|
|
- kubeadm_already_run.stat.exists
|
|
|
|
|
2017-09-26 09:38:58 +00:00
|
|
|
- name: kubeadm | Init other uninitialized masters
|
2018-12-19 13:16:14 +00:00
|
|
|
command: timeout -k 600s 600s {{ bin_dir }}/kubeadm init --config={{ kube_config_dir }}/kubeadm-config.yaml --ignore-preflight-errors=all
|
2017-09-13 18:00:51 +00:00
|
|
|
register: kubeadm_init
|
2018-11-15 18:03:23 +00:00
|
|
|
retries: 10
|
|
|
|
until: kubeadm_init is succeeded or "field is immutable" in kubeadm_init.stderr
|
2018-11-07 19:46:11 +00:00
|
|
|
when: inventory_hostname != groups['kube-master']|first and not kubeadm_already_run.stat.exists
|
2017-09-26 09:38:58 +00:00
|
|
|
failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
|
2019-01-10 00:38:02 +00:00
|
|
|
environment:
|
|
|
|
PATH: "{{ bin_dir }}:{{ ansible_env.PATH }}"
|
2017-09-26 09:38:58 +00:00
|
|
|
notify: Master | restart kubelet
|
|
|
|
|
2018-12-25 07:06:27 +00:00
|
|
|
- name: kubeadm | upgrage kubernetes cluster
|
|
|
|
import_tasks: kubeadm-upgrade.yml
|
|
|
|
when: upgrade_cluster_setup
|
2017-09-26 09:38:58 +00:00
|
|
|
|
2018-12-28 07:27:27 +00:00
|
|
|
- name: kubeadm | Check apiserver key again
|
2017-09-26 09:38:58 +00:00
|
|
|
stat:
|
2018-12-28 07:27:27 +00:00
|
|
|
path: "{{ kube_cert_dir }}/apiserver.key"
|
|
|
|
register: apiserver_key_after
|
2017-09-26 09:38:58 +00:00
|
|
|
delegate_to: "{{groups['kube-master']|first}}"
|
|
|
|
run_once: true
|
|
|
|
|
|
|
|
- name: kubeadm | Set secret_changed if service account key was updated
|
|
|
|
command: /bin/true
|
|
|
|
notify: Master | set secret_changed
|
2018-12-28 07:27:27 +00:00
|
|
|
when: apiserver_key_before.stat.checksum|default("") != apiserver_key_after.stat.checksum
|
2017-09-26 09:38:58 +00:00
|
|
|
|
|
|
|
- name: kubeadm | cleanup old certs if necessary
|
2018-01-29 11:37:48 +00:00
|
|
|
import_tasks: kubeadm-cleanup-old-certs.yml
|
2018-12-06 10:33:38 +00:00
|
|
|
when:
|
|
|
|
- old_apiserver_cert.stat.exists
|
2018-10-09 08:40:43 +00:00
|
|
|
|
|
|
|
- name: kubeadm | Remove taint for master with node role
|
2018-11-21 09:35:40 +00:00
|
|
|
command: "{{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf taint node {{ inventory_hostname }} node-role.kubernetes.io/master:NoSchedule-"
|
2018-10-09 08:40:43 +00:00
|
|
|
delegate_to: "{{groups['kube-master']|first}}"
|
|
|
|
when: inventory_hostname in groups['kube-node']
|
2018-10-19 07:38:12 +00:00
|
|
|
failed_when: false
|