目录
一、configmap
1.1 configmap概述
1.2 configmap作用
1.2 configmap特点
1.3 configmap结构
2、创建方法
2.1 命令行直接创建
2.2 通过文件/目录创建
2.3 YAML创建
3、使用
3.1 变量引入
3.2 文件挂载
二、Secret
1、概述
2、创建
2.1 命令行创建
2.2 YAML文件创建
3、使用
3.1 变量引用
3.2 文件挂载
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/
}
新创一个configmap用于测试
apiVersion: v1
kind: ConfigMap
metadata:
name: test
labels:
app: test
data:
xy: "xiayan"
hi: "hello world"
kubectl apply -f test.yaml
创建一个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
kubectl apply -f pod-configmap.yaml
kubectl get pod
kubectl logs mypod
apiVersion: v1
kind: Pod
metadata:
name: mypod1
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/hi" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: test
restartPolicy: Never
#创建pod
kubectl apply -f cm.yaml
#查看pod日志
kubectl logs mypod1
Secret 解决了密码、token、秘钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec 中。Secret 可以以 Volume 或者环境变量的方式使用。
secret 可选参数有三种:
Secret 类型:
Secret的命令行创建方法与configmap一样,可以通过命令行或者文件/目录的方式创建,这里就不过多介绍,此处使用命名行创建,其它方法可以参照上方的方法进行创建
把 mysql 的 root 用户的 password 创建成 secret
kubectl create secret generic mysql-password --from-literal=password=xy123456
# 查看secret详细信息
kubectl describe secret mysql-password
通过手动加密,基于base64加密
echo -n 'admin' | base64
YWRtaW4=
echo -n 'xy123456' | base64
eHkxMjM0NTY=
创建YAML
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: eHkxMjM0NTY=
kubectl apply -f secret.yaml
kubectl get secret
kubectl describe secret mysecret
使用方法与configmap一样
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
kubectl apply -f pod_secret_volume.yaml
kubectl get pod
kubectl exec -it secret-volume-pod /bin/bash
进入pod可以看到/etc/secret下有password和username两个文件,查看内容和我们创建的secret内容吻合。