配置管理
ConfigMap
创建ConfigMap
使用 kubectl create configmap -h 查看示例,构建 configmap 对象
bash
$ kubectl create cm -h
##################### OUTPUT #############################
...
Examples:
# Create a new config map named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
# Create a new config map named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# Create a new config map named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
...
文件夹方式创建
bash
# cm/db.properties
username=root
password=admin
# cm/redis.properties
host=127.0.0.1
port=6379
# 创建cm
$ kubectl create configmap db-config --from-file=cm
# 查看cm列表
$ kubectl get cm
NAME DATA AGE
db-config 2 4m17s
kube-root-ca.crt 1 12d
# 查看db-config cm信息
$ kubectl describe cm db-config
...
Data
====
db.properties:
----
username=root
password=admin
redis.properties:
----
host=127.0.0.1
port=6379
...
文件方式创建
bash
# application.yaml
spring:
application:
name: test-app
server:
port: 8080
# 创建cm
$ kubectl create configmap spring-config --from-file=application.yaml
# 查看cm列表
$ kubectl get cm
NAME DATA AGE
db-config 2 10m
kube-root-ca.crt 1 12d
spring-config 1 6s
# 查看 spring-config cm
$ kubectl describe cm spring-config
...
Data
====
application.yaml:
----
spring:
application:
name: test-app
server:
port: 8080
...
使用ConfigMap
需求:将cm挂载到容器
资源清单
yaml
# test-cm.yaml
apiVersion: apps/v1 # deployment api版本
kind: Deployment # 资源类型为 deployment
metadata: # 元信息
labels: # 标签
app: test-cm
name: test-cm # deployment名称
namespace: default # 所在的命名空间
spec:
replicas: 1 # 期望副本数
revisionHistoryLimit: 10 # 进行滚动更新后保留的历史版本数
selector: # 选择器,用于找到匹配的RS
matchLabels: # 按照标签匹配
app: test-cm
strategy:
type: RollingUpdate # 更新类型,滚动更新
template: # pod模板
metadata: # pod的元信息
labels: # 标签
app: test-cm
spec: # pod的期望信息
containers: # pod容器
- image: alpine # 镜像
command: ["/bin/sh", "-c", "env;sleep 3600"]
imagePullPolicy: IfNotPresent # 拉取策略
name: test-cm # 容器名称
volumeMounts:
- name: application-config # 加载的数据卷的名字,需跟 volumes.name 对应上
mountPath: "/usr/local/mysql/conf" # 想要将数据卷中的文件加载到哪个目录下
readOnly: true # 是否只读
volumes:
- name: application-config # 数据卷名字
configMap:
name: spring-config
items: # 对 configMap中的key进行映射,不指定则将所有key转换为一个同名文件
- key: "application.yaml" # configMap 中的key
path: "application.yaml" # 将该key的值转换为文件
restartPolicy: Always # 重启策略
terminationGracePeriodSeconds: 30 # 删除操作最大宽限时长
bash
# 创建挂载了数据卷的deploy
$ kubectl create -f test-cm.yaml
# 查看文件挂载情况
$ kubectl exec -it test-cm-7c945f774c-lzjh2 -- sh
/ # cd /usr/local/mysql/conf/
/usr/local/mysql/conf # more application.yaml
################# OUTPUT ########################
spring:
application:
name: test-app
server:
port: 8080
注意:用 configMap 挂载会将容器内 整个目录全都覆盖掉 ,需要用 subPath 解决
Secret
与 configMap 类似,采用的是 Base64 加密,不安全。
持久化存储
Volumes
将节点上的文件或目录挂载到 Pod 上,此时该目录会变成持久化存储目录,即使 Pod 被删除后重启,也可以重新加载到该目录,该目录下的文件不会丢失
HostPath
yaml
apiVersion: v1
kind: Pod
metadata:
name: test-volume-pd
spec:
containers:
- image: nginx
name: nginx-volume
volumeMounts:
- mountPath: /test-pd # 挂载到容器的哪个目录
name: test-volume # 挂载哪个volume
volumes:
- name: test-volume
hostPath: # 与主机共享目录,加载主机中的指定目录到容器中
path: /data # 节点中的目录
type: DirectoryOrCreate # 检查类型,在挂载前对挂载目录做什么检查操作,有多种选项,默认为空字符串,不做任何检查