Skip to content

Commit

Permalink
优化只读kubectl配置的创建流程 #537
Browse files Browse the repository at this point in the history
  • Loading branch information
gjmzj committed May 1, 2019
1 parent a193907 commit 881a43a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 90 deletions.
6 changes: 3 additions & 3 deletions docs/op/readonly_kubectl.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
## 创建

- 备份下原先 admin 权限的 kubeconfig 文件:`mv ~/.kube ~/.kubeadmin`
- 执行 `ansible-playbook /etc/ansible/roles/deploy/create-read-kubeconfig.yml`,成功后查看~/.kube/config 即为只读权限
- 执行 `ansible-playbook /etc/ansible/01.prepare.yml -t create_kctl_cfg -e KUBECTL_USER=read`,成功后查看~/.kube/config 即为只读权限

## 讲解

对照文件`/etc/ansible/roles/deploy/create-read-kubeconfig.yml`,创建主要包括三个步骤:
对照文件`/etc/ansible/roles/deploy/tasks/main.yml`,创建主要包括三个步骤:

- 创建 group:read rbac 权限
- 创建 read 用户证书和私钥
Expand Down Expand Up @@ -62,7 +62,7 @@ kubeconfig 为与apiserver交互使用的认证配置文件,如脚本步骤需
## 恢复 admin 权限

- 可以恢复之前备份的`~/.kubeadmin`文件:`mv ~/.kube ~/.kuberead && mv ~/.kubeadmin ~/.kube`
- 或者直接执行 `ansible-playbook /etc/ansible/roles/deploy/create-admin-kubeconfig.yml`
- 或者直接执行 `ansible-playbook /etc/ansible/01.prepare.yml -t create_kctl_cfg`

## 参考

Expand Down
33 changes: 0 additions & 33 deletions roles/deploy/create-admin-kubeconfig.yml

This file was deleted.

44 changes: 0 additions & 44 deletions roles/deploy/create-read-kubeconfig.yml

This file was deleted.

4 changes: 4 additions & 0 deletions roles/deploy/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ CLUSTER_NAME: "cluster1"
USER_NAME: "user1"
CONTEXT_NAME: "context1"

# kubectl 权限配置
# 'admin' 表示创建集群管理员(所有)权限的 kubeconfig
# 'read' 表示创建只读权限的 kubeconfig
KUBECTL_USER: "admin"
41 changes: 31 additions & 10 deletions roles/deploy/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- "{{ ca_dir }}"
- "{{ base_dir }}"
- "/etc/kubernetes"
- "/opt/kube/kube-system"

- name: 下载证书工具 CFSSL和 kubectl
copy: src={{ base_dir }}/bin/{{ item }} dest={{ bin_dir }}/{{ item }} mode=0755
Expand Down Expand Up @@ -33,34 +34,54 @@
shell: "cd {{ ca_dir }} && {{ bin_dir }}/cfssl gencert -initca ca-csr.json | {{ bin_dir }}/cfssljson -bare ca"

#----------- 创建kubectl kubeconfig文件: /root/.kube/config
- name: 准备kubectl使用的admin 证书签名请求
template: src=admin-csr.json.j2 dest={{ ca_dir }}/admin-csr.json
- name: 删除原有kubeconfig
file: path=/root/.kube state=absent
tags: create_kctl_cfg

- name: 创建 admin证书与私钥
- name: 下载 group:read rbac 文件
copy: src=read-group-rbac.yaml dest=/opt/kube/kube-system/read-group-rbac.yaml
when: KUBECTL_USER == "read"
tags: create_kctl_cfg

- name: 创建group:read rbac 绑定
shell: "{{ bin_dir }}/kubectl apply -f /opt/kube/kube-system/read-group-rbac.yaml"
when: KUBECTL_USER == "read"
tags: create_kctl_cfg

- name: 准备kubectl使用的{{ KUBECTL_USER }}证书签名请求
template: src={{ KUBECTL_USER }}-csr.json.j2 dest={{ ca_dir }}/{{ KUBECTL_USER }}-csr.json
tags: create_kctl_cfg

- name: 创建{{ KUBECTL_USER }}证书与私钥
shell: "cd {{ ca_dir }} && {{ bin_dir }}/cfssl gencert \
-ca={{ ca_dir }}/ca.pem \
-ca-key={{ ca_dir }}/ca-key.pem \
-config={{ ca_dir }}/ca-config.json \
-profile=kubernetes admin-csr.json | {{ bin_dir }}/cfssljson -bare admin"
# 设置集群参数,指定CA证书和apiserver地址
-profile=kubernetes {{ KUBECTL_USER }}-csr.json | {{ bin_dir }}/cfssljson -bare {{ KUBECTL_USER }}"
tags: create_kctl_cfg

- name: 设置集群参数
shell: "{{ bin_dir }}/kubectl config set-cluster {{ CLUSTER_NAME }} \
--certificate-authority={{ ca_dir }}/ca.pem \
--embed-certs=true \
--server={{ KUBE_APISERVER }}"
# 设置客户端认证参数,指定使用admin证书和私钥
tags: create_kctl_cfg

- name: 设置客户端认证参数
shell: "{{ bin_dir }}/kubectl config set-credentials {{ USER_NAME }} \
--client-certificate={{ ca_dir }}/admin.pem \
--client-certificate={{ ca_dir }}/{{ KUBECTL_USER }}.pem \
--embed-certs=true \
--client-key={{ ca_dir }}/admin-key.pem"
# 设置上下文参数,说明使用cluster集群和用户admin
--client-key={{ ca_dir }}/{{ KUBECTL_USER }}-key.pem"
tags: create_kctl_cfg

- name: 设置上下文参数
shell: "{{ bin_dir }}/kubectl config set-context {{ CONTEXT_NAME }} \
--cluster={{ CLUSTER_NAME }} --user={{ USER_NAME }}"
# 选择默认上下文
tags: create_kctl_cfg

- name: 选择默认上下文
shell: "{{ bin_dir }}/kubectl config use-context {{ CONTEXT_NAME }}"
tags: create_kctl_cfg

#------------创建kube-proxy.kubeconfig配置文件: /root/kube-proxy.kubeconfig
- name: 准备kube-proxy 证书签名请求
Expand Down

0 comments on commit 881a43a

Please sign in to comment.