一 Kubernetes证书

1.1 TLS

Kubernetes系统的各个组件需要使用TLS证书对其通信加密以及授权认证,建议在部署之前先生成相关的TLS证书。

1.2 CA证书创建方式

kubernetes 系统各个组件需要使用TLS证书对通信进行加密,通常可通过以下工具生产自建证书:
  • openssl
  • cfssl
  • easyrsa

1.3 Kubernetes组件证书

部署kubernetes组件建议使用TLS双向认证的,相关组件涉及的主要证书有:
  • etcd证书:etcd集群之间通信加密使用的TLS证书。
  • kube-apiserver证书:配置kube-apiserver组件的证书。
  • kube-controller-manager证书:用于和kube-apiserver通信认证的证书。
  • kube-scheduler证书:用于和kube-apiserver通信认证的证书。
  • kubelet证书【可选,非必需】:用于和kube-apiserver通信认证的证书,如果使用TLS Bootstarp认证方式,将没有必要配置。
  • kube-proxy证书【可选,非必需】:用于和kube-apiserver通信认证的证书,如果使用TLS Bootstarp认证方式,将没有必要配置。

二 openssl生成证书

2.1 openssl创建证书

 [root@master ~]# MASTER_IP=172.24.8.71			#定义MASTER_IP
[root@master ~]# mkdir cert #建议创建独立存储证书的目录
[root@master ~]# cd cert
[root@master cert]# openssl genrsa -out ca.key 2048 #生成一个 2048 bit的ca.key
[root@master cert]# openssl req -x509 -new -nodes -key ca.key -subj "/CN=${MASTER_IP}" -days 10000 -out ca.crt #根据 ca.key 生成一个 ca.crt(使用 -days 设置证书的有效时间)
[root@master cert]# openssl genrsa -out server.key 2048 #生成一个 2048 bit 的 server.key
[root@master cert]# openssl req -new -key server.key -subj "/CN=${MASTER_IP}" -out server.csr #根据 server.key 生成一个 server.csr
[root@master cert]# openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 10000 #根据 ca.key、ca.crt 和 server.csr 生成 server.crt
[root@master cert]# openssl x509 -noout -text -in ./server.crt
 

三 cfssl生成证书

3.1 cfssl创建证书

 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl         #下载cfssl软件
[root@master ~]# chmod u+x /usr/local/bin/cfssl
[root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson #下载json模板
[root@master ~]# chmod u+x /usr/local/bin/cfssljson
[root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
[root@master ~]# chmod u+x /usr/local/bin/cfssl-certinfo
[root@master ~]# mkdir cert
[root@master ~]# cd cert/
[root@master cert]# cfssl print-defaults config > config.json
[root@master cert]# cfssl print-defaults csr > csr.json #创建模版配置json文件
[root@master cert]# cp config.json ca-config.json #复制一份作为CA的配置文件
[root@master cert]# vi ca-config.json
{
"signing": {
"default": {
"expiry": "168h"
},
"profiles": {
"kubernetes": {
"expiry": "8760h",
"usages": [
"signing",
"key encipherment",
"server auth"
"client auth"
]
}
}
}
}
 
字段解释:
config.json:可以定义多个profiles,分别指定不同的过期时间、使用场景等参数;后续在签名证书时使用某个profile;
  • signing: 表示该证书可用于签名其它证书;生成的ca.pem 证书中CA=TRUE;
  • server auth: 表示client 可以用该CA 对server 提供的证书进行校验;
  • client auth: 表示server 可以用该CA 对client 提供的证书进行验证。
 [root@master cert]# cp csr.json ca-csr.json					#复制一份作为CA的配置文件
[root@master cert]# vi ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "Shanghai",
"L": "Shanghai",
"O": "k8s",
"OU": "System"
}
]
}
 
字段解释:
  • CN: Common Name,kube-apiserver 从证书中提取该字段作为请求的用户名(User Name);浏览器使用该字段验证网站是否合法;
  • C:country;
  • ST:state;
  • L:city;
  • O: Organization,kube-apiserver 从证书中提取该字段作为请求用户所属的组(Group);
  • OU:organization unit。
 [root@master cert]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca	#生成CA密钥(ca-key.pem)和证书(ca.pem)
提示:生成证书后,Kubernetes集群需要双向TLS认证,则可将ca-key.pem和ca.pem拷贝到所有要部署的机器的/etc/kubernetes/ssl目录下。

四 easyrsa生成证书

4.1 easyrsa创建证书

 [root@master ~]# mkdir cert
