Files
doc/利用scp和ssh实现中转服务器证书配置自动同步.md
2025-10-16 17:44:16 +08:00

74 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 利用scp和ssh实现中转服务器证书配置自动同步
**背景**
博主使用的架构是
用户-->中转服务器nginx反代-->wireguard-->rke2集群网关nginx
因为有中转tls握手需要在nginx实现一次
因为有wireguard自己会加密所以tls可以终止与中转服务器nginx
通过搜索资料得知这样也可以实现自动化利用scp和ssh创建一个pod监听tls证书生成
然后分成nginx需要的pem和key连同server配置块一同上传到中转服务器
scp需要登录用户对文件夹有写入权限这一点有时候比较难注意到
因此推荐使用sudo tee的方式这样可以在服务器配置该用户sudo某些指令不需要使用密码ssh连接使用bash更加方便
**操作**
使用预先创建的ssh密钥或者让pod自己生成ssh密钥
自己创建的密钥打包进入容器后记得设置权限私钥600公钥644否则ssh-server不会使用
上传例如这样:
```
ssh_command = [
"ssh", f"{NGINX_USER}@{NGINX_HOST}",
f"sudo tee {remote_file} > /dev/null"
]
```
编写好脚本后在服务器运行我使用tls-sync.yaml创建了一个pod为了使pod有权限监听全部命名空间需要创建一个serviceAccount并绑定pod
```
# serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: secret-watcher
namespace: basic
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-watcher-role
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: secret-watcher-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: secret-watcher-role
subjects:
- kind: ServiceAccount
name: secret-watcher
namespace: basic
```
namespace替换成pod运行的位置
使用kubectl apply应用serviceaccount.yaml和tls-sync.yaml
确保pod正常运行后及时将ssh密钥处理好然后进入容器手动进行一次ssh连接以信任并保存指纹
至此,监听程序正常运行