日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Kubernetes数据持久化方案

發布時間:2025/3/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 : busyboximage: registry.fjhb.cn/busyboximagePullPolicy: IfNotPresentcommand:- sleep- "3600"volumeMounts:- mountPath: /busybox-dataname: datavolumes:- name: dataemptyDir: {}


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 : busyboximage: registry.fjhb.cn/busyboximagePullPolicy: IfNotPresentcommand:- sleep- "3600"volumeMounts:- mountPath: /busybox-dataname: datavolumes:- hostPath:path: /tmpname: data


emptyDir和hostPat很多場景是無法滿足持久化需求,因為在Pod發生遷移的時候,數據都無法進行轉移的,這就需要分布式文件系統的支持。

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

將變量做成configmap

將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



在rc配置文件中使用configmap

# cat nginx-rc-configmap.yaml apiVersion: v1 kind: ReplicationController metadata:name: nginxlabels:name: nginx spec:replicas: 2selector:name: nginxtemplate:metadata:labels: name: nginxspec:containers:- name: nginximage: docker.io/nginxvolumeMounts:- name: nginx-etcmountPath: /etc/nginx/nginx.confsubPath: nginx.confports:- containerPort: 80volumes:- name: nginx-etcconfigMap:name: nginxconfig items:- key: nginx.conf path: nginx.conf # kubectl create -f nginx-rc-configmap.yaml



configmap的信息實際是存儲在etcd中的,可以使用kubectl edit configmap xxx 來對configmap進行修改

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


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


2、使用pod直接掛載nfs
要保證集群內所有的node節點都可以掛載nfs

# cat nfs.yaml apiVersion: v1 kind: Pod metadata:name: busybox spec:containers:- name : busyboximage: registry.fjhb.cn/busyboximagePullPolicy: IfNotPresentcommand:- sleep- "3600"volumeMounts:- mountPath: /busybox-nfsdataname: nfsdatavolumes:- name: nfsdatanfs:server: 192.168.115.6path: /home



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: /homeserver: 192.168.115.6persistentVolumeReclaimPolicy: Recycle

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


在PVC綁定PV時通常根據兩個條件來綁定,一個是存儲的大小,另一個就是訪問模式。

在rc文件中使用PVC

# cat nginx-rc-configmap.yaml apiVersion: v1 kind: ReplicationController metadata:name: nginxlabels:name: nginx spec:replicas: 2selector:name: nginxtemplate:metadata:labels: name: nginxspec:containers:- name: nginximage: docker.io/nginxvolumeMounts:- name: nginx-datamountPath: /usr/share/nginx/html- name: nginx-etcmountPath: /etc/nginx/nginx.confsubPath: nginx.confports:- containerPort: 80volumes:- name: nginx-datapersistentVolumeClaim:claimName: nfs-data- name: nginx-etcconfigMap:name: nginxconfig items:- key: nginx.conf path: nginx.conf

總結

以上是生活随笔為你收集整理的Kubernetes数据持久化方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。