[root@master ~]# curl -LO https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz #下载easyrsa软件
[root@master ~]# tar xzf easy-rsa.tar.gz
[root@master ~]# cd easy-rsa-master/easyrsa3
[root@master easyrsa3]# ./easyrsa init-pki
[root@master easyrsa3]# MASTER_IP=172.24.8.71 #定义MASTER_IP
[root@master easyrsa3]# ./easyrsa --batch "--req-cn=${MASTER_IP}@`date +%s`" build-ca nopass #生成 CA
 
解释:
--batch:设置为自动模式;
--req-cn:设置默认的 CN
 [root@master easyrsa3]# ./easyrsa --subject-alt-name="IP:${MASTER_IP}" build-server-full server nopass	#生成服务器证书和密钥
解释:
build-server-full [文件名]:生成一个键值对,在本地为客户端和服务器签名。
 [root@master easyrsa3]# cp pki/ca.crt pki/issued/server.crt pki/private/server.key /root/cert/		#复制相关证书
提示:生成证书后,Kubernetes集群可通过如下配置使用证书:
  • --client-ca-file=/root/cert/ca.crt
  • --tls-cert-file=/root/cert/server.crt
  • --tls-private-key-file=/root/cert/server.key

五 相关证书及配置项

5.1 API Server 证书

API Server 证书配置为如下两个选项:
  • --tls-cert-file string
File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated after server cert). If HTTPS serving is enabled, and --tls-cert-file and --tls-private-key-file are not provided, a self-signed certificate and key are generated for the public address and saved to the directory specified by --cert-dir.
 
  • --tls-private-key-file string
File containing the default x509 private key matching --tls-cert-file.

5.2 Client CA 证书

  • --client-ca-file string
If set, any request presenting a client certificate signed by one of the authorities in the client-ca-file is authenticated with an identity corresponding to the CommonName of the client certificate.
该配置明确了 Clent 连接 API Server 时,API Server 应当确保其证书源自哪个 CA 签发;如果其证书不是由该 CA 签发,则拒绝请求;事实上,这个 CA 不必与 HTTPS 端点所使用的证书 CA 相同;同时这里的 Client 是一个泛指的,可以是 kubectl,也可能是你自己开发的应用

5.3 请求头证书

API Server 支持多种认证方式的,其中一种就是使用 HTTP 头中的指定字段来进行认证,相关配置如下:
  • --requestheader-allowed-names stringSlice
List of client certificate common names to allow to provide usernames in headers specified by --requestheader-username-headers. If empty, any client certificate validated by the authorities in --requestheader-client-ca-file is allowed.
  • --requestheader-client-ca-file string
Root certificate bundle to use to verify client certificates on incoming requests before trusting usernames in headers specified by --requestheader-username-headers. WARNING: generally do not depend on authorization being already done for incoming requests.

5.4 kubelet证书

对于 Kubelet 组件,API Server 单独提供了证书配置选项,从而指定 API Server 与 Kubelet 通讯所使用的证书以及其签署的 CA。同时这个 CA 可以完全独立与上述其他CA。同时 Kubelet 组件也提供了反向设置的相关选项:
# API Server
  • --kubelet-certificate-authority string
Path to a cert file for the certificate authority.
  • --kubelet-client-certificate string
Path to a client cert file for TLS.
  • --kubelet-client-key string
Path to a client key file for TLS.
 
# Kubelet
  • --client-ca-file string
If set, any request presenting a client certificate signed by one of the authorities in the client-ca-file is authenticated with an identity corresponding to the CommonName of the client certificate.
  • --tls-cert-file string
File containing x509 Certificate used for serving HTTPS (with intermediate certs, if any, concatenated after server cert). If --tls-cert-file and --tls-private-key-file are not provided, a self-signed certificate and key are generated for the public address and saved to the directory passed to --cert-dir.
  • --tls-private-key-file string
File containing x509 private key matching --tls-cert-file.
5.5 Service Account 证书
在 API Server 配置中,对于 Service Account 同样有两个证书配置:
  • --service-account-key-file stringArray
File containing PEM-encoded x509 RSA or ECDSA private or public keys, used to verify ServiceAccount tokens. The specified file can contain multiple keys, and the flag can be specified multiple times with different files. If unspecified, --tls-private-key-file is used. Must be specified when --service-account-signing-key is provided
  • --service-account-signing-key-file string
Path to the file that contains the current private key of the service account token issuer. The issuer will sign issued ID tokens with this private key. (Requires the 'TokenRequest' feature gate.)
这两个配置描述了对 Service Account 进行签名验证时所使用的证书;不过需要注意的是这里并没有明确要求证书 CA,所以这两个证书的 CA 理论上也是可以完全独立的。
Kubernetes相关证书及配置项参考:
https://mritd.me/2018/08/26/kubernetes-certificate-configuration/
提示:以上证书创建示例参考:https://notes.doublemine.me/2018-03-26-Kubernetes%E9%9B%86%E7%BE%A4%E4%B9%8B%E8%B7%AF%E4%B9%8BTLS%E8%AF%81%E4%B9%A6%E9%85%8D%E7%BD%AE.html

