您好,登录后才能下订单哦!
在实际kubernetes应用场景中,集群内组件之间访问都是通过TLS双向认证安全访问,即https访问,所以在部署时,给kubernetes各个组件创建证书是必要的,这是因为没有https安全访问会导致集群间节点通信访问不安全,非法用户可以通过客户端操作,对集群资源非法操作,引起集群服务异常,甚至集群宕机。例如:非法操作etcd存储的数据,因此集群开启https访问双向认证是必要的。
# 证书及密钥存放路径
SSL_BIN_PATH=/usr/local/cfssl
CA_DIR=/etc/k8s/ssl
# 外网和内网apiserver vip访问地址
VIP_KUBEAPI_OUTSIDE=192.168.20.100
VIP_KUBEAPI_INSIDE=10.10.10.100
# master集群各节点组件监听地址
MASTER1_IP=10.10.10.22
MASTER2_IP=10.10.10.23
MASTER3_IP=10.10.10.24
# etcd集群内各节点监听地址
ETCD1_IP=10.10.10.22
ETCD2_IP=10.10.10.23
ETCD3_IP=10.10.10.24
# kubernetes默认service发地址
CLUSTER_KUBERNETES_SVC_IP=10.254.0.1
证书认证域名
DOMAIN=mo9.com
kube-apiserver组件:
使用 ca.pem、kubernetes.pem、kubernetes-key.pem、etcd公私钥证书、 metric公私钥等证书;
kube-controller-manager组件
使用 ca-key.pem, ca.pem, kuber-controller-manager公私钥等证书;
kube-scheduler组件:
使用ca.pem、kube-scheduler.pem、kube-scheduler-key.pem等证书;
kube-proxy组件:
使用 ca.pem、kube-proxy-key.pem、kube-proxy.pem等证书;
kubelet组件:
使用 ca.pem等证书;
kubectl组件:
使用 ca.pem、admin-key.pem、admin.pem等证书;
etcd组件:
使用 ca.pem、etcd-key.pem、etcd.pem等证书;
flannel组件:
使用ca.pem、flannel.pem、flannel-key.pem等证书;
本安装文档为所有的组件生成证书文件,实际情况下也可以选择部分组件共用某个证书或不选择开启https访问。如:etcd、flannel共用apiserver组件证书或不启用https访问,controller-manager,scheduler也可以基于http访问;
mkdir -p $SSL_BIN_PATH
mkdir -p $CA_DIR
大多数情况下,kubernetes集群都是建立在自己的私有网络里,比如公有云vpc区域,自建数据中心等等,由于k8s所有相关组件都是需要TLS双向认证,合法证书机构大多都是提供ssl单向认证证书,所以必须自建CA根证书,并生成相应的公钥和私钥根证书,可以通过openssl或cfssl创建CA证书,这里以cfssl为例:
mkdir -p $SSL_BIN_PATH/bin > /dev/null 2>&1
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -P $SS_BINL_PATH/bin/
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -P $SSL_BIN__PATH/bin/
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -P $SSL_BIN_PATH/bin/
cd $SSL_BIN_PATH/bin/
mv cfssl_linux-amd64 cfssl && mv cfssljson_linux-amd64 cfssljson
mv cfssl-certinfo_linux-amd64 cfssl-certinfo
chmod +x * && ln -sf $SSL_BIN_PATH/bin/cfssl* /usr/local/bin/
CA 证书是集群所有节点共享的,只需要创建一个 CA 证书,后续创建的所有证书都由它签名。
CA 配置文件用于配置根证书的使用场景 (profile) 和具体参数 (usage,过期时间、服务端认证、客户端认证、加密等),后续在签名其它证书时需要指定特定场景。
cat >$CA_DIR/ca-config.json<<EOF
{
"signing": {
"default": {
"expiry": "175200h"
},
"profiles": {
"kubernetes": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "175200h"
}
}
}
}
EOF
cat >$CA_DIR/ca-csr.json<<EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "ShangHai",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cd $CA_DIR
cfssl gencert --initca=true ca-csr.json | cfssljson --bare ca
cat >$CA_DIR/kubernetes-csr.json << EOF
{
"CN": "kubernetes",
"hosts": [
"127.0.0.1",
"${MASTER1_IP}",
"${MASTER2_IP}",
"${MASTER3_IP}",
"${VIP_KUBEAPI_INSIDE}",
"${VIP_KUBEAPI_OUTSIDE}",
"${CLUSTER_KUBERNETES_SVC_IP}",
"*.${DOMAIN}",
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "SahgnHai",
"O": "k8s",
"OU": "System"
}
]
}
EOF
$ kubectl get svc kubernetes
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 1d
cd $CA_DIR
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
kubernetes-csr.json | cfssljson -bare kubernetes
cat > $CA_DIR/kube-controller-manager-csr.json <<EOF
{
"CN": "system:kube-controller-manager",
"key": {
"algo": "rsa",
"size": 2048
},
"hosts": [
"127.0.0.1",
"${MASTER1_IP}",
"${MASTER2_IP}",
"${MASTER3_IP}"
],
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "ShangHai",
"O": "system:kube-controller-manager",
"OU": "System"
}
]
}
EOF
cd $CA_DIR
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
cat > $CA_DIR/kube-scheduler-csr.json <<EOF
{
"CN": "system:kube-scheduler",
"hosts": [
"127.0.0.1",
"${MASTER1_IP}",
"${MASTER2_IP}",
"${MASTER3_IP}"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "ShangHai",
"O": "system:kube-scheduler",
"OU": "System"
}
]
}
EOF
cd $CA_DIR
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
kube-scheduler-csr.json | cfssljson -bare kube-scheduler
cat > $CA_DIR/kube-proxy-csr.json << EOF
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "SahgnHai",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
kube-proxy-csr.json | cfssljson -bare kube-proxy
cat > $CA_DIR/admin-csr.json << EOF
{
"CN": "admin",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "SahgnHai",
"O": "system:masters",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
admin-csr.json | cfssljson -bare admin
cat > $CA_DIR/etcd-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"127.0.0.1",
"${ETCD1_IP}",
"${ETCD2_IP}",
"${ETCD3_IP}"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "SahgnHai",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
etcd-csr.json | cfssljson -bare etcd
cat > $CA_DIR/flannel-csr.json << EOF
{
"CN": "flanneld",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "ShangHai",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cd $CA_DIR
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
flannel-csr.json | cfssljson -bare flannel
cat > $CA_DIR/proxy-client-csr.json <<EOF
{
"CN": "aggregator",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ShangHai",
"L": "ShangHai",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cd $CA_DIR
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes \
proxy-client-csr.json | cfssljson -bare proxy-client
cd $CA_DIR
rm -rf *csr* && rm -rf *json
chmod 666 *
ansible master_k8s_vgs -m \
synchronize -a "src=${CA_DIR} dest=${CA_DIR}/ mode=push delete=yes rsync_opts=-avz'" -b
ansible master_k8s_vgs -m shell -a "chmod 666 ${CA_DIR}/*" -b
ansible master_k8s_vgs -m shell -a "cd ${CA_DIR} && rm -rf kube-proxy*" -b
ansible worker_k8s_vgs -m copy -a "src=${CA_DIR}/ca.pem dest=${CA_DIR}/" -b
ansible worker_k8s_vgs -m copy -a "src=${CA_DIR}/flannel.pem dest=${CA_DIR}/" -b
ansible worker_k8s_vgs -m copy -a "src=${CA_DIR}/flannel-key.pem dest=${CA_DIR}/" -b
备注:
证书创建内容基本完成,关于tls证书双向认证请自行查阅文档,创建完成证书后,还需要创建各组件认证文件,请参阅下一节:kubernetes集群安装指南:客户端安装及各组件认证文件创建
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。