0.概念
名称解释
apiVersion创建该对象所使用的k8s API的版本
kind需要创建的对象的类别
metadata唯一性标识对象的一些数据
spec对象的状态
1.ConfigMap(保存非机密信息)

ConfigMap是一种API对象,用来将非机密性的数据保存到键值对中。使用时,Pods可以将其当作环境变量,命令参数或存储卷中的配置文件;
ConfigMap将环境配置信息和容器镜像解耦,便于应用配置的修改;

apiVersion: v1
kind: ConfigMap
metadata: 
  name: game-demo
data:
  name: "liyuan"
  age: "20"
  gender: "male"
  
  game.properties: |
    student.name=liyuan,yuanxi
    student.age=20
# 实践
apiVersion: v1
kind: Pod
metadata: 
  name: cm-pod
spec:
  containers:
  - name: demo
    image: alpine
    command: ["sleep","3600"]
    env:
      - name: name1
        valueFrom:
          configMapKeyRef:
            name: game-demo  # configMap名称
            key: name  #取至相关configMap
      - name: name2
        valueFrom:
          configMapKeyRef:
            name: game-demo
            key: age
    volumeMounts:
    - name: config
      mountPath: "/config"
      readOnly: true
  volumes:
    - name: config
      configMap: 
        name: game-demo
        items:
        - key: "game.properties"
          path: "game.properties"
2. StatefulSets

用于管理有状态应用的工作负载API对象,用于管理Pod集合的部署和扩缩,并为这些Pod提供持久化存储和持久化标识符

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi

3.服务

将运行在一组Pods上的应用程序公开为网络服务的抽象方法。
Service:逻辑上的一组Pod,一种可以访问它们的策略,通常称为微服务。

3.持久卷(PersistentVolume)

PersistentVolume: 集群中的一块存储,可由管理员事先提供或使用存储类动态供应,持久卷为集群资源;

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2
4.PersistentVolumeClaim

表达的是用户对于存储的请求,会消耗PV资源,可申请特定大小和访问模式的卷;

静态供应:管理员实现创建好的卷
动态供应:通过StorageClass实现

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