参考官方文档:https://kubernetes.io/docs/concepts/configuration/secret/
方式一:使用kubectl create secret方式创建
创建名为secret01的secret资源
##编写secret01数据信息,并创建
[root@k8s_master ~]# echo -n 'admin' > ./username.txt
[root@k8s_master ~]# echo -n '1f2d1e2e67df' > ./password.txt
[root@k8s_master ~]# kubectl create secret generic secret01 --from-file=./username.txt --from-file=./password.txt
查看已经创建的secret信息
[root@k8s_master ~]# kubectl describe secret secret01
Name: secret01
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
password.txt: 12 bytes
username.txt: 5 bytes
方式二:编写yaml文件,引用64位编码
先通过64位编码方式,明文转密文
[root@k8s_master ~]# echo -n 'admin' | base64
YWRtaW4=
[root@k8s_master ~]# echo -n '1f2d1e2e67df' | base64
MWYyZDFlMmU2N2Rm
编写yaml文件,引用密文创建secret
[root@k8s_master ~]# vim secret02.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
##创建secret资源
[root@k8s_master ~]# kubectl apply -f secret02.yaml
查看已经创建的secret信息
[root@k8s_master ~]# kubectl describe secret mysecret
Name: mysecret
Namespace: default
Labels: <none>
Annotations:
Type: Opaque
Data
====
password: 12 bytes
username: 5 bytes
第一种:使用secret中的变量导入到pod中
示例:将上面secret资源中的username和password重新赋值
key:username赋值给aaa
key:password 赋值给bbb
编写pod资源,将secret中的变量赋值
##编写yaml文件
[root@k8s_master ~]# vim secret03.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
- name: aaa
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: bbb
valueFrom:
secretKeyRef:
name: mysecret
key: password
##创建pod资源
[root@k8s_master ~]# kubectl apply -f secret03.yaml
进入该pod资源,验证secret信息
##查看pod资源创建情况
[root@k8s_master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 29s
##进入pod,验证secret信息
[root@k8s_master ~]# kubectl exec -it mypod bash
root@mypod:/# echo $aaa
admin
root@mypod:/# echo $bbb
1f2d1e2e67df
第二种:将secret容器以volume的形式挂载到pod的某个目录下
示例:
##编写yaml文件,创建pod资源
[root@k8s_master ~]# vim secret04.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod02
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
##创建pod资源
[root@k8s_master ~]# kubectl create -f secret04.yaml
进入该pod查看数据卷中的secret信息
[root@k8s_master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 7m35s
mypod02 1/1 Running 0 39s
##进入pod,查看secret信息
[root@k8s_master ~]# kubectl exec -it mypod02 bash
root@mypod02:/# cd /etc/foo
root@mypod02:/etc/foo# ls
password username
root@mypod02:/etc/foo# cat username
adminroot
root@mypod02:/etc/foo# cat password
1f2d1e2e67
方式一:使用kubectl创建
示例:
[root@k8s_master ~]# vim redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
[root@k8s_master ~]# kubectl create configmap redis-config --from-file=redis.properties
[root@k8s_master ~]# kubectl get configmap
NAME DATA AGE
redis-config 1 74s
[root@k8s_master ~]# kubectl describe configmap redis-config
Name: redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
Events: <none>
方式二:变量参数形式
示例:
创建configmap资源,定义变量
special.level: info
special.type: hello
##编写yaml文件
[root@k8s_master ~]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
##创建configap资源
[root@k8s_master ~]# kubectl apply -f configmap.yaml
[root@k8s_master ~]# kubectl describe configmap myconfig
Name: myconfig
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","data":{"special.level":"info","special.type":"hello"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"myconfig...
Data
====
special.level:
----
info
special.type:
----
hello
方法一:创建mypod资源查看文件导入(引用redis-config容器)
##编写yaml文件
[root@k8s_master ~]# vim test01.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod01
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: redis-config
restartPolicy: Never
##创建资源
[root@k8s_master ~]# kubectl apply -f test01.yaml
##查看资源创建情况
[root@k8s_master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod01 0/1 Completed 0 60s
##查看该资源信息
[root@k8s_master ~]# kubectl logs mypod01
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
方式二:使用configmap资源输出变量参数(引用myconfig容器)
##编写yaml文件
[root@k8s_master ~]# vim test02.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod05
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
##创建pod资源
[root@k8s_master ~]# kubectl apply -f test02.yaml
[root@k8s_master ~]# kubectl logs mypod05
info hello