EKS创建ebs存储卷

1、创建ebs卷

也可以aws命令创建

bash 复制代码
# aws ec2 create-volume --availability-zone ap-east-1a --size 100 --volume-type gp3
{
    "AvailabilityZone": "ap-east-1a",
    "Encrypted": false,
    "VolumeType": "gp3",
    "VolumeId": "vol-04db7b8331034440d",
    "State": "creating",
    "Iops": 3000,
    "SnapshotId": "",
    "CreateTime": "2017-01-04T03:53:00.298Z",
    "Size": 100
}

记住VolumeId:vol-04db7b8331034440d

2、创建K8S 存储类

存储类yaml文件

yaml 复制代码
# cat storgeclass2.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ebs3
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
parameters:
  type: gp3
  fsType: ext4

创建并查询存储类

bash 复制代码
[root@ip-10-0-7-211 ~]# kubectl apply -f storgeclass2.yaml 
storageclass.storage.k8s.io/ebs3 created
[root@ip-10-0-7-211 ~]# kubectl get StorageClass
NAME            PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
ebs3            ebs.csi.aws.com         Delete          Immediate              false                  39s

3、创建 K8S 中的 Persistent Volume (PV)

编写yaml文件

bash 复制代码
cat aws-pv3.yaml 
apiVersion: "v1"
kind: "PersistentVolume"
metadata:
  name: "aws-pv3" 
  labels:
    type: amazonEBS
spec:
  capacity:
    storage: "10Gi" 
  accessModes:
    - ReadWriteOnce
  storageClassName: ebs3
  awsElasticBlockStore: 
    fsType: "ext4" 
    volumeID: "vol-02902e667e038a442"

创建pv

bash 复制代码
# kubectl apply -f aws-pv3.yaml 
persistentvolume/aws-pv3 created

查看创建状态

bash 复制代码
[root@ip-10-0-7-211 ~]# kubectl get PersistentVolume
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM              STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE                        51m
aws-pv3   10Gi       RWO            Retain           Available                      ebs3           <unset>                          25s

3、创建 Persistent Volume Claim

创建 Persistent Volume Claim (PVC) 和之前创建的 PV 进行绑定,K8S 中的 POD 通过 PVC 来使用 PV

创建文件 pvc.yaml :

yaml 复制代码
# cat pvc3.yaml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: aws-pvc3
  labels:
    type: amazonEBS
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ebs3      
  resources:
    requests:
      storage: 10Gi

通过 kubectl 创建 PVC :

bash 复制代码
# kubectl apply -f pvc3.yaml
persistentvolumeclaim/aws-pvc created

查看创建的 PVC

bash 复制代码
# kubectl get pvc
NAME       STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
aws-pvc3   Bound    aws-pv3   10Gi       RWO            ebs3           <unset>                 4m40s

出现报错

log 复制代码
AttachVolume.Attach failed for volume "aws-pv2" : rpc error: code = Internal desc = Could not attach volume "vol-07b297f1ae13f164a" to node "i-0900340d8108b7fe8": could not attach volume "vol-07b297f1ae13f164a" to node "i-0900340d8108b7fe8": operation error EC2: AttachVolume, https response error StatusCode: 400, RequestID: ecab32b9-1bb5-45df-b669-1ad61836d792, api error InvalidVolume.ZoneMismatch: The volume 'vol-07b297f1ae13f164a' is not in the same availability zone as instance 'i-0900340d8108b7fe8'

在eksnode角色中添加以下策略

bash 复制代码
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
				"ec2:DescribeVolumes",
				"ec2:DescribeVolumeAttachment",            
                "ec2:AttachVolume",
                "ec2:DetachVolume"
            ],
            "Resource": "*"
        }
    ]
}

nginx pod测试

上述操作还是不能实现多挂载,只能配置成读写many,其中storgeclass的volumeBindingMode: Immediate代表多个ec2的pod共同挂载

而且只能改成io1 或者 io2类型的卷

yaml 复制代码
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: io2
provisioner: ebs.csi.aws.com
volumeBindingMode: Immediate
parameters:
  type: io2
  fsType: ext4
apiVersion: v1
kind: PersistentVolume
metadata:
  name: aws-pvio2
  labels:
    type: amazonEBS
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  storageClassName: io2
  awsElasticBlockStore:
    fsType: ext4
    volumeID: vol-09df79d629291e203
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: aws-pvcio2
  labels:
    type: amazonEBS
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: io2
  resources:
    requests:
      storage: 10Gi

以下是gp2格式的

1、创建存储类

存储类文件

yaml 复制代码
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    storageclass.kubernetes.io/is-default-class: 'true'
  name: ebs