附008.Kubernetes TLS证书介绍及创建的更多相关文章

  1. 附024.Kubernetes全系列大总结

    Kubernetes全系列总结如下,后期不定期更新.欢迎基于学习.交流目的的转载和分享,禁止任何商业盗用,同时希望能带上原文出处,尊重ITer的成果,也是尊重知识.若发现任何错误或纰漏,留言反馈或右侧 ...

  2. kubernetes(K8S)创建自签TLS证书

    TLS证书用于进行通信使用,组件需要证书关系如下: 组件 需要使用的证书 etcd ca.pem server.pem server-key.pem flannel ca.pem server.pem ...

  3. 二进制搭建kubernetes多master集群【一、使用TLS证书搭建etcd集群】

    上一篇我们介绍了kubernetes集群架构以及系统参数配置,参考:二进制搭建kubernetes多master集群[开篇.集群环境和功能介绍] 下面本文etcd集群才用三台centos7.5搭建完成 ...

  4. 在阿里云托管kubernetes上利用 cert-manager 自动签发 TLS 证书[无坑版]

    前言 排错的过程是痛苦的也是有趣的. 运维乃至IT,排错能力是拉开人与人之间的重要差距. 本篇会记录我的排错之旅. 由来 现如今我司所有业务都运行在阿里云托管kubernetes环境上,因为前端需要对 ...

  5. 4.第三篇 PKI基础概念、cfssl工具介绍及kubernetes中证书

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247483787&idx=1&sn=08dd3404 ...

  6. 基于TLS证书手动部署kubernetes集群(下)

    一.master节点组件部署 承接上篇文章--基于TLS证书手动部署kubernetes集群(上),我们已经部署好了etcd集群.flannel网络以及每个节点的docker,接下来部署master节 ...

  7. 基于TLS证书手动部署kubernetes集群(上)

    一.简介 Kubernetes是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernetes也叫K8S. K8S是Google内部一个叫Borg的容器集群管理系统衍生 ...

  8. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之自签TLS证书及Etcd集群部署(二)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.服务器设置 1.把每一 ...

  9. Kubernetes 集群部署(1) -- 自签 TLS 证书

    集群功能各模块功能描述: Master节点:主要由四个模块组成,APIServer,schedule, controller-manager, etcd APIServer: APIServer负责对 ...

随机推荐

  1. zabbix 设备(自己的实践)

    1. 下载源代码包 wget http://sourceforge.net/projects/zabbix/files/ 2.  解压 tar -zxvf zabbix-2.2.3.tar.gz 3. ...

  2. OpenGL中GLSL渲染茶壶光照完整程序

    顶点着色器VertexShader.txt: uniform vec3 lightposition;//光源位置 uniform vec3 eyeposition;//相机位置 uniform vec ...

  3. WPF: Creation of Text Labels for 3D Scene

    原文:WPF: Creation of Text Labels for 3D Scene 转载:http://www.codeproject.com/KB/WPF/WPF_Text3D.aspx Do ...

  4. 转:PyQt4学习资料汇总 from coderzh

    一个月前研究了下PyQt4,感觉比较不错.相比wxpython,界面美观了很多,并且将界面设计与代码逻辑很好的分离了开来.关于PyQt4的资料也不少,这里我将我找到的资料汇总一下,以防自己以后忘得一干 ...

  5. JS中常用函数总结

    //LTrim(string):去除左边的空格 function LTrim(str) { var whitespace = new String(" \t\n\r"); var ...

  6. Beginner’s Tutorial: 3D Line and Border Effects in XAML

    This mini-tutorial might be for you if you’re having troubles finding the right line colors to achie ...

  7. Lync 2013和Exchange 2013集成

    定位到下面Powershell 文件夹: C:\Program Files\Microsoft\Exchange Server\V15\Scripts\,运行例如以下命令: .\Configure-E ...

  8. JS加载&解析XML文件,浏览器兼容

    #  JS加载XML,浏览器之间有差异,代码如下 this.createXMLDom = function() { var xmldoc; var xmlFile = "XXXXXXXXX. ...

  9. Ionic2开发环境搭建-VS 2017

    原文:Ionic2开发环境搭建-VS 2017 目前在VS 2017中创建Ionic2版本项目 注:在VS中开发Ionic项目,使用的Ionic(v2.x),Cordova(v6.3.1),Angul ...

  10. 深入理解Amazon Alexa Skill(三)

    本节来讨论Alexa Skill中涉及到的授权问题. Alexa内功能的授权 Alexa会发给skill用户的token,然后skill代码使用这个token来访问Web API访问用户的Alexa内 ...