2018-10-12 13:30:19 +08:00
|
|
|
|
# 01-创建证书和环境配置
|
2017-11-30 18:27:53 +08:00
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
本步骤[01.prepare.yml](../../01.prepare.yml)主要完成:
|
2018-03-18 18:25:38 +08:00
|
|
|
|
|
2019-06-09 10:58:01 +08:00
|
|
|
|
- [chrony role](../guide/chrony.md): 集群节点时间同步[可选]
|
2018-09-17 22:20:52 +08:00
|
|
|
|
- deploy role: 创建CA证书、kubeconfig、kube-proxy.kubeconfig
|
|
|
|
|
- prepare role: 分发CA证书、kubectl客户端安装、环境配置
|
2018-03-18 18:25:38 +08:00
|
|
|
|
|
|
|
|
|
## deploy 角色
|
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
请在另外窗口打开[roles/deploy/tasks/main.yml](../../roles/deploy/tasks/main.yml) 文件,对照看以下讲解内容。
|
2017-11-30 18:27:53 +08:00
|
|
|
|
|
2019-06-09 10:58:01 +08:00
|
|
|
|
### 创建 CA 证书和
|
|
|
|
|
|
2017-11-30 18:27:53 +08:00
|
|
|
|
``` bash
|
2018-03-18 18:25:38 +08:00
|
|
|
|
roles/deploy/
|
2019-06-09 10:58:01 +08:00
|
|
|
|
├── defaults
|
|
|
|
|
│ └── main.yml # 配置文件:证书有效期,kubeconfig 相关配置
|
|
|
|
|
├── files
|
|
|
|
|
│ └── read-group-rbac.yaml # 只读用户的 rbac 权限配置
|
2017-11-30 18:27:53 +08:00
|
|
|
|
├── tasks
|
2019-06-09 10:58:01 +08:00
|
|
|
|
│ └── main.yml # 主任务脚本
|
2017-11-30 18:27:53 +08:00
|
|
|
|
└── templates
|
2019-06-09 10:58:01 +08:00
|
|
|
|
├── admin-csr.json.j2 # kubectl客户端使用的admin证书请求模板
|
2018-03-18 18:25:38 +08:00
|
|
|
|
├── ca-config.json.j2 # ca 配置文件模板
|
|
|
|
|
├── ca-csr.json.j2 # ca 证书签名请求模板
|
2019-06-09 10:58:01 +08:00
|
|
|
|
├── kube-proxy-csr.json.j2 # kube-proxy使用的证书请求模板
|
|
|
|
|
└── read-csr.json.j2 # kubectl客户端使用的只读证书请求模板
|
2017-11-30 18:27:53 +08:00
|
|
|
|
```
|
2019-06-09 10:58:01 +08:00
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
kubernetes 系统各组件需要使用 TLS 证书对通信进行加密,使用 CloudFlare 的 PKI 工具集生成自签名的 CA 证书,用来签名后续创建的其它 TLS 证书。[参考阅读](https://coreos.com/os/docs/latest/generate-self-signed-certificates.html)
|
2017-12-04 16:36:08 +08:00
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
根据认证对象可以将证书分成三类:服务器证书`server cert`,客户端证书`client cert`,对等证书`peer cert`(表示既是`server cert`又是`client cert`),在kubernetes 集群中需要的证书种类如下:
|
2017-12-04 16:36:08 +08:00
|
|
|
|
|
2019-06-09 10:58:01 +08:00
|
|
|
|
+ `etcd` 节点需要标识自己服务的`server cert`,也需要`client cert`与`etcd`集群其他节点交互,当然可以分别指定2个证书,为方便这里使用一个对等证书
|
2018-03-18 18:25:38 +08:00
|
|
|
|
+ `master` 节点需要标识 apiserver服务的`server cert`,也需要`client cert`连接`etcd`集群,这里也使用一个对等证书
|
|
|
|
|
+ `kubectl` `calico` `kube-proxy` 只需要`client cert`,因此证书请求中 `hosts` 字段可以为空
|
2019-06-09 10:58:01 +08:00
|
|
|
|
+ `kubelet` 需要标识自己服务的`server cert`,也需要`client cert`请求`apiserver`,也使用一个对等证书
|
2017-11-30 22:44:45 +08:00
|
|
|
|
|
2019-06-10 22:47:20 +08:00
|
|
|
|
整个集群要使用统一的CA 证书,只需要在ansible控制端创建,然后分发给其他节点;为了保证安装的幂等性,如果已经存在CA 证书,就跳过创建CA 步骤
|
2017-11-30 22:44:45 +08:00
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
#### 创建 CA 配置文件 [ca-config.json.j2](../../roles/deploy/templates/ca-config.json.j2)
|
2017-11-30 18:27:53 +08:00
|
|
|
|
``` bash
|
|
|
|
|
{
|
|
|
|
|
"signing": {
|
|
|
|
|
"default": {
|
|
|
|
|
"expiry": "87600h"
|
|
|
|
|
},
|
|
|
|
|
"profiles": {
|
|
|
|
|
"kubernetes": {
|
|
|
|
|
"usages": [
|
|
|
|
|
"signing",
|
|
|
|
|
"key encipherment",
|
|
|
|
|
"server auth",
|
|
|
|
|
"client auth"
|
|
|
|
|
],
|
|
|
|
|
"expiry": "87600h"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
+ `signing`:表示该证书可用于签名其它证书;生成的 ca.pem 证书中 `CA=TRUE`;
|
2018-03-18 18:25:38 +08:00
|
|
|
|
+ `server auth`:表示可以用该 CA 对 server 提供的证书进行验证;
|
|
|
|
|
+ `client auth`:表示可以用该 CA 对 client 提供的证书进行验证;
|
2018-08-18 17:11:38 +08:00
|
|
|
|
+ `profile kubernetes` 包含了`server auth`和`client auth`,所以可以签发三种不同类型证书;
|
2017-11-30 21:11:31 +08:00
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
#### 创建 CA 证书签名请求 [ca-csr.json.j2](../../roles/deploy/templates/ca-csr.json.j2)
|
2017-11-30 18:27:53 +08:00
|
|
|
|
``` bash
|
|
|
|
|
{
|
|
|
|
|
"CN": "kubernetes",
|
|
|
|
|
"key": {
|
|
|
|
|
"algo": "rsa",
|
|
|
|
|
"size": 2048
|
|
|
|
|
},
|
|
|
|
|
"names": [
|
|
|
|
|
{
|
|
|
|
|
"C": "CN",
|
|
|
|
|
"ST": "HangZhou",
|
|
|
|
|
"L": "XS",
|
|
|
|
|
"O": "k8s",
|
|
|
|
|
"OU": "System"
|
|
|
|
|
}
|
2018-12-25 13:16:55 +08:00
|
|
|
|
],
|
|
|
|
|
"ca": {
|
|
|
|
|
"expiry": "876000h"
|
|
|
|
|
}
|
2017-11-30 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2017-11-30 21:11:31 +08:00
|
|
|
|
|
2017-11-30 18:27:53 +08:00
|
|
|
|
#### 生成CA 证书和私钥
|
|
|
|
|
``` bash
|
2017-11-30 22:23:19 +08:00
|
|
|
|
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
|
2017-11-30 18:27:53 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
### 生成 kubeconfig 配置文件
|
|
|
|
|
|
|
|
|
|
kubectl使用~/.kube/config 配置文件与kube-apiserver进行交互,且拥有管理 K8S集群的完全权限,
|
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
准备kubectl使用的admin 证书签名请求 [admin-csr.json.j2](../../roles/deploy/templates/admin-csr.json.j2)
|
2018-03-18 18:25:38 +08:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
{
|
|
|
|
|
"CN": "admin",
|
|
|
|
|
"hosts": [],
|
|
|
|
|
"key": {
|
|
|
|
|
"algo": "rsa",
|
|
|
|
|
"size": 2048
|
|
|
|
|
},
|
|
|
|
|
"names": [
|
|
|
|
|
{
|
|
|
|
|
"C": "CN",
|
|
|
|
|
"ST": "HangZhou",
|
|
|
|
|
"L": "XS",
|
|
|
|
|
"O": "system:masters",
|
|
|
|
|
"OU": "System"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
+ kubectl 使用客户端证书可以不指定hosts 字段
|
2018-12-25 13:16:55 +08:00
|
|
|
|
+ 证书请求中 `O` 指定该证书的 Group 为 `system:masters`,而 `RBAC` 预定义的 `ClusterRoleBinding` 将 Group `system:masters` 与 ClusterRole `cluster-admin` 绑定,这就赋予了kubectl**所有集群权限**
|
2018-03-18 18:25:38 +08:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ kubectl describe clusterrolebinding cluster-admin
|
|
|
|
|
Name: cluster-admin
|
|
|
|
|
Labels: kubernetes.io/bootstrapping=rbac-defaults
|
|
|
|
|
Annotations: rbac.authorization.kubernetes.io/autoupdate=true
|
|
|
|
|
Role:
|
|
|
|
|
Kind: ClusterRole
|
|
|
|
|
Name: cluster-admin
|
|
|
|
|
Subjects:
|
|
|
|
|
Kind Name Namespace
|
|
|
|
|
---- ---- ---------
|
|
|
|
|
Group system:masters
|
|
|
|
|
```
|
|
|
|
|
|
2019-06-09 10:58:01 +08:00
|
|
|
|
#### 生成 admin 用户证书
|
2018-12-25 13:16:55 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 生成 ~/.kube/config 配置文件
|
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
使用`kubectl config` 生成kubeconfig 自动保存到 ~/.kube/config,生成后 `cat ~/.kube/config`可以验证配置文件包含 kube-apiserver 地址、证书、用户名等信息。
|
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
kubectl config set-cluster kubernetes --certificate-authority=ca.pem --embed-certs=true --server=127.0.0.1:8443
|
|
|
|
|
kubectl config set-credentials admin --client-certificate=admin.pem --embed-certs=true --client-key=admin-key.pem
|
|
|
|
|
kubectl config set-context kubernetes --cluster=kubernetes --user=admin
|
|
|
|
|
kubectl config use-context kubernetes
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
### 生成 kube-proxy.kubeconfig 配置文件
|
|
|
|
|
|
|
|
|
|
创建 kube-proxy 证书请求
|
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
{
|
|
|
|
|
"CN": "system:kube-proxy",
|
|
|
|
|
"hosts": [],
|
|
|
|
|
"key": {
|
|
|
|
|
"algo": "rsa",
|
|
|
|
|
"size": 2048
|
|
|
|
|
},
|
|
|
|
|
"names": [
|
|
|
|
|
{
|
|
|
|
|
"C": "CN",
|
|
|
|
|
"ST": "HangZhou",
|
|
|
|
|
"L": "XS",
|
|
|
|
|
"O": "k8s",
|
|
|
|
|
"OU": "System"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
+ kube-proxy 使用客户端证书可以不指定hosts 字段
|
|
|
|
|
+ CN 指定该证书的 User 为 system:kube-proxy,预定义的 ClusterRoleBinding system:node-proxier 将User system:kube-proxy 与 Role system:node-proxier 绑定,授予了调用 kube-apiserver Proxy 相关 API 的权限;
|
2017-12-11 09:52:20 +08:00
|
|
|
|
|
|
|
|
|
``` bash
|
2018-03-18 18:25:38 +08:00
|
|
|
|
$ kubectl describe clusterrolebinding system:node-proxier
|
|
|
|
|
Name: system:node-proxier
|
|
|
|
|
Labels: kubernetes.io/bootstrapping=rbac-defaults
|
|
|
|
|
Annotations: rbac.authorization.kubernetes.io/autoupdate=true
|
|
|
|
|
Role:
|
|
|
|
|
Kind: ClusterRole
|
|
|
|
|
Name: system:node-proxier
|
|
|
|
|
Subjects:
|
|
|
|
|
Kind Name Namespace
|
|
|
|
|
---- ---- ---------
|
|
|
|
|
User system:kube-proxy
|
2017-12-11 09:52:20 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
#### 生成 system:kube-proxy 用户证书
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 生成 kube-proxy.kubeconfig
|
|
|
|
|
|
|
|
|
|
使用`kubectl config` 生成kubeconfig 自动保存到 kube-proxy.kubeconfig
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
kubectl config set-cluster kubernetes --certificate-authority=ca.pem --embed-certs=true --server=127.0.0.1:8443 --kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
kubectl config set-credentials kube-proxy --client-certificate=kube-proxy.pem --embed-certs=true --client-key=kube-proxy-key.pem --kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
kubectl config set-context default --cluster=kubernetes --user=kube-proxy --kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-18 18:25:38 +08:00
|
|
|
|
## prepare 角色
|
|
|
|
|
|
2018-12-25 13:16:55 +08:00
|
|
|
|
请在另外窗口打开[roles/prepare/tasks/main.yml](../../roles/prepare/tasks/main.yml) 文件,比较简单直观
|
2017-12-06 21:45:07 +08:00
|
|
|
|
|
2019-06-09 10:58:01 +08:00
|
|
|
|
1. 首先设置基础操作系统软件和系统参数,请阅读脚本中的注释内容
|
2017-12-06 21:45:07 +08:00
|
|
|
|
1. 首先创建一些基础文件目录
|
2019-06-09 10:58:01 +08:00
|
|
|
|
1. 把证书工具 CFSSL 下发到指定节点,并下发kubeconfig配置文件
|
2017-12-06 21:45:07 +08:00
|
|
|
|
1. 把CA 证书相关下发到指定节点的 {{ ca_dir }} 目录
|
|
|
|
|
|
2017-12-18 17:35:51 +08:00
|
|
|
|
|
2018-10-12 13:30:19 +08:00
|
|
|
|
[后一篇](02-install_etcd.md)
|