Secret
Secret 的次要作用就是加密数据,而后存在 etcd 外面,让 Pod 容器以挂载 Volume 形式进行拜访
场景:用户名 和 明码进行加密
个别场景的是对某个字符串进行 base64 编码 进行加密
echo -n 'admin' | base64
echo -n 'admin123' | base64
变量模式挂载到 Pod
1. 创立 secret 加密数据 secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: YWRtaW4xMjM=
创立 secret
kubectl create -f secret.yaml
查看 secret
kubectl get secret
NAME TYPE DATA AGE
mysecret Opaque 2 18s
2. 以变量模式挂载到 pod 容器中
创立 yaml 文件 secret-val.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
执行
kubectl apply -f secret-val.yaml
查看容器中有没有变量
kubectl get pods
kubectl exec -it mypod
echo $SECRET_USERNAME #admin
echo $SECRET_PASSWORD #admin123
删除 Pod
kubectl delete -f secret-val.yaml
3. 以数据卷模式挂载
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
执行
kubectl apply -f secret-vol.yaml
查看
kubectl exec -it mypod bash
cat /etc/foo/username #admin
cat /etc/foo/password #admin123
删除全副 pod
kubectl delete pod --all
ConfigMap 配置文件
ConfigMap 作用是存储不加密的数据到 etcd 中,让 Pod 以变量或数据卷 Volume 挂载到容器中
创立配置文件
vim redis.properties
redis.port=127.0.0.1
redis.port=6379
redis.password=123456
创立 ConfigMap
kubectl create configmap redis-config --from-file=redis.properties
查看详细信息
kubectl describe cm redis-config
Name: redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis.properties:
----
redis.port=127.0.0.1
redis.port=6379
redis.password=123456
Events: <none>
以数据卷模式挂载
编辑 yaml
创立查看
# 创立
kubectl apply -f cm.yaml
# 查看
kubectl get pods
# 查看执行日志
kubectl logs mypod
删除 configmap
kubectl get cm
kubectl delete -f cm.yaml
以变量的模式挂载 Pod
配置文件 myconfig.yaml 申明变量
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
创立和查看
# 创立 pod
kubectl apply -f myconfig.yaml
# 获取
kubectl get cm
NAME DATA AGE
myconfig 2 3s
应用配置 config-val.yaml
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: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
查看日志
kubectl logs mypod
#输入 info hello