可能是史上最全的Kubernetes证书解析

2023-11-09

为了避免广告法,题目还是加个可能吧。

想要安全就必须复杂起来,证书是少不了的。在Kubernetes中提供了非常丰富的证书类型,满足各种不同场景的需求,今天我们就来看一看Kubernetes中的证书。

k8s证书分类

在说证书之前,先想想作为集群的入口apiserver需要提供那些服务,与那些组件通信,通信的两方可能需要配置证书。
与apiserver通信的组件大体可以分为以下几类:

  • client(kubectl,restapi等):普通用户与apiserver之间的通信,对各类资源进行操作
  • kubelet,kubeproxy:master与node之间的通信
  • etcd:k8s的存储库
  • webhook:这里指apiserver提供的admission-webhook,在数据持久化前调用webhook
  • aggregation layer:扩展apiserver, 需要将自定义的api注册到k8s中,相比CRD性能更新
  • pod: 在pod中调用apiserver(一般调用为10.254.0.1:433)

居然有这么多种,除了在pod中通过serviceacount认证(当然pod需要认证apiserver的证书),其他几种都需要配置证书。

其他集群内组件与apiserver通信的,kubelet/etcd/kube-proxy对应的也可以配置证书。

apiserver证书

简单列举下apiserver证书相关的启动参数

--cert-dir string                           The directory where the TLS certs are located. If --tls-cert-file and --tls-private-key-file are provided, this flag will be ignored. (default "/var/run/kubernetes")
--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.
--etcd-certfile string                      SSL certification file used to secure etcd communication.
--etcd-keyfile string                       SSL key file used to secure etcd communication.
--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.
--proxy-client-cert-file string             Client certificate used to prove the identity of the aggregator or kube-apiserver when it must call out during a request. This includes proxying requests to a user api-server and calling out to webhook admission plugins. It is expected that this cert includes a signature from the CA in the --requestheader-client-ca-file flag. That CA is published in the 'extension-apiserver-authentication' configmap in the kube-system namespace. Components recieving calls from kube-aggregator should use that CA to perform their half of the mutual TLS verification.
--proxy-client-key-file string              Private key for the client certificate used to prove the identity of the aggregator or kube-apiserver when it must call out during a request. This includes proxying requests to a user api-server and calling out to webhook admission plugins.
--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
--service-account-key-file stringArray      File containing PEM-encoded x509 RSA or ECDSA private or public keys, used to verify ServiceAccount tokens. If unspecified, --tls-private-key-file is used. The specified file can contain multiple keys, and the flag can be specified multiple times with different files.
--ssh-keyfile string                        If non-empty, use secure SSH proxy to the nodes, using this user keyfile
--tls-ca-file string                        If set, this certificate authority will used for secure access from Admission Controllers. This must be a valid PEM-encoded CA bundle. Alternatively, the certificate authority can be appended to the certificate provided by --tls-cert-file.
--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 /var/run/kubernetes.
--tls-private-key-file string               File containing the default x509 private key matching --tls-cert-file.
--tls-sni-cert-key namedCertKey             A pair of x509 certificate and private key file paths, optionally suffixed with a list of domain patterns which are fully qualified domain names, possibly with prefixed wildcard segments. If no domain patterns are provided, the names of the certificate are extracted. Non-wildcard matches trump over wildcard matches, explicit domain patterns trump over extracted names. For multiple key/certificate pairs, use the --tls-sni-cert-key multiple times. Examples: "example.crt,example.key" or "foo.crt,foo.key:*.foo.com,foo.com". (default [])
--oidc-ca-file string                       If set, the OpenID server's certificate will be verified by one of the authorities in the oidc-ca-file, otherwise the host's root CA set will be used.
--tls-sni-cert-key namedCertKey             A pair of x509 certificate and private key file paths, optionally suffixed with a list of domain patterns which are fully qualified domain names, possibly with prefixed wildcard segments. If no domain patterns are provided, the names of the certificate are extracted. Non-wildcard matches trump over wildcard matches, explicit domain patterns trump over extracted names. For multiple key/certificate pairs, use the --tls-sni-cert-key multiple times. Examples: "example.crt,example.key" or "foo.crt,foo.key:*.foo.com,foo.com". (default [])

