K8s 基于TLS的认证 命令
时间: 2024-05-10 21:18:28 浏览: 134
Kubernetes 基于TLS的认证需要使用证书和私钥。以下是使用命令行工具生成证书和私钥的步骤:
1. 生成私钥
```bash
openssl genrsa -out <private_key_filename>.key 2048
```
2. 生成证书签名请求 (CSR)
```bash
openssl req -new -key <private_key_filename>.key \
-out <csr_filename>.csr \
-subj "/CN=<username>/O=<organization>"
```
其中 `<username>` 为用户名,`<organization>` 为组织名。
3. 签发证书
在 Kubernetes 集群中,通常有一组可信任的 CA 证书,可以使用该 CA 证书签发用户证书。以下是示例命令:
```bash
openssl x509 -req -in <csr_filename>.csr \
-CA <ca_cert_filename>.crt \
-CAkey <ca_private_key_filename>.key \
-CAcreateserial \
-out <user_cert_filename>.crt \
-days 365
```
其中 `<ca_cert_filename>` 为 CA 证书文件名,`<ca_private_key_filename>` 为 CA 私钥文件名,`<user_cert_filename>` 为用户证书文件名。
4. 配置 Kubernetes 客户端
生成的证书和私钥需要在 Kubernetes 客户端中配置。以下是示例命令:
```bash
kubectl config set-credentials <username> \
--client-certificate=<user_cert_filename>.crt \
--client-key=<private_key_filename>.key
```
其中 `<username>` 为用户名,`<user_cert_filename>` 为用户证书文件名,`<private_key_filename>` 为私钥文件名。
然后,将用户配置添加到 Kubernetes 配置文件中:
```bash
kubectl config set-context <context_name> \
--cluster=<cluster_name> \
--user=<username>
```
其中 `<context_name>` 为上下文名称,`<cluster_name>` 为集群名称。
阅读全文
相关推荐


















