创建configmap
1 创建目录
mkdir ~/huawei_k8s/labfile/configmap && cd ~/huawei_k8s/labfile/configmap
2 创建两个文件,用于保存配置文件
#vim game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.lever=noGoodRotten
#vim ui.properties
color.good=purple
color.bad=yello
allow.textmode=true
3 从这两个文件创建configmap
kubectl create configmap game-config --from-file= ~/huawei_k8s/labfile/configmap
4 查看configmap
kubectl show configmap
kubectl describe configmap game-config
5 从文件创建configmap
kubectl create configmap game-config-3 --from-file=~/huawei_k8s/labfile/configmap/game.properties --from-file=~/huawei_k8s/labfile/configmap/ui.properties
6 查看game-config-3的详细信息,与game-config对比,内容一致
7 直接从参数创建configmap
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
8查看结果
9 从yaml文件创建configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: specialconfig-2
data:
key1: vaule1
pro.property: |
key2: value2
key3: value3
10 创建configmap并查看结果
使用configmap
1 创建pod的yaml文件,引用configmap做env参数
#vim cmpod.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: cm-container
image: busybox
args: ["/bin/sh","-c","env"]
env: #环境变量应用configmap
- name: special-env
valueFrom:
configMapKeyRef:
name: specialconfig-2
key: key1
envFrom:
- configMapRef:
name: specialconfig-2
restartPolicy: Never
2 创建pod
3 查看pod输出的日志
kubectl logs cm-test-pod
4 通过volume使用configmap,创建pod的yaml文件
#vim cmpod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: cmpod2
spec:
containers:
- name: cmpod2
image: busybox
args: ["/bin/sh","-c","sleep 3000"]
volumeMounts:
- name: db
mountPath: "/etc/db"
readOnly: true
volumes:
- name: db
configMap:
name: specialconfig-2
5 创建pod
6 进入pod。查看volume内文件
kubectl exec -it cmpod2 /bin/sh
cd /etc/db
ls
cat key1
cat por.porperty
使用Secret
1 创建两个文件存放用户和密码两个参数
echo -n "admin" > username.txt;echo -n "Huawei@123" > password.txt
2 使用命令行创建Secret,名字为db-user-pass
kubectl create secret generic db-user-pass --from-file=username.txt --from-file=password.txt
3 查看secret信息,可以看到secret不直接显示明文数据
4 使用base64数据编码
echo -n "admin" | base64 && echo -n "Huawei@123" | base64
5 创建yaml文件,写入编码后的数据
#vim secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: SHVhd2VpQDEyMw==
6 创建secret
7 查看secret的详细信息
使用secrets
1 创建pod的yaml文件,以volume形式挂载在secrets
#vim spod.yaml
apiVersion: v1
kind: Pod
metadata:
name: spod
spec:
containers:
- image: busybox
name: spod
args: ["/bin/sh","-c","sleep 3000"]
volumeMounts:
- name: secrets
mountPath: "/etc/secret"
readOnly: true
volumes:
- name: secrets
secret:
secretName: mysecret
2 创建pod
3 进入pod,查看pod内的secret数据,可以看到数据已经被解码,并且以一个"DATA"存储为一个文件
4 使用secret并且挂载指定值,创建使用secret的pod
#vim spod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: spod2
spec:
containers:
- image: busybox
name: spod2
args: ["/bin/sh","-c","sleep 3000"]
volumeMounts:
- name: secrets
mountPath: "/etc/secret"
readOnly: true
volumes:
- name: secrets
secret:
secretName: mysecret
items:
- key: password
path: my-group/my-passwd
5 创建pod
6 进入pod查看secret数据