不要害怕,咱们一个个看。

tls证书

首先,apiserver本身是一个http服务器,需要tls证书

--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.
其他client验证apiserver时可以通过签署这两个证书的CA,我们称为`tls-ca`

client证书

apiserver提供了tls证书,同样也需要验证client的配置,但是client太多了(kubectl,各种restapi调用的), 这些client需要统一用一个CA签发,我们称为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.

需要注意的是,在apiserver认证中,通过CNO来识别用户,开启RBAC的用户要配置CNO做一些授权:

  • CN:Common Name,kube-apiserver 从证书中提取作为请求的用户名 (User Name);浏览器使用该字段验证网站是否合法;
  • O:Organization,kube-apiserver 从证书中提取该字段作为请求用户所属的组 (Group)

如kube-proxy的证书申请, User为system:kube-proxy, Group为k8s

{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

requestheader证书

apiserver可以使用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.
--requestheader-extra-headers-prefix strings        
    List of request header prefixes to inspect. X-Remote-Extra- is suggested.
--requestheader-group-headers strings               
    List of request headers to inspect for groups. X-Remote-Group is suggested.
--requestheader-username-headers strings            
    List of request headers to inspect for usernames. X-Remote-User is common.

收到请求时,apiserver会首先认证requsetheader-ca,验证成功并且CNrequestheader-allowed-names(默认全部需求)中,然后通过Http header中的X-Remote-User, X-Remote-Group去得到用户;如果匹配不成功回去验证client-ca

如上,requestheader证书与client-ca不能是同一个。

proxy证书

k8s提供了丰富的扩展机制,CRD与[API Aggregation][https://kubernetes.io/zh/docs/tasks/access-kubernetes-api/configure-aggregation-layer/]。
对于API Aggregation(例如metrics-server提供了metrics.k8s.io api), apiserver接受到请求后经过一系列验证过滤,会将请求转发到扩展API,这里apisever作为代理服务器,需要配置配置证书。

--proxy-client-cert-file string             
    Client certificate used to prove the identity of the aggregator or kube-apiserver when it must call out during a request. This includes proxying requests to a user api-server and calling out to webhook admission plugins. It is expected that this cert includes a signature from the CA in the --requestheader-client-ca-file flag. That CA is published in the 'extension-apiserver-authentication' configmap in the kube-system namespace. Components recieving calls from kube-aggregator should use that CA to perform their half of the mutual TLS verification.
--proxy-client-key-file string              
    Private key for the client certificate used to prove the identity of the aggregator or kube-apiserver when it must call out during a request. This includes proxying requests to a user api-server and calling out to webhook admission plugins.

需要注意的是对证书需要通过requestheader-ca签发,扩展api会通过requestheader证书去验证,具体流程后面会写一篇,下图为官方提供的流程
aggregation-api

kubelet证书

对于kubelet,apiserver单独提供了证书配置选项,同时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.

kubelet也是即作为server也作为client, 需要提供tls证书和client-ca, 我们称这个CA为kubelet-ca, 可以是单独的CA。

etcd证书

这个也不用多说,用来连接etcd,由etcd-ca签发

--etcd-certfile string                      SSL certification file used to secure etcd communication.
--etcd-keyfile string                       SSL key file used to secure etcd communication.

serviceaccount证书

在k8s中,通过JWT认证serviecaccount,同样有两个证书配置:

# apiserver
--service-account-key-file stringArray # 用于验证sa
    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.)

# controller-manager
–service-account-private-key-file #用于签署sa

这两个配置描述了对serviceaccount进行签名验证时所使用的证书;可以是单独的生成,我们称为sa-key

其他证书

其他还有oidc证书,用于OpenID认证;ssh证书,用来连接node,目前以及废弃。

etcd与kubelet证书上面已经提过了,需要双方都配置。

k8s中也支持证书申请,用户可以创建CertificateSigningRequest来申请证书,需要在controller-manager配置下面的证书,用于签发证书称为sing-ca,多用于webhook的证书配置。

--cluster-signing-cert-file string          Filename containing a PEM-encoded X509 CA certificate used to issue cluster-scoped certificates (default "/etc/kubernetes/ca/ca.pem")
--cluster-signing-key-file string           Filename containing a PEM-encoded RSA or ECDSA private key used to sign cluster-scoped certificates (default "/etc/kubernetes/ca/ca.key")

总结

k8s提供了强大的功能,需要考虑到各个场景的安全问题,上面我们梳理了遍目前常用的证书

  • tls-ca
  • client-ca
  • requestheader-ca
  • proxy-ca
  • kubelet-ca
  • etcd-ca
  • sa-key
  • sign-ca

上面除了proxy-ca必须使用requestheader-ca签发,其他所有的都可以是单独的CA,可以根据安全性评估是使用一个CA还是多个CA,我们建议下面的CA尽量是独立的

  • client-ca
  • requestheader-ca
  • etcd-ca
  • kubelet-ca

终于理完了,可以起床啦。


更多文章见 Qinng’s Blog

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

可能是史上最全的Kubernetes证书解析 的相关文章

随机推荐

  • Vue3.0中引用子组件类型声明报错问题

    Vue3 0中引用子组件类型声明报错问题 报错原因 1 找不到组件模块或者找不到对应的类型声明 2 Typescript 只能理解 ts 文件 无法理解 vue 文件 解决方案 1 在项目根目录或 src 文件夹下创建一个后缀为 d ts
  • 第一个djiango项目(包含搭建环境)

    1 安装django框架 pip install django 2 创建项目命令 django admin startproject 项目名 django admin version 如果您看到Django版本号的输出 则表示安装成功 3
  • 数据分析(二) - Excel按一个单元格内的分隔符进行分行

    文章目录 场景 一 python 二 excel word 场景 办公室老师给了我一张Excel表 记录了每位同学的获奖情况 学号 姓名 奖项 加分 101 小明 ICPC世界冠军 国奖 优秀班干部 15 0 102 小亮 一作论文 数学建
  • vm manager failed to contact configuration server

    当用virt manager命令启动VM 管理工具是报错 vm manager failed to contact configuration server 如下办法解决了我的问题 读取dbus uuid dbus uuidgen get
  • 花费7元训练自己的GPT 2模型

    在上一篇博客中 我介绍了用Tensorflow来重现GPT 1的模型和训练的过程 这次我打算用Pytorch来重现GPT 2的模型并从头进行训练 GPT 2的模型相比GPT 1的改进并不多 主要在以下方面 1 GPT 2把layer nor
  • Gensim 中 word2vec 模型的恢复训练:载入存储模型并继续训练

    Gensim 中 word2vec 模型的恢复训练 本文为系列文章之一 前面的几篇请点击链接 NLP 利器 gensim 库基本特性介绍和安装方式 NLP 利器 Gensim 库的使用之 Word2Vec 模型案例演示 NLP 利器 Gen
  • 数据挖掘概述

    目录 1 数据挖掘概述 2 数据挖掘常用库 3 模型介绍 3 1 分类 3 2 聚类 3 3 回归 3 4 关联 3 5 模型集成 4 模型评估 ROC 曲线 5 模型应用 1 数据挖掘概述 数据挖掘 寻找数据中隐含的知识并用于产生商业价值
  • 无基础学c语言的打卡日记总论

    背景知识 笨人浙江考生 选课是政史地 目前在读大一 知道自己的专业学c并且还学数学分析和高等代数 一开始不以为意 学校用的教材是谭浩强老师的c语言程序设计 推荐的 小白友好 上课之前有很认真的自习课本 第一章好像是一个总论 里面有一些思想以
  • 在NPU上的切片操作x=x[:,::-1,:,:]不生效的分析解决

    1 系统环境 硬件环境 Ascend GPU CPU Ascend GPU MindSpore版本 1 9 0 执行模式 PyNative Graph 不限 Python版本 3 7 5 操作系统平台 Linux 2 报错信息 2 1 问题
  • winform下mapxtreme2008 v7.0 生成release版提示找不到dll问题

    在winform下基于mapxtreme2008 v7 0 生成了一个地图软件 用debug方式运行无误 但改为release版时提示缺少一大堆dll 如 无法从C Program Files x86 Common Files MapInf
  • 本地网站域名与联网冲突吐槽篇

    提示 前面是吐槽360使用bug 以及网站开发者使用弊端 解决冲突主要方法在后面 前言是解决电脑无法保存修改的hosts文件真相以及解决棒法 处理不行的话 只能一棒打死安全软件 前言 电脑里安装了360之类的安全软件 安全类软件为了安全 往
  • 时序预测

    时序预测 MATLAB实现时间序列回归之评估模型残差及统计分布 目录 时序预测 MATLAB实现时间序列回归之评估模型残差及统计分布 基本介绍 程序设计 异方差性 统计分布 学习总结 参考资料 致谢 基本介绍 残差分析的基本目的是检查 CL
  • 偷懒的一天-------Day83

    今天实在是学不进去 从公司里工作着也是浑浑噩噩的 虽然不是我媳妇生孩子 但这也是我们这个大家庭里的第一个孩子 我的亲大侄子啊 当然还可能是侄女 还在想名字 都想了好多了 还是有些激动有些紧张啊 偷懒一天 来码上几个字 草草写上至少我也知道我
  • Opencv的基础操作

    一 图像填充 首先定义图像显示函数 def cv show name img cv2 imshow name img cv2 waitKey 0 cv2 destroyAllWindows 图像读取 img cat cv2 imread c
  • 一定能让你理解的素数筛法——埃氏筛法和欧式筛法

    先上代码 埃氏筛法 include
  • 卸载Docker方法

    卸载步骤 在安装Autoware库的时候安装了Docker 发现电脑硬盘容量被占用不少 现在想卸载一下docker 查找了很多资料 最终使用以下方法完整卸载 1 在配置autoware的时候其实安装的docker ce 所以需要执行 sud
  • Nginx基本使用

    一 Nginx作用 1 HTTP服务器 2 反向代理 3 负载均衡 4 正向代理 5 虚拟主机 等 二 Nginx安装 1 下载nginx最新稳定版 windows nginx 1 14 0 2 解压 备注 如上图 配置关键配置文件后 双击
  • 基于tensorflow实现手写数字识别

    实验目的 了解机器学习的相关知识 实现基于tensorflow的手写数字识别 实验环境 ubuntu16 04 或 windows python 3 默认安装版本 tensorflow 2 0 版本以上 或其他深度学习框架 实验内容 实现基
  • 内网穿透-Natapp实现免费远程桌面

    很多人都在使用Teamviewer访问远程桌面 但收费很贵 而且经常有一些安全漏洞 下面教大家一个免费安全的远程桌面方法 打开Natapp主页 https natapp cn 点击 立即下载 按钮 下载操作系统匹配的客户端 下载之后 解压至
  • 可能是史上最全的Kubernetes证书解析

    为了避免广告法 题目还是加个可能吧 想要安全就必须复杂起来 证书是少不了的 在Kubernetes中提供了非常丰富的证书类型 满足各种不同场景的需求 今天我们就来看一看Kubernetes中的证书 k8s证书分类 在说证书之前 先想想作为集