In order to create a pod with an existing EBS volume attached you can do the following there are some steps you need to complete, each of the following resources can be created in your cluster executing command$ kubectl apply -f file-name.yaml
:
1. Create a storage class
This resource is needed in order to specify the provisioner, the volume type and the binding properties that K8s will apply when a persistent volume claim specifies this storage class.
You can have your default storage class so there’s no need to claim it by the name later, however here let’s have a different one from the default because the properties might vary for different use cases.
The most important option here is the Reclaim Policy and it’s set to Retain, having the default option, Delete once the persistent volume claim is removed would cause the EBS volume to be removed as well and we want to keep the volume where the data is so it will be just “released”.
The provisioner specified is a volume plugin shipped with kubernetes.
The VolumeBinding mode ‘Intermediate’ means that not delay is introduced like in cases where further requirements need to be matched, bind is done as persistent volume claim is created.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: example-sc
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
reclaimPolicy: Retain
mountOptions:
- debug
volumeBindingMode: Immediate
2. Add the volume to the cluster by creating a persistentvolume
By means of this resource (pv) you are adding to the Cluster an existing EBS volume that was previously created in AWS account. This might have existing info we want to make available to the container or you might use the same volume from now on for a new database.
kind: PersistentVolume
apiVersion: v1
metadata:
name: test
labels:
type: amazonEBS
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
awsElasticBlockStore:
volumeID: <volume-id>
fsType: ext4
3. Create a persistent volume claim
Creating persistent volume claim you just create a “Request” for storage. This Request will be invoked later when the pod is created and this PVC is specified.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: task-pv-claim
spec:
storageClassName: example-sc
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
4. Create the pod referring the perstistent volume claim created before.
Take a look: ebs-k8s,
USEFUL INFORMATION
Also there are a few EBS volume plugins that could bind the EBS volume directly with container, such as firecamp, flocker, rexray
. Flocker and RexRay could bind one EBS volume to a single container. While, FireCamp is aware of the stateful service and could bind one EBS volume to each container of one service.
With these plugins, you could mount one volume to one container. The containers will not use up the root space, and will not impact each other.
Take a look: ebs-inside-container, ebs-aws.