从零开始学习k8s-10配置
点点寒彬 2021-11-05 21:24:19
kubernetes
背景
k8s支持的配置主要有以下几种:
- Secret
- ConfigMap
- Downward API
- ServiceAccountToken
一般我们可能会用到的是前两个
Secret
Secret
比较典型的场景就是存放一些凭证,例如用户名密码这些。
apiVersion: v1
kind: Pod
metadata:
name: test-projected-volume
spec:
containers:
- name: test-secret-volume
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysql-cred
mountPath: "/projected-volume"
readOnly: true
volumes:
- name: mysql-cred
secret:
secretName: user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
这里我们会使用user
这个凭证。我们在创建之前,需要先创建这两个secret
。
echo 'sven' > username.txt
kubectl create secret generic user --from-file=./username.txt
secret/user created
kubectl get secrets
NAME TYPE DATA AGE
user Opaque 1 57s
1
2
3
4
5
6
7
2
3
4
5
6
7
我们创建这个容器,然后进去查看
kubectl exec -it test-projected-volume -- /bin/bash
cd projected-volume/
ls
username.txt
cat username.txt
sven
1
2
3
4
5
6
2
3
4
5
6
可以看到凭证已经被写入到挂载对应的目录下了。
这种方式,我们的数据将会保存在k8s
的etcd
中,一旦这个数据在etcd
中被更新了,那么所有的pod
内的这个文件都会被更细。
ConfigMap
ConfigMap
指的是那些不需要加密的信息,例如常规的配置文件。
举个例子,我们有这样一个配置文件
cat xxx.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
1
2
3
4
5
2
3
4
5
我们使用这个配置文件创建一个ConfigMap
吧刚刚的yaml
文件调整一下
apiVersion: v1
kind: Pod
metadata:
name: test-configmap
spec:
containers:
- name: test-configmap
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysql-cred
mountPath: "/projected-volume"
readOnly: true
volumes:
- name: mysql-cred
configMap:
name: xxxconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
创建容器后可以看到,这个文件已经在目录中了。
/projected-volume # ls
xxx.properties
/projected-volume # cat xxx.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
1
2
3
4
5
6
7
2
3
4
5
6
7
- 一些注意点
首先ConfigMap
不是用来存储大量数据的,它只能存储1MB的数据。
ConfigMap
也是存储在etcd
中,一旦发生了变更,那么所有的容器内的ConfigMap
也会一并发生变更。
ConfigMap
里面的对象应该是确定的,也就是不会因为容器创建而发生变化的数据。