ceph集群来为kubernetes提供存储。

创建存储类

1、获取admin key

grep key /etc/ceph/ceph.client.admin.keyring |awk '{printf "%s", $NF }'|base64

QVFCZ2ZZOWJ1dGdBQ0JBQXN5dGdLZ1BFOGlsblIzWjJqNVVKMUE9PQ==

2、写入 ceph-secret-admin.yaml

apiVersion: v1
kind: Secret
metadata:
  name: ceph-secret-admin
type: "kubernetes.io/rbd"
data:
  key: QVFCZ2ZZOWJ1dGdBQ0JBQXN5dGdLZ1BFOGlsblIzWjJqNVVKMUE9PQ==

3、创建secret

kubectl  apply -f ceph-secret-admin.yaml
kubectl  get secret
NAME                                 TYPE                                  DATA      AGE
ceph-secret-admin                    kubernetes.io/rbd                     1         6m

4、 修改 rbd-storage-class.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
   name: rbd
provisioner: kubernetes.io/rbd
parameters:
    monitors: 192.168.100.100:6789,192.168.100.101:6789,192.168.100.102:6789
    adminId: admin
    adminSecretName: ceph-secret-admin
    adminSecretNamespace: default
    pool: k8s
    userId: admin
    userSecretName: ceph-secret-admin
    userSecretNamespace: default
    fsType: ext4
    imageFormat: "2"
    imageFeatures: "layering"

5、创建storage class

kubectl apply -f rbd-storage-class.yaml

[root@qd01-stop-cloud001 rbd]# kubectl  get sc
NAME      PROVISIONER         AGE
rbd         kubernetes.io/rbd   4m

创建PVC & PV

6、创建pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: claim
  namespace: default
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: rbd
  resources:
    requests:
      storage: 1Gi

7、查看pvc,显示状态为Bound表示成功

kubectl apply -f pvc.yaml
kubectl  get pvc
NAME      STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
claim    Bound     pvc-130c2445-b4a5-11e8-9d27-782bcb3bb379   1Gi        RWO            slow           13m

测试验证

8、创建pod.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: server
spec:
  replicas: 1
  selector:
    role: server
  template:
    metadata:
      labels:
        role: server
    spec:
      containers:
      - name: server
        image: nginx
        volumeMounts:
          - mountPath: /var/lib/www/html
            name: mypvc
      volumes:
        - name: mypvc
          persistentVolumeClaim:
            claimName: claim

9、查看挂载存储

/dev/rbd0       1G  9.1M  0.98G   1% /var/lib/kubelet/plugins/kubernetes.io/rbd/mounts/k8s-image-kubernetes-dynamic-pvc-198f56b3-b4a5-11e8-97eb-782bcb3bb379

错误处理

如果出错failed to create rbd image: executable file not found in $PATH
参考
可以使用下面的项目来创建存储类

使用external-storage创建存储类

$ git clone https://github.com/kubernetes-incubator/external-storage.git
$ tree external-storage/ceph/rbd/deploy/
├── README.md
├── non-rbac
│   └── deployment.yaml
└── rbac
    ├── clusterrole.yaml
    ├── clusterrolebinding.yaml
    ├── deployment.yaml
    └── serviceaccount.yaml

Install without RBAC roles:
cd $GOPATH/src/github.com/kubernetes-incubator/external-storage/ceph/rbd/deploy
kubectl apply -f ./non-rbac

Install with RBAC roles:
cd $GOPATH/src/github.com/kubernetes-incubator/external-storage/ceph/rbd/deploy
NAMESPACE=default     # change this if you want to deploy it in another namespace
sed -r -i "s/namespace: [^ ]+/namespace: $NAMESPACE/g" ./rbac/clusterrolebinding.yaml ./rbac/rolebinding.yaml
kubectl -n $NAMESPACE apply -f ./rbac

如果secret和provisioner不在同一个namespace中的话,获取secret权限不够。
解决方法:
以下文件添加secrets的权限
external-storage/ceph/rbd/deploy/rbac/clusterrole.yaml

  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "create", "delete"]

 

posted @ 2021-03-16 20:17  fat_girl_spring  阅读(121)  评论(0编辑  收藏  举报