By Kan Junbao
The solution for dynamically generating a NAS volume introduced in this article: On an existing file system, a directory is automatically generated, which is defined as the target volume;
Image address: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v1.11.5.4-433631d-aliyun
Resources generated by default:
Name of the generated PV: pvc-${pvc-uid}
Name of the generated Directory: namespace-pvcname-pvname
The following declaration can be made in the annotations of the PVC to customize the name:
The generated PV and directory name are defined below.
annotations:
pv-name-created: replace-user-id
Create alicloud-nas-controller to implement dynamic provider nas pv;
Create alicloud-nas storageclass to provide a template for nas pv provision;
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: alicloud-nas
provisioner: alicloud/nas
reclaimPolicy: Delete
parameters:
drivertype: flexvolume
nfsversion: "4.0"
options: ""
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: alicloud-nas-controller
namespace: kube-system
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: alicloud-nas-controller
spec:
tolerations:
- effect: NoSchedule
operator: Exists
key: node-role.kubernetes.io/master
- effect: NoSchedule
operator: Exists
key: node.cloudprovider.kubernetes.io/uninitialized
serviceAccount: admin
containers:
- name: alicloud-nas-controller
image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v1.11.5.4-433631d-aliyun
imagePullPolicy: Always
volumeMounts:
- mountPath: /persistentvolumes
name: nfs-client-root
env:
- name: NFS_SERVER
value: 154154b095-**.cn-beijing.nas.aliyuncs.com
- name: NFS_PATH
value: /
volumes:
- name: nfs-client-root
flexVolume:
driver: alicloud/nas
options:
path: /
server: 154154b095-**.cn-beijing.nas.aliyuncs.com
vers: "4.0"
StorageClass usage instructions:
drivertype: used to indicate the storage type of the generated PV. NFS and Flexvolume are available.
nfs: the default option, indicating that the K8S native NFS driver is used for mounting;
flexvolume: indicates that the Flexvolume NAS driver provided by Alibaba Cloud is used for mounting;
nfsversion: the version used to mount NAS, and 3 and 4.0 are supported. The default is 4.0;
When drivertype is Flexvolume, it is configured here;
When drivertype is NFS, it is configured through mountOptions;
options: configurable options for mounting NFS;
When drivertype is Flexvolume, it is configured here;
When drivertype is NFS, it is configured through mountOptions;
StorageClass example:
## Use the NFS driver provided by Kubernetes, and configure mountOptions. The reclaimPolicy is Delete;
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: alicloud-nas-nfs
mountOptions:
- vers=4.0
- noresvport
provisioner: alicloud/nas
reclaimPolicy: Delete
## Use the Flexvolume NAS driver provided by Alibaba Cloud to configure the NAS version and options;
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: alicloud-nas-flex
provisioner: alicloud/nas
reclaimPolicy: Delete
parameters:
drivertype: flexvolume
nfsversion: "3"
options: "noresvport"
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: replace-user-id
annotations:
pv-name-created: replace-user-id
spec:
storageClassName: alicloud-nas
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: "deploy-nas"
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: deploy-nas
spec:
containers:
- name: "nginx"
image: "nginx"
volumeMounts:
- name: pvc-nas
mountPath: "/data"
volumes:
- name: pvc-nas
persistentVolumeClaim:
claimName: replace-user-id
Run:
# userID="hello-123"
# cat deploy.yaml | sed "s/replace-user-id/\"$userID\"/g" | kubectl create -f -
# kubectl get pod | grep deploy-nas
deploy-nas-85696b6bfc-t5dmh 1/1 Running 0 28m
# kubectl get pvc | grep hell
hello-123 Bound hello-123 5Gi RWX alicloud-nas-flex 28m
# kubectl get pv | grep hell
hello-123 5Gi RWX Delete Bound default/hello-123 alicloud-nas-flex 28m
# View the generated directory under the NAS directory:
# ls -l | grep hello
drwxrwxrwx 2 root root 4096 Feb 19 09:58 hello-123
If volumeTemplateClaim is used, pv-name-created is not supported to configure the PV name;
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: web
spec:
replicas: 2
serviceName: "nginx"
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
volumeMounts:
- mountPath: "/data"
name: pvc-sts
volumeClaimTemplates:
- metadata:
name: pvc-sts
spec:
accessModes:
- ReadWriteOnce
storageClassName: alicloud-nas-flex
resources:
requests:
storage: 2Gi
View the application after creation:
# kubectl get pod | grep web
web-0 1/1 Running 0 7s
web-1 1/1 Running 0 4s
# kubectl get pvc | grep web
pvc-sts-web-0 Bound pvc-65ab251a-33ec-11e9-a151-00163e066784 2Gi RWO alicloud-nas-flex 13m
pvc-sts-web-1 Bound pvc-8437c50e-33ed-11e9-a151-00163e066784 2Gi RWO alicloud-nas-flex 5m
# kubectl get pv | grep web
pvc-65ab251a-33ec-11e9-a151-00163e066784 2Gi RWO Delete Bound default/pvc-sts-web-0 alicloud-nas-flex 13m
pvc-8437c50e-33ed-11e9-a151-00163e066784 2Gi RWO Delete Bound default/pvc-sts-web-1 alicloud-nas-flex 5m
# View the generated directory under the NAS directory:
# ls -l | grep sts
drwxrwxrwx 2 root root 4096 Feb 19 10:16 default-pvc-sts-web-0-pvc-65ab251a-33ec-11e9-a151-00163e066784
drwxrwxrwx 2 root root 4096 Feb 19 10:24 default-pvc-sts-web-1-pvc-8437c50e-33ed-11e9-a151-00163e066784
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: replace-user-id
annotations:
pv-name-created: replace-user-id
spec:
storageClassName: alicloud-nas-flex
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
apiVersion: v1
kind: Pod
metadata:
name: "nas-pod"
spec:
containers:
- name: "nginx"
image: "nginx"
volumeMounts:
- name: pvc-nas
mountPath: "/data"
volumes:
- name: pvc-nas
persistentVolumeClaim:
claimName: replace-user-id
# userID="pod-123"
# cat pod.yaml | sed "s/replace-user-id/\"$userID\"/g" | kubectl create -f -
# kubectl get pod | grep pod
nas-pod 1/1 Running 0 32s
# kubectl get pvc | grep pod
pod-123 Bound pod-123 5Gi RWX alicloud-nas-flex 44s
# kubectl get pv | grep pod
pod-123 5Gi RWX Delete Bound default/pod-123 alicloud-nas-flex 48s
# ls -l | grep pod
drwxrwxrwx 2 root root 4096 Feb 19 10:54 pod-123
Quickly and Automatically Build Container Images Using Serverless Kubernetes and Kaniko
Dynamically Update Routing Configurations through Alibaba Cloud K8S Ingress Controller
164 posts | 30 followers
FollowAlibaba Developer - April 2, 2020
Alibaba Developer - February 1, 2021
Alibaba Developer - June 17, 2020
Alibaba Clouder - February 10, 2021
Alibaba Developer - August 18, 2020
Alibaba Developer - April 3, 2020
164 posts | 30 followers
FollowSimple, scalable, on-demand and reliable network attached storage for use with ECS instances, HPC and Container Service.
Learn MorePlan and optimize your storage budget with flexible storage services
Learn MoreA cost-effective, efficient and easy-to-manage hybrid cloud storage solution.
Learn MoreProvides scalable, distributed, and high-performance block storage and object storage services in a software-defined manner.
Learn MoreMore Posts by Alibaba Container Service