k8s往secret里导入证书_K8S之Secret
簡(jiǎn)介
secret顧名思義,用于存儲(chǔ)一些敏感的需要加密的數(shù)據(jù)。這些數(shù)據(jù)可能是要保存在pod的定義文件或者docker的鏡像中。把這些數(shù)據(jù)通過(guò)加密的方式存放到secrets對(duì)象中,可以降低信息泄露的風(fēng)險(xiǎn)。在secret中存儲(chǔ)的數(shù)據(jù)都需要通過(guò)base64進(jìn)行轉(zhuǎn)換加密后存放。
創(chuàng)建secret
1、加密用戶名密碼
假設(shè)有兩個(gè)敏感數(shù)據(jù),分別是用戶名breeze,密碼:123456。首先需要使用base64來(lái)轉(zhuǎn)換數(shù)據(jù):
echo -n "breeze"| base64
YnJlZXpl
echo -n "123456"| base64
MTIzNDU2
然后創(chuàng)建一個(gè)secret.yml文件,內(nèi)容如下:
apiVersion: v1
kind: Secret
metadata:
name: test-secret
data:
username: YnJlZXpl
password: MTIzNDU2
創(chuàng)建secret:
kubectl create -f secret.yml
也可以直接使用命令行生成secret:
kubectl create secret generic test-secret --from-literal=username='breeze',password='123456'
2、加密證書(shū)文件
kubectl create secret tls ingress-secret --key server.key --cert ca.crt
使用secret
1、使用volume掛載方式
給出一個(gè)pod示例:
apiVersion: v1
kind: Pod
metadata:
name: secret-test-pod
spec:
containers:
- name: test-container
image: nginx
volumeMounts:
- name: secret-volume
mountPath: /etc/secret-volume
volumes:
- name: secret-volume
secret:
secretName: test-secret
2、將secret用于env
創(chuàng)建一介secret.yml文件,內(nèi)容如下:
apiVersion: v1
data:
data-1: c2ltb25zdQ==
data-2: c2ltb25zdS5tYWlsQGdtYWlsLmNvbQ==
kind: Secret
metadata:
name: test-secret
namespace: default
type: Opaque
與configmap的用法一致,下面直接貼上示例文件:
apiVersion: v1
kind: Pod
metadata:
name: secret-by-env
spec:
containers:
- name: busydayeasylife
image: gcr.io/google_containers/busybox:1.24
command:
- /bin/sh
- -c
- --
args:
- "while [ true ]; do echo $NAME; echo $EMAIL; sleep 30; done;"
env:
- name: NAME
valueFrom:
secretKeyRef:
name: test-secret
key: data-1
- name: EMAIL
valueFrom:
secretKeyRef:
name: test-secret
key: data-2
總結(jié)
以上是生活随笔為你收集整理的k8s往secret里导入证书_K8S之Secret的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python定义函数prime判断是否是
- 下一篇: 罗技g703怎么连电脑(罗技g733使用