You can use Alibaba Cloud static volumes directly or through PVs and PVCs.
Prerequisites
To use a disk as a volume, you must create the disk in the ECS console first. For
more information about how to create a disk, see
Create a pay-as-you-go disk .
Directly as volumes
Use the following disk-deploy.yaml file to create a Pod:
Create the disk-deploy.yaml file with the following content.
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: d-wz9f6l6nm0uvazgh7y17
mountPath: /data
volumes:
- name: "d-wz9f6l6nm0uvazgh7y17"
flexVolume:
driver: "alicloud/disk"
fsType: "ext4"
options:
volumeId: "d-wz9f6l6nm0uvazgh7y17"
Run the following command to create a Pod:
kubectl apply -f disk-deploy.yaml
Note
In the volumes
section, we recommend that you set the name
and volumeId
to the same value.
If your cluster is deployed across multiple zones, you need to schedule the Pod and
disk to the same zone.
You need to add a nodeSelector for the Pod as follows:
nodeSelector:
failure-domain.beta.kubernetes.io/zone: cn-hangzhou-b
Through PVs and PVCs
Use a disk to create a PV
You can create a PV through the console or by using a YAML file.
Create a PV by using a YAML file
Use the following
disk-pv.yaml file to create a PV:
apiVersion: v1
kind: PersistentVolume
metadata:
name: d-bp1j17ifxfasvts3****
labels:
failure-domain.beta.kubernetes.io/zone: cn-hangzhou-b
failure-domain.beta.kubernetes.io/region: cn-hangzhou
spec:
capacity:
storage: 20Gi
storageClassName: disk
accessModes:
- ReadWriteOnce
flexVolume:
driver: "alicloud/disk"
fsType: "ext4"
options:
volumeId: "d-bp1j17ifxfasvts3****"
Note The name
of the PV must be the same as the disk ID, volumeId
.
Create a PV through the console
Log on to the Container Service console .
In the left-side navigation pane, choose to go to the PVs and PVCs page.
On the Persistent Volumes tab, select a cluster and click Create in the upper-right corner.
Set the parameters in the Create PV dialog box.
PV Type : In this example, set the PV type to Cloud Disk .
Access Mode : Default is ReadWriteOnce.
Disk ID : Select a disk that is in the same region and zone as your cluster.
File System Type : Select the data type in which data is stored in the disk. Supported data types include
ext4, ext3, xfs, and vfat. Default is ext4.
Label : Add labels for the PV.
After the configuration is complete, click Create .
Create a PVC
Use the following
disk-pvc.yaml file to create a PVC:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-disk
spec:
accessModes:
- ReadWriteOnce
storageClassName: disk
resources:
requests:
storage: 20Gi
Create a Pod
Use the following disk-pod.yaml file to create a Pod:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: pvc-disk
mountPath: /data
volumes:
- name: pvc-disk
persistentVolumeClaim:
claimName: pvc-disk