Kubernetes实战篇之配置与存储

配置管理

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	# 检查类型,在挂载前对挂载目录做什么检查操作,有多种选项,默认为空字符串,不做任何检查
相关推荐
wxjlkh18 小时前
5分钟部署Docker!Rocky Linux极速安装+一键加速配置脚本
云原生·eureka
丈剑走天涯18 小时前
kubernetes java app 部署使用harbor私服 问题集合
java·容器·kubernetes
Jinkxs20 小时前
Java 部署:滚动更新(K8s RollingUpdate 策略)
java·开发语言·kubernetes
lpfasd12321 小时前
Kubernetes (K8s) 底层早已不再直接使用 Docker 引擎了
java·docker·kubernetes
不吃香菜kkk、1 天前
通过夜莺n9e监控Kubernetes集群
安全·云原生·容器·kubernetes
淡泊if1 天前
K8s 网络排障:从抓包开始,一步步定位诡异“502”
网络·kubernetes·k8s
小李的便利店1 天前
k8s集群部署Prometheus和Grafana
kubernetes·grafana·prometheus·监控
margu_1681 天前
【Docker】nscenter命令详解
运维·docker·容器
阿里云云原生1 天前
Higress 加入 CNCF:保障 Nginx Ingress 迁移,提供企业级 AI 网关
云原生
道清茗1 天前
【Kubernetes知识点问答题】Pod 调度
云原生·容器·kubernetes