kubernetes 集群不会为你处理数据的存储,我们可以为数据库挂载一个磁盘来确保数据的安全。
你可以选择云存储、本地磁盘、NFS。
描述卷的具体信息,例如磁盘大小,访问模式。文档,类型,Local 示例
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongodata
spec:
capacity:
storage: 2Gi
volumeMode: Filesystem # Filesystem(文件系统) Block(块)
accessModes:
- ReadWriteOnce # 卷可以被一个节点以读写方式挂载
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /root/data
nodeAffinity:
required:
# 通过 hostname 限定在某个节点创建存储卷
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node2
对存储需求的一个申明,可以理解为一个申请单,系统根据这个申请单去找一个合适的 PV
还可以根据 PVC 自动创建 PV。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodata
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "local-storage"
resources:
requests:
storage: 2Gi
kubectl rollout restart statefulset mongodb
把节点上的一个目录挂载到 Pod,但是已经不推荐使用了,文档
配置方式简单,需要手动指定 Pod 跑在某个固定的节点。
仅供单节点测试使用;不适用于多节点集群。
minikube 提供了 hostPath 存储,文档
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
serviceName: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongo
image: mongo:4.4
# IfNotPresent 仅本地没有镜像时才远程拉,Always 永远都是从远程拉,Never 永远只用本地镜像,本地没有则报错
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /data/db # 容器里面的挂载路径
name: mongo-data # 卷名字,必须跟下面定义的名字一致
volumes:
- name: mongo-data # 卷名字
hostPath:
path: /data/mongo-data # 节点上的路径
type: DirectoryOrCreate # 指向一个目录,不存在时自动创建
[root@VM-79-37-centos ~/work/test-k8s/yaml/storage]# kubectl apply -f ./mongo.yaml
statefulset.apps/mongodb configured
[root@VM-79-37-centos ~/work/test-k8s/yaml/storage]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/mongodb-0 1/1 Running 0 1s
pod/mongodb-client 1/1 Running 0 7m7s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9m32s
service/mongodb ClusterIP None <none> 27017/TCP 7m27s
NAME READY AGE
statefulset.apps/mongodb 1/1 7m27s
[root@VM-79-37-centos ~]# docker exec -it minikube /bin/sh
# ls
Release.key boot dev etc kic.txt lib lib64 media opt root sbin sys usr
bin data docker.key home kind lib32 libx32 mnt proc run srv tmp var
# cd data
# ls
mongo-data
# cd mongo-data
# ls
WiredTiger _mdb_catalog.wt index-1-7095991055263109797.wt mongod.lock
WiredTiger.lock collection-0-7095991055263109797.wt index-3-7095991055263109797.wt sizeStorer.wt
WiredTiger.turtle collection-2-7095991055263109797.wt index-5-7095991055263109797.wt storage.bson
WiredTiger.wt collection-4-7095991055263109797.wt index-6-7095991055263109797.wt
WiredTigerHS.wt diagnostic.data journal
switched to db test
db.users.save({'_id':'easydoc','name':'易文档'}) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "easydoc" })
db.users.find() { "_id" : "easydoc", "name" : "易文档" }
结论: 数据依然存在
[root@master data]# cd /root
[root@master ~]# ls
Changelog
[root@master ~]# mkdir data
[root@VM-0-14-centos storage]# kubectl apply -f ./sc.yaml
storageclass.storage.k8s.io/local-storage created
[root@VM-0-14-centos storage]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
cbs (default) com.tencent.cloud.csi.cbs Delete Immediate false 6h42m
local-storage kubernetes.io/no-provisioner Delete WaitForFirstConsumer false 18s
[root@master storage]# kubectl apply -f ./pv.yaml
persistentvolume/mongodata created
[root@master storage]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mongodata 2Gi RWO Delete Available local-storage 9s
mongodata-pv 10Gi RWO Retain Bound default/mongodata-pvc cbs 8h
[root@master storage]# kubectl apply -f ./pvc.yaml
persistentvolumeclaim/mongodata created
[root@master storage]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mongodata Pending local-storage 5s
mongodata-pvc Bound mongodata-pv 10Gi RWO cbs 8h
[root@master storage]# kubectl delete -f ./mongo.yaml
statefulset.apps "mongodb" deleted
service "mongodb" deleted
[root@master storage]# kubectl apply -f ./mongo.yaml
statefulset.apps/mongodb created
service/mongodb created
[root@master storage]# kubectl describe pod mongodb-0 -n default
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。