StorageClass

介绍

StorageClass为管理员提供了描述存储类的方法,不同的类型可能会映射到不同的服务质量等级或者备份策略,或是由管理员制定的任意策略。kubernetes本身并不清楚各种类代表的什么,这个类的概念在其他存储系统中有时被称为配置文件。

StorageClass资源

每个StorageClass都包含provisionerparametersreclaimPolicy字段,这些字段会在StorageClass需要动态分配PV是会使用到。

apiVersion: v1  #服务账号
kind: ServiceAccount
metadata:
  name: test-nfs-client-provisioner
  namespace: kube-system

---
kind: ClusterRole  #角色
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: test-nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]

---
kind: ClusterRoleBinding #绑定
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: test-run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: test-nfs-client-provisioner
    namespace: kube-system 
roleRef:
  kind: ClusterRole
  name: test-nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io

---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-provisioner-test
  namespace: kube-system
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-provisioner-test
  template:
    metadata:
      labels:
        app: nfs-provisioner-test
    spec:
      serviceAccountName: test-nfs-client-provisioner
      containers:
        - name: test-nfs-client-provisioner
          image: jmgao1983/nfs-client-provisioner:latest
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: nfs-client-test
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: nfs-provisioner-test  # 此处供应者名字供storageclass调用
            - name: NFS_SERVER
              value: 192.168.2.111   # 填入NFS的地址
            - name: NFS_PATH
              value: /data/nfs-test   # 填入NFS挂载的目录
      volumes:
        - name: nfs-client-test
          nfs:
            server: 192.168.2.111   # 填入NFS的地址
            path: /data/nfs-test   # 填入NFS挂载的目录

---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-test
provisioner: nfs-provisioner-test
# Supported policies: Delete、 Retain , default is Delete
reclaimPolicy: Retain

实例

wordpress.yaml

apiVersion: v1
kind: Service
metadata:
  namespace: nfs-test
  name: wordpress1
  labels:
    app: wordpress
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePode: 30019
  selector:
    app: wordpress
    tier: frontend
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: nfs-test
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  storageClassName: nfs-test
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: nfs-test
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          value: password
        - name: TZ
          value: Asia/Shanghai
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

wordpress-mysql.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  namespace: nfs-test
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: nfs-test
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  storageClassName: nfs-test
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: nfs-test
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        - name: TZ
          value: Asia/Shanghai
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

查看

查看pv

[root@101 workpress]# kubectl get pv|grep nfs-test
pvc-1f429b1a-a39d-4c2e-9af9-1955e68567b8   20Gi       RWO            Delete           Bound    nfs-test/wp-pv-claim          nfs-test                                104m
pvc-7fdca7ff-180c-4548-8ce5-b8942b13cb52   20Gi       RWX            Delete           Bound    nfs-test/mysql-pv-claim       nfs-test                                110m

查看pvc

[root@101 workpress]# kubectl get pvc -n nfs-test
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pv-claim   Bound    pvc-7fdca7ff-180c-4548-8ce5-b8942b13cb52   20Gi       RWX            nfs-test       111m
wp-pv-claim      Bound    pvc-1f429b1a-a39d-4c2e-9af9-1955e68567b8   20Gi       RWO            nfs-test       105m
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