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
相關文章
相關標籤/搜索