之前文章提到,k8s官方是支持nfs存储的,那么在windows常见的文件共享协议是否也支持呢。答案是肯定的。不过支持的方式是通过CSI接口进行支持的。官方提供的项目是csi-driver-smb
官网:https://github.com/kubernetes-csi/csi-driver-smb
安装
curl -skSL https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/v1.9.0/deploy/install-driver.sh | bash -s v1.9.0 --
国内的环境实测安装有点困难install-driver.sh实际上对应5个yaml文件安装,可以从项目的delploy目录代码中找到
kubectl apply -f rbac-csi-smb.yaml
kubectl apply -f csi-smb-driver.yaml
kubectl apply -f csi-smb-controller.yaml
kubectl apply -f csi-smb-node.yaml
kubectl apply -f csi-smb-node-windows.yaml
这几个yaml文件有4个镜像来自registry.k8s.io,也不是那么好下载,建议从其他地方下载导入再执行apply
registry.k8s.io/sig-storage/smbplugin
registry.k8s.io/sig-storage/csi-provisioner
registry.k8s.io/sig-storage/livenessprobe
registry.k8s.io/sig-storage/csi-node-driver-registrar
安装完后查看pod
kubectl get pod -A |grep smb
可以看到如下结果
kube-system csi-smb-controller-5879b96656-w4d64 3/3 Running 0 145m
kube-system csi-smb-node-bqkkk 3/3 Running 0 145m
说明安装成功
之后是使用过程
创建secret保存smb的用户名密码
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD"
这里将USERNAME改成你的smb用户名PASSWORD 改成你的smb密码,如果有AD域,使用--from-literal domain=DOMAINNAME 指定
编写yaml文件,这里以部署tomcat8为例。
storageclass-smb.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: smb
provisioner: smb.csi.k8s.io
parameters:
source: "//192.168.1.16/AirDisk/新加卷-A/k8sstorage"
# if csi.storage.k8s.io/provisioner-secret is provided, will create a sub directory
# with PV name under source
csi.storage.k8s.io/provisioner-secret-name: "smbcreds"
csi.storage.k8s.io/provisioner-secret-namespace: "default"
csi.storage.k8s.io/node-stage-secret-name: "smbcreds"
csi.storage.k8s.io/node-stage-secret-namespace: "default"
reclaimPolicy: Delete # available values: Delete, Retain
volumeBindingMode: Immediate
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=1001
- gid=1001
这里source改成你自己的路径
tomcat-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tomcat-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 4Gi
storageClassName: "smb"
tomcat-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deploy
spec:
replicas: 2
selector:
matchLabels:
app: tomcat8
template:
metadata:
labels:
app: tomcat8
spec:
containers:
- name: mytomcat8
image: tomcat:8.5.40
ports:
- containerPort: 8080
volumeMounts:
- name: tomcat-persistent-storage
mountPath: /usr/local/tomcat/webapps
volumes:
- name: tomcat-persistent-storage
persistentVolumeClaim:
claimName: tomcat-pvc
---
apiVersion: v1
kind: Service
metadata:
name: tomcatservice
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 30010
selector:
app: tomcat8
安装命令
kubectl apply -f storageclass-smb.yaml
kubectl apply -f tomcat-pvc.yaml
kubectl apply -f tomcat-deploy.yaml
卸载命令
kubectl delete -f tomcat-deploy.yaml
kubectl delete -f tomcat-pvc.yaml
kubectl delete -f storageclass-smb.yaml
访问tomcat
http://192.168.1.10:30010/
默认出现404,把war包拷贝到smb目录就可以正常访问了,我这里把webapp的原有内容拷贝下来了。页面可以看到汤姆猫了
虽然搞定的tomcat的smb存储,但是mysql部署却失败了,很有可能出现在含单引号的文件名和目录名这个问题上。