2017-11-11 19:14:21 +08:00
|
|
|
|
- name: prepare some dirs
|
|
|
|
|
file: name={{ item }} state=directory
|
|
|
|
|
with_items:
|
2019-05-27 21:28:36 +08:00
|
|
|
|
- "{{ base_dir }}/.cluster/ssl"
|
|
|
|
|
- "{{ base_dir }}/.cluster/yaml"
|
2017-11-11 19:14:21 +08:00
|
|
|
|
|
2019-05-27 20:02:02 +08:00
|
|
|
|
- name: 本地设置 bin 目录权限
|
|
|
|
|
file: path={{ base_dir }}/bin state=directory mode=0755 recurse=yes
|
2018-03-16 18:23:34 +08:00
|
|
|
|
|
2018-03-24 09:25:20 +08:00
|
|
|
|
# 注册变量p,根据p的stat信息判断是否已经生成过ca证书,如果没有,下一步生成证书
|
|
|
|
|
# 如果已经有ca证书,为了保证整个安装的幂等性,跳过证书生成的步骤
|
|
|
|
|
- name: 读取ca证书stat信息
|
2019-05-27 21:28:36 +08:00
|
|
|
|
stat: path="{{ base_dir }}/.cluster/ssl/ca.pem"
|
2018-03-24 09:25:20 +08:00
|
|
|
|
register: p
|
2017-11-11 19:14:21 +08:00
|
|
|
|
|
2019-05-27 20:02:02 +08:00
|
|
|
|
- name: 准备CA配置文件和签名请求
|
2019-05-27 21:28:36 +08:00
|
|
|
|
template: src={{ item }}.j2 dest={{ base_dir }}/.cluster/ssl/{{ item }}
|
2019-05-27 20:02:02 +08:00
|
|
|
|
with_items:
|
|
|
|
|
- "ca-config.json"
|
|
|
|
|
- "ca-csr.json"
|
2018-03-24 09:25:20 +08:00
|
|
|
|
when: p.stat.isreg is not defined
|
2017-11-11 19:14:21 +08:00
|
|
|
|
|
|
|
|
|
- name: 生成 CA 证书和私钥
|
2018-03-24 09:25:20 +08:00
|
|
|
|
when: p.stat.isreg is not defined
|
2019-05-27 21:28:36 +08:00
|
|
|
|
shell: "cd {{ base_dir }}/.cluster/ssl && \
|
2019-05-27 20:02:02 +08:00
|
|
|
|
{{ base_dir }}/bin/cfssl gencert -initca ca-csr.json | {{ base_dir }}/bin/cfssljson -bare ca"
|
2017-11-11 19:14:21 +08:00
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
#----------- 创建kubectl kubeconfig文件: /root/.kube/config
|
2019-05-27 20:02:02 +08:00
|
|
|
|
- block:
|
|
|
|
|
- name: 删除原有kubeconfig
|
|
|
|
|
file: path=/root/.kube state=absent
|
|
|
|
|
|
|
|
|
|
- name: 下载 group:read rbac 文件
|
2019-05-27 21:28:36 +08:00
|
|
|
|
copy: src=read-group-rbac.yaml dest={{ base_dir }}/.cluster/yaml/read-group-rbac.yaml
|
2019-05-27 20:02:02 +08:00
|
|
|
|
when: USER_NAME == "read"
|
|
|
|
|
|
|
|
|
|
- name: 创建group:read rbac 绑定
|
2019-05-27 21:28:36 +08:00
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl apply -f {{ base_dir }}/.cluster/yaml/read-group-rbac.yaml"
|
2019-05-27 20:02:02 +08:00
|
|
|
|
when: USER_NAME == "read"
|
|
|
|
|
|
|
|
|
|
- name: 准备kubectl使用的{{ USER_NAME }}证书签名请求
|
2019-05-27 21:28:36 +08:00
|
|
|
|
template: src={{ USER_NAME }}-csr.json.j2 dest={{ base_dir }}/.cluster/ssl/{{ USER_NAME }}-csr.json
|
2019-05-27 20:02:02 +08:00
|
|
|
|
|
|
|
|
|
- name: 创建{{ USER_NAME }}证书与私钥
|
2019-05-27 21:28:36 +08:00
|
|
|
|
shell: "cd {{ base_dir }}/.cluster/ssl && {{ base_dir }}/bin/cfssl gencert \
|
2019-05-27 20:02:02 +08:00
|
|
|
|
-ca=ca.pem \
|
|
|
|
|
-ca-key=ca-key.pem \
|
|
|
|
|
-config=ca-config.json \
|
|
|
|
|
-profile=kubernetes {{ USER_NAME }}-csr.json | {{ base_dir }}/bin/cfssljson -bare {{ USER_NAME }}"
|
|
|
|
|
|
|
|
|
|
- name: 设置集群参数
|
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config set-cluster {{ CLUSTER_NAME }} \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--certificate-authority={{ base_dir }}/.cluster/ssl/ca.pem \
|
2019-05-27 20:02:02 +08:00
|
|
|
|
--embed-certs=true \
|
|
|
|
|
--server={{ KUBE_APISERVER }}"
|
|
|
|
|
|
|
|
|
|
- name: 设置客户端认证参数
|
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config set-credentials {{ USER_NAME }} \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--client-certificate={{ base_dir }}/.cluster/ssl/{{ USER_NAME }}.pem \
|
2019-05-27 20:02:02 +08:00
|
|
|
|
--embed-certs=true \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--client-key={{ base_dir }}/.cluster/ssl/{{ USER_NAME }}-key.pem"
|
2019-05-27 20:02:02 +08:00
|
|
|
|
|
|
|
|
|
- name: 设置上下文参数
|
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config set-context {{ CONTEXT_NAME }} \
|
|
|
|
|
--cluster={{ CLUSTER_NAME }} --user={{ USER_NAME }}"
|
|
|
|
|
|
|
|
|
|
- name: 选择默认上下文
|
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config use-context {{ CONTEXT_NAME }}"
|
2019-05-01 17:20:20 +08:00
|
|
|
|
tags: create_kctl_cfg
|
2018-03-16 18:23:34 +08:00
|
|
|
|
|
2019-05-27 21:28:36 +08:00
|
|
|
|
#------------创建kube-proxy配置文件: kube-proxy.kubeconfig
|
2018-03-16 18:23:34 +08:00
|
|
|
|
- name: 准备kube-proxy 证书签名请求
|
2019-05-27 21:28:36 +08:00
|
|
|
|
template: src=kube-proxy-csr.json.j2 dest={{ base_dir }}/.cluster/ssl/kube-proxy-csr.json
|
2018-03-16 18:23:34 +08:00
|
|
|
|
|
|
|
|
|
- name: 创建 kube-proxy证书与私钥
|
2019-05-27 21:28:36 +08:00
|
|
|
|
shell: "cd {{ base_dir }}/.cluster/ssl && {{ base_dir }}/bin/cfssl gencert \
|
2019-05-27 20:02:02 +08:00
|
|
|
|
-ca=ca.pem \
|
|
|
|
|
-ca-key=ca-key.pem \
|
|
|
|
|
-config=ca-config.json \
|
|
|
|
|
-profile=kubernetes kube-proxy-csr.json | {{ base_dir }}/bin/cfssljson -bare kube-proxy"
|
2018-03-16 18:23:34 +08:00
|
|
|
|
|
|
|
|
|
- name: 设置集群参数
|
2019-05-27 20:02:02 +08:00
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config set-cluster kubernetes \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--certificate-authority={{ base_dir }}/.cluster/ssl/ca.pem \
|
2018-03-16 18:23:34 +08:00
|
|
|
|
--embed-certs=true \
|
|
|
|
|
--server={{ KUBE_APISERVER }} \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--kubeconfig={{ base_dir }}/.cluster/kube-proxy.kubeconfig"
|
2018-03-16 18:23:34 +08:00
|
|
|
|
- name: 设置客户端认证参数
|
2019-05-27 20:02:02 +08:00
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config set-credentials kube-proxy \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--client-certificate={{ base_dir }}/.cluster/ssl/kube-proxy.pem \
|
|
|
|
|
--client-key={{ base_dir }}/.cluster/ssl/kube-proxy-key.pem \
|
2018-03-16 18:23:34 +08:00
|
|
|
|
--embed-certs=true \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--kubeconfig={{ base_dir }}/.cluster/kube-proxy.kubeconfig"
|
2018-03-16 18:23:34 +08:00
|
|
|
|
- name: 设置上下文参数
|
2019-05-27 20:02:02 +08:00
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config set-context default \
|
2018-03-16 18:23:34 +08:00
|
|
|
|
--cluster=kubernetes \
|
|
|
|
|
--user=kube-proxy \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--kubeconfig={{ base_dir }}/.cluster/kube-proxy.kubeconfig"
|
2018-03-16 18:23:34 +08:00
|
|
|
|
- name: 选择默认上下文
|
2019-05-27 20:02:02 +08:00
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl config use-context default \
|
2019-05-27 21:28:36 +08:00
|
|
|
|
--kubeconfig={{ base_dir }}/.cluster/kube-proxy.kubeconfig"
|
2019-05-27 20:02:02 +08:00
|
|
|
|
|
|
|
|
|
- name: 本地创建 easzctl 工具的软连接
|
|
|
|
|
file: src={{ base_dir }}/tools/easzctl dest=/usr/bin/easzctl state=link
|
|
|
|
|
|
|
|
|
|
- name: 本地写入环境变量$PATH
|
|
|
|
|
lineinfile:
|
|
|
|
|
dest: ~/.bashrc
|
|
|
|
|
state: present
|
|
|
|
|
regexp: 'kubeasz'
|
|
|
|
|
line: 'export PATH={{ base_dir }}/bin/:$PATH # generated by kubeasz'
|
|
|
|
|
ignore_errors: true
|
2019-05-28 23:46:22 +08:00
|
|
|
|
|
|
|
|
|
- name: ansible 控制端添加 kubectl 自动补全
|
|
|
|
|
lineinfile:
|
|
|
|
|
dest: ~/.bashrc
|
|
|
|
|
state: present
|
|
|
|
|
regexp: 'kubectl completion'
|
|
|
|
|
line: 'source <(kubectl completion bash)'
|
|
|
|
|
ignore_errors: true
|