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	# 检查类型,在挂载前对挂载目录做什么检查操作,有多种选项,默认为空字符串,不做任何检查
相关推荐
正经教主1 天前
【docker基础】第一课、从零开始理解容器技术
docker·云原生·容器·eureka
正经教主1 天前
【docker基础】0、系统学习docker之总计划
学习·docker·容器
Yang三少喜欢撸铁1 天前
【Centos7通过kubeadm方式部署kubernetes1.30版本【一主两从】】
docker·kubernetes·container
@土豆1 天前
基于Docker部署Squid正向代理文档
运维·docker·容器
Cyber4K2 天前
【Kubernetes专项】K8s 包工具- Helm 入门到企业实战
云原生·容器·kubernetes
观无2 天前
微服务下的跨域问题
微服务·云原生·架构
Chuncheng's blog2 天前
K8S二进制部署exec unable to upgrade connection: Unauthorized异常解决方案
云原生·容器·kubernetes
FJW0208142 天前
HAProxy+Keepalived实现Kubernetes高可用集群部署
云原生·容器·kubernetes
正经教主2 天前
【docker基础】第二课:安装、配置与基础命令
docker·容器·eureka
倔强的胖蚂蚁2 天前
云原生服务器存储规划与磁盘选型实施
运维·服务器·云原生