2021-01-19 17:41:00 +08:00
|
|
|
- name: 下载 kube_master 二进制
|
2017-11-11 19:14:21 +08:00
|
|
|
copy: src={{ base_dir }}/bin/{{ item }} dest={{ bin_dir }}/{{ item }} mode=0755
|
|
|
|
with_items:
|
|
|
|
- kube-apiserver
|
|
|
|
- kube-controller-manager
|
|
|
|
- kube-scheduler
|
2019-06-04 23:46:12 +08:00
|
|
|
- kubectl
|
2018-05-11 11:07:14 +08:00
|
|
|
tags: upgrade_k8s
|
2017-11-11 19:14:21 +08:00
|
|
|
|
2022-10-09 19:24:44 +08:00
|
|
|
- name: 分发controller/scheduler kubeconfig配置文件
|
|
|
|
copy: src={{ cluster_dir }}/{{ item }} dest=/etc/kubernetes/{{ item }}
|
|
|
|
with_items:
|
|
|
|
- kube-controller-manager.kubeconfig
|
|
|
|
- kube-scheduler.kubeconfig
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: force_change_certs
|
2022-10-09 19:24:44 +08:00
|
|
|
|
2021-01-22 23:54:12 +08:00
|
|
|
- name: 注册变量 KUBERNETES_SVC_IP
|
|
|
|
shell: echo {{ SERVICE_CIDR }}|cut -d/ -f1|awk -F. '{print $1"."$2"."$3"."$4+1}'
|
|
|
|
register: KUBERNETES_SVC_IP
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: change_cert, force_change_certs
|
2021-01-22 23:54:12 +08:00
|
|
|
|
|
|
|
- name: 设置变量 CLUSTER_KUBERNETES_SVC_IP
|
|
|
|
set_fact: CLUSTER_KUBERNETES_SVC_IP={{ KUBERNETES_SVC_IP.stdout }}
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: change_cert, force_change_certs
|
2021-01-22 23:54:12 +08:00
|
|
|
|
2017-11-11 19:14:21 +08:00
|
|
|
- name: 创建 kubernetes 证书签名请求
|
2020-12-30 11:25:54 +08:00
|
|
|
template: src=kubernetes-csr.json.j2 dest={{ cluster_dir }}/ssl/kubernetes-csr.json
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: change_cert, force_change_certs
|
2020-12-30 11:25:54 +08:00
|
|
|
connection: local
|
2017-11-11 19:14:21 +08:00
|
|
|
|
|
|
|
- name: 创建 kubernetes 证书和私钥
|
2020-12-30 11:25:54 +08:00
|
|
|
shell: "cd {{ cluster_dir }}/ssl && {{ base_dir }}/bin/cfssl gencert \
|
|
|
|
-ca=ca.pem \
|
|
|
|
-ca-key=ca-key.pem \
|
|
|
|
-config=ca-config.json \
|
|
|
|
-profile=kubernetes kubernetes-csr.json | {{ base_dir }}/bin/cfssljson -bare kubernetes"
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: change_cert, force_change_certs
|
2020-12-30 11:25:54 +08:00
|
|
|
connection: local
|
2017-11-11 19:14:21 +08:00
|
|
|
|
2018-06-17 13:07:57 +08:00
|
|
|
# 创建aggregator proxy相关证书
|
|
|
|
- name: 创建 aggregator proxy证书签名请求
|
2020-12-30 11:25:54 +08:00
|
|
|
template: src=aggregator-proxy-csr.json.j2 dest={{ cluster_dir }}/ssl/aggregator-proxy-csr.json
|
|
|
|
connection: local
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: force_change_certs
|
2018-06-17 13:07:57 +08:00
|
|
|
|
|
|
|
- name: 创建 aggregator-proxy证书和私钥
|
2020-12-30 11:25:54 +08:00
|
|
|
shell: "cd {{ cluster_dir }}/ssl && {{ base_dir }}/bin/cfssl gencert \
|
|
|
|
-ca=ca.pem \
|
|
|
|
-ca-key=ca-key.pem \
|
|
|
|
-config=ca-config.json \
|
|
|
|
-profile=kubernetes aggregator-proxy-csr.json | {{ base_dir }}/bin/cfssljson -bare aggregator-proxy"
|
|
|
|
connection: local
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: force_change_certs
|
2020-12-30 11:25:54 +08:00
|
|
|
|
|
|
|
- name: 分发 kubernetes证书
|
|
|
|
copy: src={{ cluster_dir }}/ssl/{{ item }} dest={{ ca_dir }}/{{ item }}
|
|
|
|
with_items:
|
|
|
|
- ca.pem
|
|
|
|
- ca-key.pem
|
|
|
|
- kubernetes.pem
|
|
|
|
- kubernetes-key.pem
|
|
|
|
- aggregator-proxy.pem
|
|
|
|
- aggregator-proxy-key.pem
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: change_cert, force_change_certs
|
2018-06-17 13:07:57 +08:00
|
|
|
|
2020-01-31 12:37:44 +08:00
|
|
|
- name: 替换 kubeconfig 的 apiserver 地址
|
|
|
|
lineinfile:
|
|
|
|
dest: "{{ item }}"
|
|
|
|
regexp: "^ server"
|
2021-04-02 18:53:46 +08:00
|
|
|
line: " server: https://127.0.0.1:{{ SECURE_PORT }}"
|
2020-01-31 12:37:44 +08:00
|
|
|
with_items:
|
|
|
|
- "/etc/kubernetes/kube-controller-manager.kubeconfig"
|
|
|
|
- "/etc/kubernetes/kube-scheduler.kubeconfig"
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: force_change_certs
|
2021-02-15 09:55:37 +08:00
|
|
|
|
2019-05-28 09:10:44 +08:00
|
|
|
- name: 创建 master 服务的 systemd unit 文件
|
|
|
|
template: src={{ item }}.j2 dest=/etc/systemd/system/{{ item }}
|
|
|
|
with_items:
|
|
|
|
- kube-apiserver.service
|
|
|
|
- kube-controller-manager.service
|
|
|
|
- kube-scheduler.service
|
2019-07-04 14:25:32 +08:00
|
|
|
tags: restart_master, upgrade_k8s
|
2019-05-28 09:10:44 +08:00
|
|
|
|
2018-03-16 18:23:34 +08:00
|
|
|
- name: enable master 服务
|
|
|
|
shell: systemctl enable kube-apiserver kube-controller-manager kube-scheduler
|
2018-04-10 18:33:24 +08:00
|
|
|
ignore_errors: true
|
2017-11-11 19:14:21 +08:00
|
|
|
|
2018-03-16 18:23:34 +08:00
|
|
|
- name: 启动 master 服务
|
|
|
|
shell: "systemctl daemon-reload && systemctl restart kube-apiserver && \
|
|
|
|
systemctl restart kube-controller-manager && systemctl restart kube-scheduler"
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2018-03-14 16:53:21 +08:00
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
# 轮询等待kube-apiserver启动完成
|
|
|
|
- name: 轮询等待kube-apiserver启动
|
2021-05-28 10:11:35 +08:00
|
|
|
shell: "systemctl is-active kube-apiserver.service"
|
2021-01-07 09:30:50 +08:00
|
|
|
register: api_status
|
2021-05-28 10:11:35 +08:00
|
|
|
until: '"active" in api_status.stdout'
|
2021-01-07 09:30:50 +08:00
|
|
|
retries: 10
|
|
|
|
delay: 3
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2021-01-07 09:30:50 +08:00
|
|
|
|
|
|
|
# 轮询等待kube-controller-manager启动完成
|
|
|
|
- name: 轮询等待kube-controller-manager启动
|
2021-05-28 10:11:35 +08:00
|
|
|
shell: "systemctl is-active kube-controller-manager.service"
|
2021-01-07 09:30:50 +08:00
|
|
|
register: cm_status
|
2021-05-28 10:11:35 +08:00
|
|
|
until: '"active" in cm_status.stdout'
|
2021-01-07 09:30:50 +08:00
|
|
|
retries: 8
|
|
|
|
delay: 3
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2021-01-07 09:30:50 +08:00
|
|
|
|
|
|
|
# 轮询等待kube-scheduler启动完成
|
|
|
|
- name: 轮询等待kube-scheduler启动
|
2021-05-28 10:11:35 +08:00
|
|
|
shell: "systemctl is-active kube-scheduler.service"
|
2021-01-07 09:30:50 +08:00
|
|
|
register: sch_status
|
2021-05-28 10:11:35 +08:00
|
|
|
until: '"active" in sch_status.stdout'
|
2021-01-07 09:30:50 +08:00
|
|
|
retries: 8
|
|
|
|
delay: 3
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2021-01-07 09:30:50 +08:00
|
|
|
|
2022-10-09 20:43:19 +08:00
|
|
|
- block:
|
|
|
|
- name: 复制kubectl.kubeconfig
|
|
|
|
shell: 'cd {{ cluster_dir }} && cp -f kubectl.kubeconfig {{ inventory_hostname }}-kubectl.kubeconfig'
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2022-10-09 20:43:19 +08:00
|
|
|
|
|
|
|
- name: 替换 kubeconfig 的 apiserver 地址
|
|
|
|
lineinfile:
|
|
|
|
dest: "{{ cluster_dir }}/{{ inventory_hostname }}-kubectl.kubeconfig"
|
|
|
|
regexp: "^ server"
|
|
|
|
line: " server: https://{{ inventory_hostname }}:{{ SECURE_PORT }}"
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2022-10-09 20:43:19 +08:00
|
|
|
|
|
|
|
- name: 轮询等待master服务启动完成
|
|
|
|
command: "{{ base_dir }}/bin/kubectl --kubeconfig={{ cluster_dir }}/{{ inventory_hostname }}-kubectl.kubeconfig get node"
|
|
|
|
register: result
|
|
|
|
until: result.rc == 0
|
|
|
|
retries: 5
|
|
|
|
delay: 6
|
2022-11-26 10:56:52 +08:00
|
|
|
tags: upgrade_k8s, restart_master, force_change_certs
|
2022-10-09 20:43:19 +08:00
|
|
|
|
|
|
|
- name: 获取user:kubernetes是否已经绑定对应角色
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl get clusterrolebindings|grep kubernetes-crb || echo 'notfound'"
|
|
|
|
register: crb_info
|
|
|
|
run_once: true
|
|
|
|
|
|
|
|
- name: 创建user:kubernetes角色绑定
|
2022-11-26 11:18:02 +08:00
|
|
|
command: "{{ base_dir }}/bin/kubectl create clusterrolebinding kubernetes-crb --clusterrole=system:kubelet-api-admin --user=kubernetes"
|
2022-10-09 20:43:19 +08:00
|
|
|
run_once: true
|
|
|
|
when: "'notfound' in crb_info.stdout"
|
|
|
|
connection: local
|