目录
一、Secret
1、概述
2、创建及使用
2.1、用kubectl create secret命令创建Secret
2.2、YAML文件创建
3、变量引用
4、文件挂载
二、configmap
1、概述
2、configmap作用
3、configmap特点
4、创建方法
4.1、命令行直接创建
4.2、通过文件/目录创建
编辑 4.3、通过目录创建
4.4、YAML创建
新创一个configmap用于测试
4.5、变量引入
4.6、文件挂载
Secret 解决了密码、token、秘钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec 中。Secret 可以以 Volume 或者环境变量的方式使用。
secret 可选参数有三种:
Secret 类型:
Pod 需要先引用才能使用某个 secret,Pod 有 3 种方式来使用 secret:
mkdir -p /opt/demo
cd /opt/demo
echo -n 'admin' > ./username.txt
echo -n '1f2d1e2e67df' > ./password.txt
kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
kubectl get secret
kubectl describe secret db-user-pass
通过手动加密,基于base64加密
[root@master demo]# echo -n "admin" | base64
YWRtaW4=
[root@master demo]# echo -n "12345678" |base64
MTIzNDU2Nzg=
创建YAML
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MTIzNDU2Nzg=
kubectl apply -f secret.yaml
kubectl get secret
kubectl describe secret mysecret
apiVersion: v1
kind: Pod
metadata:
name: secretpod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","echo $(SECRET_USERNAME) $(SECRET_PASSWORD)" ]
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
restartPolicy: Never
kubectl apply -f secret-pod.yaml
kubectl get pod
kubectl logs secretpod
将 Secret 挂载到 Volume 中
vim pod_secret_volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: secret-volume-pod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: secret-volume
mountPath: "/etc/secret"
readOnly: true
volumes:
- name: secret-volume
secret:
secretName: mysecret
进入pod可以看到/etc/secret下有password和username两个文件,查看内容和我们创建的secret内容吻合。
Configmap 是 k8s 中的资源对象,用于保存非机密性的配置的,数据可以用 key/value 键值对的
形式保存,也可通过文件的形式保存。
我们在部署服务的时候,每个服务都有自己的配置文件,
直接在命令行中指定 configmap 参数创建,通过**–from-literal** 指定参数
#创建
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=nginx
#查看configmap
kubectl describe configmap nginx-config
通过指定文件创建一个 configmap,–from-file=<文件/目录>
vim nginx.conf
server {
server_name www.nginx.com;
listen 80;
root /home/nginx/www/
}
通过指定文件创建
kubectl create configmap www-nginx --from-file=www=/opt/config/nginx.conf
kubectl describe configmap www-nginx
当某些服务存在多个配置文件时,放在同一目录下,我们可以指定目录,进行创建
kubectl create configmap www-nginx2 --from-file=/opt/config/
kubectl describe configmap www-nginx2
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
labels:
app: nginx-conf
data:
nginx.conf: |
server {
server_name www.nginx.com;
listen 80;
root /home/nginx/www/
}
apiVersion: v1
kind: ConfigMap
metadata:
name: test
labels:
app: test
data:
li: "liy"
yo: "hello world"
创建一个pod,引用configmap
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: test
key: xy
- name: TYPE
valueFrom:
configMapKeyRef:
name: test
key: hi
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: mypod1
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/yo" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: test
restartPolicy: Never