Kubernetes数据持久化方案

在开始介绍k8s持久化存储前,咱们有必要了解一下k8s的emptydir和hostpath、configmap以及secret的机制和用途。html

一、Emptydir
EmptyDir是一个空目录,他的生命周期和所属的 Pod 是彻底一致的,EmptyDir主要做用能够在同一 Pod 内的不一样容器之间共享工做过程当中产生的文件。若是Pod配置了emptyDir类型Volume, Pod 被分配到Node上时候,会建立emptyDir,只要Pod运行在Node上,emptyDir都会存在(容器挂掉不会致使emptyDir丢失数据),可是若是Pod从Node上被删除(Pod被删除,或者Pod发生迁移),emptyDir也会被删除,而且永久丢失。node

# cat emptydir.yaml 
apiVersion: v1
kind: Pod
metadata:
   name: busybox
spec:
  containers:
   - name : busybox
     image: registry.fjhb.cn/busybox
     imagePullPolicy: IfNotPresent
     command:
      - sleep
      - "3600"
     volumeMounts:
      - mountPath: /busybox-data
        name: data
  volumes:
   - name: data
     emptyDir: {}

Kubernetes数据持久化方案
二、Hostpath
Hostpath会把宿主机上的指定卷加载到容器之中,若是 Pod 发生跨主机的重建,其内容就难保证了。这种卷通常和DaemonSet搭配使用。hostPath容许挂载Node上的文件系统到Pod里面去。若是Pod有须要使用Node上的东西,可使用hostPath,不过不过建议使用,由于理论上Pod不该该感知Node的。nginx

# cat hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:
   name: busybox
spec:
  containers:
   - name : busybox
     image: registry.fjhb.cn/busybox
     imagePullPolicy: IfNotPresent
     command:
      - sleep
      - "3600"
     volumeMounts:
      - mountPath: /busybox-data
        name: data
  volumes:
   - hostPath:
      path: /tmp
     name: data

Kubernetes数据持久化方案
emptyDir和hostPat不少场景是没法知足持久化需求,由于在Pod发生迁移的时候,数据都没法进行转移的,这就须要分布式文件系统的支持。docker

三、Configmap
镜像使用的过程当中,常常须要利用配置文件、启动脚本等方式来影响容器的运行方式,若是仅有少许配置,咱们可使用环境变量的方式来进行配置。然而对于一些较为复杂的配置,k8s提供了configmap解决方案。 
ConfigMap API资源存储键/值对配置数据,这些数据能够在pods里使用。
ConfigMap跟Secrets相似,可是ConfigMap能够更方便的处理不包含敏感信息的字符串。
当ConfigMap以数据卷的形式挂载进Pod的时,这时更新ConfigMap(或删掉重建ConfigMap),Pod内挂载的配置信息会热更新。这时能够增长一些监测配置文件变动的脚本,而后reload对应服务
ConfigMap的API概念上来讲是很简单的。从数据角度来看,ConfigMap的类型只是键值组。应用能够从不一样角度来配置。在一个pod里面使用ConfigMap大体有三种方式:
一、命令行参数
二、环境变量
三、数据卷文件api

将变量作成configmap
Kubernetes数据持久化方案安全

将nginx配置文件作成configmap网络

# cat nginx.conf    
user nginx;
worker_processes auto;
error_log /etc/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server_tokens     off;
    access_log        /usr/share/nginx/html/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
[root@vm1 ~]# cat nginx.conf   
user nginx;
worker_processes auto;
error_log /etc/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server_tokens     off;
    access_log        /usr/share/nginx/html/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}
# kubectl create configmap nginxconfig --from-file nginx.conf  
# kubectl get configmap
# kubectl get configmap -o yaml

Kubernetes数据持久化方案
Kubernetes数据持久化方案
在rc配置文件中使用configmapapp

# cat nginx-rc-configmap.yaml   
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels: 
       name: nginx
    spec:
      containers:
      - name: nginx
        image: docker.io/nginx
        volumeMounts:
        - name: nginx-etc
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-etc
        configMap:
         name: nginxconfig 
         items:
          - key: nginx.conf 
            path: nginx.conf
# kubectl create -f nginx-rc-configmap.yaml

Kubernetes数据持久化方案
Kubernetes数据持久化方案
configmap的信息实际是存储在etcd中的,可使用kubectl edit configmap xxx 来对configmap进行修改tcp

# etcdctl ls /registry/configmaps/default
# etcdctl get /registry/configmaps/default/nginxconfig

