附008.Kubernetes TLS證書介紹及創建

来源:https://www.cnblogs.com/itzgr/archive/2019/07/02/11120079.html
-Advertisement-
Play Games

一 Kubernetes證書 1.1 TLS Kubernetes系統的各個組件需要使用TLS證書對其通信加密以及授權認證,建議在部署之前先生成相關的TLS證書。 1.2 CA證書創建方式 kubernetes 系統各個組件需要使用TLS證書對通信進行加密,通常可通過以下工具生產自建證書: open ...


一 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創建證書

  1 [root@master ~]# MASTER_IP=172.24.8.71			#定義MASTER_IP
  2 [root@master ~]# mkdir cert				        #建議創建獨立存儲證書的目錄
  3 [root@master ~]# cd cert
  4 [root@master cert]# openssl genrsa -out ca.key 2048	        #生成一個 2048 bit的ca.key
  5 [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 設置證書的有效時間)
  6 [root@master cert]# openssl genrsa -out server.key 2048	#生成一個 2048 bit 的 server.key
  7 [root@master cert]# openssl req -new -key server.key -subj "/CN=${MASTER_IP}" -out server.csr	#根據 server.key 生成一個 server.csr
  8 [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
  9 [root@master cert]# openssl x509  -noout -text -in ./server.crt
 

三 cfssl生成證書

3.1 cfssl創建證書

  1 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl         #下載cfssl軟體
  2 [root@master ~]# chmod u+x /usr/local/bin/cfssl
  3 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson #下載json模板
  4 [root@master ~]# chmod u+x /usr/local/bin/cfssljson
  5 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
  6 [root@master ~]# chmod u+x /usr/local/bin/cfssl-certinfo
  7 [root@master ~]# mkdir cert
  8 [root@master ~]# cd cert/
  9 [root@master cert]# cfssl print-defaults config > config.json
 10 [root@master cert]# cfssl print-defaults csr > csr.json			#創建模版配置json文件
 11 [root@master cert]# cp config.json ca-config.json			        #複製一份作為CA的配置文件
 12 [root@master cert]# vi ca-config.json
 13 {
 14     "signing": {
 15         "default": {
 16             "expiry": "168h"
 17         },
 18         "profiles": {
 19             "kubernetes": {
 20                 "expiry": "8760h",
 21                 "usages": [
 22                     "signing",
 23                     "key encipherment",
 24                     "server auth"
 25                     "client auth"
 26                 ]
 27             }
 28         }
 29     }
 30 }
  欄位解釋: config.json:可以定義多個profiles,分別指定不同的過期時間、使用場景等參數;後續在簽名證書時使用某個profile;
  • signing: 表示該證書可用於簽名其它證書;生成的ca.pem 證書中CA=TRUE;
  • server auth: 表示client 可以用該CA 對server 提供的證書進行校驗;
  • client auth: 表示server 可以用該CA 對client 提供的證書進行驗證。
  1 [root@master cert]# cp csr.json ca-csr.json					#複製一份作為CA的配置文件
  2 [root@master cert]# vi ca-csr.json
  3 {
  4     "CN": "kubernetes",
  5     "key": {
  6         "algo": "rsa",
  7         "size": 2048
  8     },
  9     "names": [
 10         {
 11             "C": "CN",
 12             "ST": "Shanghai",
 13             "L": "Shanghai",
 14             "O": "k8s",
 15             "OU": "System"
 16         }
 17     ]
 18 }
  欄位解釋:
  • CN: Common Name,kube-apiserver 從證書中提取該欄位作為請求的用戶名(User Name);瀏覽器使用該欄位驗證網站是否合法;
  • C:country;
  • ST:state;
  • L:city;
  • O: Organization,kube-apiserver 從證書中提取該欄位作為請求用戶所屬的組(Group);
  • OU:organization unit。
  1 [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創建證書

  1 [root@master ~]# mkdir cert
  2 [root@master ~]# curl -LO https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz	#下載easyrsa軟體
  3 [root@master ~]# tar xzf easy-rsa.tar.gz
  4 [root@master ~]# cd easy-rsa-master/easyrsa3
  5 [root@master easyrsa3]# ./easyrsa init-pki
  6 [root@master easyrsa3]# MASTER_IP=172.24.8.71			                             #定義MASTER_IP
  7 [root@master easyrsa3]# ./easyrsa --batch "--req-cn=${MASTER_IP}@`date +%s`" build-ca nopass     #生成 CA
  解釋: --batch:設置為自動模式; --req-cn:設置預設的 CN
  1 [root@master easyrsa3]# ./easyrsa --subject-alt-name="IP:${MASTER_IP}" build-server-full server nopass	#生成伺服器證書和密鑰
解釋: build-server-full [文件名]:生成一個鍵值對,在本地為客戶端和伺服器簽名。
  1 [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
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 人員表+餅狀圖列印(1.不能用table佈局,2.瀏覽器不同會導致顯示頁面不同) ↩ 返回 ㊞ 導出&列印 ****報告 單位: 你單位於 參加測試,本次測試10題,體檢成績如下: 序號 編號 姓名 身份證號 分數 評價 1 201919191 張三 3735353535554454 100... ...
  • 生產環境伺服器安全策略與系統性能優化評估 1. Linux的運維經驗分享與故障排查思路 1.1 線上伺服器安裝基本策略和經驗 精簡安裝策略: 僅安裝需要的,按需安裝,不用不裝 開發包,基本網路包,基本應用包 1.1.1 CentOS 6.x ![image.png 93kB][1] ![image. ...
  • 記憶秘訣:BBDEH OPRLM TLSUV 寶貝的恩惠 歐派入聯盟 偷了suv,19 目錄 英文釋義 簡寫 詳解 1 / 根目錄 整個文件系統的唯一根目錄 2 /bin Binary 普通命令目錄 存放常用的系統命令 3 /boot Boot 開機引導目錄 包括Linux內核文件與開機所需要的文件 ...
  • [toc] linux許可權管理 特殊許可權 一,特殊許可權 1.suid(4000) SetUID(suid):會在屬主許可權位的執行許可權上寫個s 如果該屬主許可權位上有執行許可權,則:s 如果該屬主許可權位上沒有執行許可權,則:S 授權方式 setuid總結: ​ 1.讓普通用戶對可執行的二進位文件,臨時擁有二 ...
  • [toc] Linux許可權管理 特殊許可權 一、Linux系統特殊許可權概述 除了r(讀)、 w(寫)、 x(執行)這三種普通許可權外,在查詢系統文件許可權時會發現還有其他的許可權字母。 二、特殊許可權suid介紹 1. 許可權(4000) 在Linux系統中,每個普通用戶都可以更改自己的密碼,這是合理的設置,問 ...
  • 一、rpm 簡介 這是一個資料庫管理工具,可以通過讀取資料庫,判斷軟體是否已經安裝,如果已經安裝可以讀取出來所有文件的所在位置等,並可以實現刪除這些文件。 rpm:RPM is Redhat Package Manager(遞歸縮寫) rpm可以完成的操作 安裝軟體 卸載軟體 查詢軟體信息 升級、降 ...
  • 參考文章:https://www.jianshu.com/p/97c35d569aa3 因為Ubuntu自帶的源伺服器在國外,下載和更新軟體的時候速度很慢,不穩定,所以需要將源更新為國內的源。國內的源比較多,常用的就是阿裡的源。 1.備份原有源文件,方便在替換出問題時回滾 2.更改sources.l ...
  • 作用 實現對不同伺服器時間的同步校準 NTP時間服務 第一步 安裝 第二步 設置 進入配置文件 然後刪除裡面的所有內容。並插入以下代碼 第三部 重啟NTP服務 第四部 檢查NTP服務狀態 第五部 客戶端下載NTP客戶端服務 第六步 啟動測試 [root@localhost ~]# date -s " ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...