Kubernetes數據持久化方案

在開始介紹k8s持久化存儲前,我們有必要了解一下k8s的emptydir和hostpath、configmap以及secret的機制和用途。

1、Emptydir
EmptyDir是一個空目錄,他的生命週期和所屬的 Pod 是完全一致的,EmptyDir主要作用可以在同一 Pod 內的不同容器之間共享工作過程中產生的文件。如果Pod配置了emptyDir類型Volume, Pod 被分配到Node上時候,會創建emptyDir,只要Pod運行在Node上,emptyDir都會存在(容器掛掉不會導致emptyDir丟失數據),但是如果Pod從Node上被刪除(Pod被刪除,或者Pod發生遷移),emptyDir也會被刪除,並且永久丟失。

# 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數據持久化方案
2、Hostpath
Hostpath會把宿主機上的指定卷加載到容器之中,如果 Pod 發生跨主機的重建,其內容就難保證了。這種卷一般和DaemonSet搭配使用。hostPath允許掛載Node上的文件系統到Pod裏面去。如果Pod有需要使用Node上的東西,可以使用hostPath,不過不過建議使用,因爲理論上Pod不應該感知Node的。

# 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發生遷移的時候,數據都無法進行轉移的,這就需要分佈式文件系統的支持。

3、Configmap
鏡像使用的過程中,經常需要利用配置文件、啓動腳本等方式來影響容器的運行方式,如果僅有少量配置,我們可以使用環境變量的方式來進行配置。然而對於一些較爲複雜的配置,k8s提供了configmap解決方案。 
ConfigMap API資源存儲鍵/值對配置數據,這些數據可以在pods裏使用。
ConfigMap跟Secrets類似,但是ConfigMap可以更方便的處理不包含敏感信息的字符串。
當ConfigMap以數據卷的形式掛載進Pod的時,這時更新ConfigMap(或刪掉重建ConfigMap),Pod內掛載的配置信息會熱更新。這時可以增加一些監測配置文件變更的腳本,然後reload對應服務
ConfigMap的API概念上來說是很簡單的。從數據角度來看,ConfigMap的類型只是鍵值組。應用可以從不同角度來配置。在一個pod裏面使用ConfigMap大致有三種方式:
1、命令行參數
2、環境變量
3、數據卷文件

將變量做成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配置文件中使用configmap

# 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進行修改

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

Kubernetes數據持久化方案
4、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支持同時寫操作。

1、首先安裝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數據持久化方案
2、使用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數據持久化方案
3、使用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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章