Kubernetes数据持久化方案
四、Secret
Kubemetes提供了Secret来处理敏感数据,好比密码、Token和密钥,相比于直接将敏感数据配置在Pod的定义或者镜像中,Secret提供了更加安全的机制(Base64加密),防止数据泄露。Secret的建立是独立于Pod的,以数据卷的形式挂载到Pod中,Secret的数据将以文件的形式保存,容器经过读取文件能够获取须要的数据。
目前Secret的类型有3种: 
Opaque(default): 任意字符串 
kubernetes.io/service-account-token: 做用于ServiceAccount
kubernetes.io/dockercfg: 做用于Docker registry,用户下载docker镜像认证使用
secert的具体配置在前文serviceaccount中已经介绍过了,本文再也不赘述。分布式

下面咱们来介绍一下k8s的持久化存储方案,目前k8s支持的存储方案主要以下:
分布式文件系统:NFS/GlusterFS/CephFS
公有云存储方案:AWS/GCE/Auzre

Nfs存储方案
NFS 是Network File System的缩写,即网络文件系统。Kubernetes中经过简单地配置就能够挂载NFS到Pod中,而NFS中的数据是能够永久保存的,同时NFS支持同时写操做。

一、首先安装nfs

# yum -y install nfs-util*
# cat /etc/exports
/home 192.168.115.0/24(rw,sync,no_root_squash)
# systemctl start rpcbind
# systemctl start nfs
# showmount -e 127.0.0.1
Export list for 127.0.0.1:
/home 192.168.115.0/24

Kubernetes数据持久化方案
二、使用pod直接挂载nfs
要保证集群内全部的node节点均可以挂载nfs

# cat nfs.yaml 
apiVersion: v1
kind: Pod
metadata:
   name: busybox
spec:
  containers:
   - name : busybox
     image: registry.fjhb.cn/busybox
     imagePullPolicy: IfNotPresent
     command:
      - sleep
      - "3600"
     volumeMounts:
      - mountPath: /busybox-nfsdata
        name: nfsdata
  volumes:
   - name: nfsdata
     nfs:
      server: 192.168.115.6
      path: /home

Kubernetes数据持久化方案
Kubernetes数据持久化方案
三、使用PV和PVC
在实际的使用中,咱们一般会将各存储划分红PV,而后和PVC绑定给pod使用。
PV:PersistentVolume
PVC:PersistentVolumeClaim

PV和PVC的生命周期:
供应准备:经过集群外的存储系统或者公有云存储方案来提供存储持久化支持。
静态提供:管理员手动建立多个PV,供PVC使用。
动态提供:动态建立PVC特定的PV,并绑定。

绑定:用户建立pvc并指定须要的资源和访问模式。在找到可用pv以前,pvc会保持未绑定状态。

使用:用户可在pod中像使用volume同样使用pvc。

释放:用户删除pvc来回收存储资源,pv将变成“released”状态。因为还保留着以前的数据,这些数据须要根据不一样的策略来处理,不然这些存储资源没法被其余pvc使用。

回收(Reclaiming):pv能够设置三种回收策略:保留(Retain),回收(Recycle)和删除(Delete)
保留策略:容许人工处理保留的数据。
删除策略:将删除pv和外部关联的存储资源,须要插件支持。
回收策略:将执行清除操做,以后能够被新的pvc使用,须要插件支持。

PV卷阶段状态:
Available – 资源还没有被PVC使用
Bound – 卷已经被绑定到PVC了
Released – PVC被删除,PV卷处于释放状态,但未被集群回收。
Failed – PV卷自动回收失败

PV卷的访问模式
ReadWriteOnce – 单node的读写 
ReadOnlyMany – 多node的只读 
ReadWriteMany – 多node的读写

建立pv与pvc

# cat nfs-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-001 
spec:
  capacity:
    storage: 5Gi 
  accessModes:
  - ReadWriteMany 
  nfs: 
    path: /home
    server: 192.168.115.6
  persistentVolumeReclaimPolicy: Recycle

Kubernetes数据持久化方案

# cat nfs-pvc.yaml                  
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-data
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

Kubernetes数据持久化方案
在PVC绑定PV时一般根据两个条件来绑定,一个是存储的大小,另外一个就是访问模式。
Kubernetes数据持久化方案
在rc文件中使用PVC

# cat nginx-rc-configmap.yaml   
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels: 
       name: nginx
    spec:
      containers:
      - name: nginx
        image: docker.io/nginx
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-etc
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
         claimName: nfs-data
      - name: nginx-etc
        configMap:
         name: nginxconfig 
         items:
          - key: nginx.conf 
            path: nginx.conf
相关文章
相关标签/搜索