kubernetes文件配置管理包括两个部分,分别是Secret&Configmap,Secret主要应用于保存镜像仓库认证信息、凭证、https证书。Configmap主要应用于存储应用程序配置文件。
Secret
作用是将数据加密并存放到etcd中,让pod容器以挂载volume方式访问,主要的应用场景就是保存凭据,像是保存私有仓库的凭据信息,还有就是保存证书,用的都是这个。主要使用有两个方面,一个是存储一定的值能保存到容器中当作环境变量,另一种以文件的形式挂载进去给程序使用。
手动创建Secret
[root@k8s01 ~]# echo -n 'admin' > ./username.txt [root@k8s01 ~]# echo -n '123456' >./password.txt [root@k8s01 ~]# kubectl create secret generic db-user-pass --from-file=username.txt --from-file=password.txt secret/db-user-pass created [root@k8s01 ~]# kubectl get secrets NAME TYPE DATA AGE db-user-pass Opaque 2 22s default-token-bv97f kubernetes.io/service-account-token 3 5h26m nfs-client-provisioner-token-vfjf7 kubernetes.io/service-account-token 3 5h10m [root@k8s01 ~]# kubectl describe secrets db-user-pass Name: db-user-pass Namespace: default Labels: <none> Annotations: <none> Type: Opaque Data ==== password.txt: 6 bytes username.txt: 5 bytes [root@k8s01 ~]# |
这里看到的数据都不是明文的,所以无法看都他的信息,为了保证数据的安全性。
yaml文件创建
先通过base64位创建用户和密码。
[root@k8s01 ~]# echo -n 'admin' |base64 YWRtaW4= [root@k8s01 ~]# echo -n '12345' |base64 MTIzNDU= |
再编写yaml文件
[root@k8s01 ~]# cat secret.yaml apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: username: YWRtaW4= password: MTIzNDU= [root@k8s01 ~]# kubectl create -f secret.yaml secret/mysecret created [root@k8s01 ~]# kubectl get secrets NAME TYPE DATA AGE db-user-pass Opaque 2 4m58s default-token-bv97f kubernetes.io/service-account-token 3 5h31m mysecret Opaque 2 5s nfs-client-provisioner-token-vfjf7 kubernetes.io/service-account-token 3 5h15m [root@k8s01 ~]# |
容器中以变量引用Secret
[root@k8s01 ~]# cat 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 |
env来定义Pod的环境变量,name: SECRET_USERNAME为之个key: username值来设置这个环境变量的名字,这个值来自于name: mysecret里面,key: username这个key是name: mysecret的用户名,另一个name同上。
[root@k8s01 ~]# kubectl create -f secret-var.yaml pod/mypod created |
进入容器查看变量
[root@k8s01 ~]# kubectl exec -it mypod /bin/bash root@mypod:/# echo $SECRET_USERNAME admin root@mypod:/# echo $SECRET_PASSWORD 12345 root@mypod:/# |
以数据卷形式挂载到容器中
[root@k8s01 ~]# cat 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 |
定义一个pod,通过volumeMounts将volumes挂载到/etc/foo下面,会将mysecret的username和password作为文件名,创建后到容器看一下,
[root@k8s01 ~]# kubectl create -f secret-vol.yaml pod/mypod created [root@k8s01 ~]# |
查看变量
[root@k8s01 ~]# kubectl exec -it mypod /bin/bash root@mypod:/# ls -l /etc/foo/ total 0 lrwxrwxrwx 1 root root 15 Jun 7 13:33 password -> ..data/password lrwxrwxrwx 1 root root 15 Jun 7 13:33 username -> ..data/username root@mypod:/# cat /etc/foo/username;echo '' admin root@mypod:/# cat /etc/foo/password;echo '' 12345 root@mypod:/# |
ConfigMap
他和secret相似,主要存储不需要加密的数据,一般是用来保存配置文件,和secret一样可以通过``kubectl和yaml创建,他也是两种主要的用法,一种以变量的形式导入到pod中,另一个是以volumes`挂载,如果通过目录创建会让里面的所以的文件保存到不同的文件名,如果是文件他只保存里面的文件数据
kubectl创建
[root@k8s01 ~]# cat redis.properties redis.host=127.0.0.1 redis.port=6379 redis.password=123456 [root@k8s01 ~]# kubectl create configmap redis-config --from-file=redis.properties configmap/redis-config created [root@k8s01 ~]# kubectl get con configmaps controllerrevisions.apps [root@k8s01 ~]# kubectl get configmaps NAME DATA AGE redis-config 1 8s [root@k8s01 ~]# kubectl describe cm redis-config Name: redis-config Namespace: default Labels: <none> Annotations: <none> Data ==== redis.properties: ---- redis.host=127.0.0.1 redis.port=6379 redis.password=123456 Events: <none> |
可以看到这里的数据是明文的,具体怎么用,还是有两种办法,一个是以volume的形式挂载进去,还有一个就是变量,既然是配置文件,最好是挂volume了
volume挂载
[root@k8s01 ~]# cat 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 |
数据卷的来源就是configMap,挂载到了/etc/config目录下:
[root@k8s01 ~]# kubectl create -f cm.yaml pod/mypod created [root@k8s01 ~]# kubectl logs mypod redis.host=127.0.0.1 redis.port=6379 redis.password=123456 [root@k8s01 ~]# |
yaml创建
[root@k8s01 ~]# cat myconfig.yaml apiVersion: v1 kind: ConfigMap metadata: name: myconfig namespace: default data: special.level: info special.type: hello [root@k8s01 ~]# kubectl create -f myconfig.yaml configmap/myconfig created [root@k8s01 ~]# kubectl get cm NAME DATA AGE myconfig 2 4s redis-config 1 9m26s [root@k8s01 ~]# |
以变量挂载
[root@k8s01 ~]# cat 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 [root@k8s01 ~]# kubectl create -f config-var.yaml pod/mypod created [root@k8s01 ~]# kubectl logs mypod info hello [root@k8s01 ~]# |
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理