在k8s中使用secret挂载rsa密钥对

有好几种方法,我使用的不是直接写在yaml中,而是使用命令,全程不会接触和修改rsa的公私钥文件内容。

一,拿到或生成pem格式的密钥对。

过程略,假设拿到的文件名为private.pem和public.pem

二,在K8s集群中导入rsa文件。

命名为rsa-secret ,namespace为cgtest.

kubectl create secret generic rsa-secret --from-file=ssh-privatekey=private.pem --from-file=ssh-publickey=public.pem -n cgtest

后期,如果要更新,需要先delete(kubectl delete secret rsa-secret -n cgtest),再create.

三,在pod中挂载

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: golang-deploy
  namespace: cgtest
spec:
  replicas: 1
  selector:
    matchLabels:
      app: golang-deploy
  template:
    metadata:
      labels:
        app: golang-deploy
    spec:
      nodeName: xxxxx
      containers:
      - name: golang
        image: demo.harbor/golang:1.12.9-alpine3.10
        imagePullPolicy: IfNotPresent
        command: ["sh","-c","while true;do date;sleep 1;done"]
        volumeMounts:
        - name: secret-volume
          readOnly: true
          mountPath: "/etc/secret-volume"
      volumes:
      - name: secret-volume
        secret:
          secretName: rsa-secret

其中,volumes不带namespace,但会自动从Deployment下读namespace。

四,在应用中读取。

程序代码就从/etc/secret-volume目录下读取private.pem(私钥)和public.pem(公钥)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值