我应该能够挂载一个本地目录作为在minikube/kubernetes下运行的mysql docker容器的持久卷数据文件夹。
我直接使用 Docker 实现共享卷运行它没有任何问题,但在 kubernetes 下运行它,我无法
osx 10.13.6
Docker Desktop Community version 2.0.0.2 (30215)
Channel: stable
0b030e17ca
Engine 18.09.1
Compose: 1.23.2
Machine 0.16.1
Kubernetes v1.10.11
minikube version: v0.33.1
重现行为的步骤
install docker-for-mac and enable kubernetes
在 Mac 上创建一个要共享为持久卷存储的目录,例如
sudo mkdir -m 777 -p /Users/foo/mysql
部署.yml
# For use on docker for mac
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: localstorage
provisioner: docker.io/hostpath
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
app: mysql
name: mysql-pvc
spec:
storageClassName: localstorage
accessModes:
- ReadWriteOnce
- ReadOnlyMany
resources:
requests:
storage: 20Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
type: local
spec:
storageClassName: localstorage
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
- ReadOnlyMany
hostPath:
# this is the path on laptop?
path: "/Users/foo/mysql"
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
type: NodePort
selector:
app: mysql-service
ports:
- port: 3306
targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-server
labels:
app: mysql-server
spec:
selector:
matchLabels:
app: mysql-server
template:
metadata:
labels:
app: mysql-server
spec:
containers:
- name: mysql-server
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: ""
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "yes"
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-pvc
# this is the path on the pod container?
mountPath: "/mnt/data"
volumes:
- name: mysql-pvc
persistentVolumeClaim:
claimName: mysql-pvc
我可以启动 pod,通过 mysql 客户端连接,创建一个数据库,但是当 pod 关闭时,数据不会持久化,并且没有写入挂载的数据文件夹
kubectl create -f deployment.yml
kubectl port-forward mysql-server-6b64c4545f-kp7h9 3306:3306
mysql -h 127.0.0.1 -P 3306 -u root
mysql> create database foo;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| foo |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
....
删除部署:
kubectl delete sc "localstorage"
kubectl delete persistentvolume "mysql-pv"
kubectl delete persistentvolumeclaim "mysql-pvc"
kubectl delete service "mysql-service"
kubectl delete deployment.apps "mysql-server"
kubectl delete events --all
重新创建并再次连接,如上所述
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
您必须创建一个持久卷,将存储类定义为本地,然后将其映射到本地路径。
创建存储类
storage-class.yml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
然后运行kubectl create -f storage-class.yml
创造持久价值
pv-local.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /mnt/data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- cka
创建永久性卷运行kubectl create -f pv-sdc.yml
最后一个,创建永久性卷声明
聚氯乙烯1.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc1
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 10Gi
创建运行kubectl create -f pvc1.yml
的持久卷 clain
要列出持久性值,请运行kubectl get pv
。您应该会看到一些输出,例如
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
local-pv 10Gi RWO Retain Available local-storage 10s
一旦节点使用它,持久卷将可用。
这篇文章可能会对你有更多的帮助。