问题描述
如果我想在K8s中保存凭证信息,然后将其检索出来以在k8s中使用,我可以这样做吗?以及如何?
解决方法
是的,但是您可能不应该。
运行kubectl get secret
命令时,它在幕后的作用是对kubernetes api服务器的api调用。
要访问群集外部的机密,您将需要:
- 已将Kubernetes api公开给客户端(如果不在同一网络中)
- Setup authentication,以创建外部客户端使用的凭据
- 呼叫secrets endpoint。端点是这样的
/api/v1/namespaces/{namespace}/secrets
如前所述,您可能不应该这样做,因为市场上有很多工具可以用来进行秘密管理,它们更适合于这种情况。
,如果您能够在包含机密的名称空间内运行Pod,则可以创建使用该机密的Pod:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
然后将密码打印到stdout
:
kubectl exec -it mypod -- ls /etc/foo/
kubectl exec -it mypod -- cat /etc/foo/secret.file