74 lines
2.1 KiB
Markdown
74 lines
2.1 KiB
Markdown
# 利用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连接以信任并保存指纹
|
||
|
||
至此,监听程序正常运行 |