parameters:
  fsType: ext4
provisioner: ebs.csi.aws.com
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer

创建存储类

bash 复制代码
kubectl apply -f storageclass.yaml

查询存储类

bash 复制代码
# kubectl get StorageClass 
NAME            PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
ebs (default)   ebs.csi.aws.com         Delete          WaitForFirstConsumer   false                  34m

2、创建 K8S 中的 Persistent Volume (PV)

pv文件

yaml 复制代码
apiVersion: "v1"
kind: "PersistentVolume"
metadata:
  name: "aws-pv2" 
  labels:
    type: amazonEBS
spec:
  capacity:
    storage: "100Gi" 
  accessModes:
    - ReadWriteOnce
  storageClassName: ebs      
  awsElasticBlockStore: 
    fsType: "ext4" 
    volumeID: "vol-07b297f1ae13f164a"

创建pv

bash 复制代码
kubectl apply -f aws-pv.yaml

查询pv

bash 复制代码
# kubectl get PersistentVolume
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM              STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
aws-pv2   100Gi      RWO            Retain           Bound    default/aws-pvc2   ebs            <unset>                          31m

3、创建pvc

pvc文件

yaml 复制代码
apiVersion: v1
metadata:
  name: aws-pvc2
  labels:
    type: amazonEBS
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi

创建pvc

bash 复制代码
kubectl apply -f pvc.yaml

查询pvc

bash 复制代码
# kubectl get pvc
NAME       STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
aws-pvc2   Bound    aws-pv2   100Gi      RWO            ebs            <unset>                 31m

4、在nginx pod实例测试

yaml 复制代码
          volumeMounts:
            - mountPath: /usr/share/nginx/html/
              name: volume-87it6
              subPath: html/
      terminationGracePeriodSeconds: 30
      volumes:
        - name: volume-87it6
          persistentVolumeClaim:
            claimName: aws-pvc2

全文如下

yaml 复制代码
---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    k8s.kuboard.cn/displayName: nginx
  name: nginx
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/name: nginx
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s.kuboard.cn/name: nginx
        pod-template-hash: fd5bc6ffb
    spec:
      containers:
        - image: 'nginx:1.24'
          imagePullPolicy: IfNotPresent
          name: nginx
          ports:
            - containerPort: 80
              name: dfr
              protocol: TCP
          volumeMounts:
            - mountPath: /usr/share/nginx/html/
              name: volume-87it6
              subPath: html/
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
        - name: volume-87it6
          persistentVolumeClaim:
            claimName: aws-pvc2
---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels:
    k8s.kuboard.cn/name: nginx
  name: nginx
  namespace: default
  resourceVersion: '9882'
spec:
  ports:
    - name: dhey2i
      nodePort: 30000
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    k8s.kuboard.cn/name: nginx
  sessionAffinity: None
  type: NodePort

我们启动nginx对应的deployment所在的pod主机,找到对应的路径的文件,添加测试内容pvc-ebs-test

bash 复制代码
[root@ip-10-0-128-90 ~]# cd /var/lib/kubelet/pods/f8f08bd2-8945-49eb-804b-dd98569f5fde/volumes/kubernetes.io~csi/aws-pv2/mount/html/
[root@ip-10-0-128-90 html]# vim index.html
pvc-ebs-test

远程测试

bash 复制代码
# curl http://10.0.142.42
pvc-ebs-test
相关推荐
kainx12 天前
AWS EKS 相关错误修复 - remote error: tls: internal error - CSR pending
kubernetes·云计算·k8s·aws·eks·csr
CloudPilotAI1 个月前
全球最大分类广告商的Karpenter实践:减负运维、减少中断、每月省21万(上)
kubernetes·eks·finops·karpenter
yunson_Liu8 个月前
helm安装 AWS Load Balancer Controller
eks·亚马逊云
亚林瓜子10 个月前
AWS EKS(AWS云里面的K8S)
kubernetes·云计算·aws·eks
kuangxiang_panpan1 年前
AWS EKS1.26+kubesphere3.4.1
云计算·k8s·kubesphere·aws·eks
一头小山猪1 年前
亚马逊云科技 re:Invent 大会 - ElastiCache Serverless模式来袭
科技·缓存·云原生·serverless·亚马逊云
一头小山猪1 年前
亚马逊云科技 re:Invent 大会 - S3 对象存储华丽升级
科技·对象存储·亚马逊云
阿Q说代码1 年前
通过使用Amazon Neptune来预测电影类型初体验
图数据库·amazon neptune·亚马逊云
小楼看风云1 年前
AWS EKS 集群自动扩容 Cluster Autoscaler
eks