k8s的安装:http://www.haijin.xyz/list/article/486
1、创建一个secret加密数据:
作用:加密数据存在oted里面,让Pod容器以挂载Volume方式进行访问
场景:凭证
secret.yaml:
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
kubectl apply -f secret.yaml #创建
kubectl get secret # 查看secret
创建一个pod:
secret-var.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 get pods
kubectl exec -it mypod bash #进入到pod中
echo $SECRET_USERNAME #查看变量
echo $SECRET_PASSWORD
以数据卷的方式进行挂载
kubectl delete -f secret-var.yaml #删除之前的pod
创建一个yaml文件
secret-vol.yaml:
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
进入到容器中
cd /etc/foo
cat password
cat username
2、以configmap的形式创建
作用:存储不加密数据到etcd,让Pod以变量或者Volume挂载到容器中
场景:配置文件
现在我们来创建一个以redis为例的实例
kubectl get secret
kubectl delete secret mysecret #删除之前创建的secret
kubectl get pods
kubectl delete pod mypod #删除之前创建的pod
创建一个redis的配置文件
redis.properties:
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
kubectl create configmap redis--config --from-file=redis.properties #创建configmap
kubectl get cm #查看configmap
kubectl describe cm redis--config #查看详细信息
以Volume挂载到pod容器中:
创建一个cm文件:
cm.yaml:
apiVersion: v1
kind: Pod
metadata:
name: mypod
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
kubectl apply -f cm.yaml #创建pod
kubectl logs mypod
以变量的形式挂载的pod中
kubectl delete -f cm.yaml # 删除之前的pod
创建yaml文件
myconfig.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
kuectl apply -f myconfig.yaml #创建cm
kubectl get cm #查看cm
config-var.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 apply -f config-var.yaml #创建pod
kubectl get pod # 查看pod
kubectl logs mypod # 查看日志
总结:configmap与secret类似 但是cm不进行加密 一般使用为配置文件