K8s-odf-rook-ceph

From Essential
Jump to navigation Jump to search
#!/bin/bash

#==OPENSHIFT STORAGE/ODF==
#https://red-hat-storage.github.io/ocs-training/training/ocs4/ocs4-install-no-ui.html#_installing_openshift_container_storage
#https://access.redhat.com/articles/5692201

#<syntaxhighlight lang="bash">
#!/bin/bash
#odf.sh <size>
#size(GB)=supérieur à 100 et inférieur à taille disque d'un storage node

#export size=$1
export size=600

. ~/OPENSHIFT_INSTALL_VARS.sh
set -a
majorVersion=$(echo ${versionOPENSHIFT} |sed -rn 's#^([0-9]+\.[0-9]+)(|.[0-9\.]+)$#\1#p' )
set +a

oc get nodes |sed -rn 's#^(.*storage[^[:space:]]+).*$#\1#p' |while read i ;do
  oc adm taint node $i node.ocs.openshift.io/storage="true":NoSchedule
  oc label node $i node-role.kubernetes.io/worker=''
  oc label node $i node-role.kubernetes.io/infra=''
  oc label node $i cluster.ocs.openshift.io/openshift-storage=''
done
cat <<EOF | oc apply -f -
apiVersion: v1
kind: Namespace
metadata:
 name: openshift-local-storage
spec: {}
EOF
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
 name: local-operator-group
 namespace: openshift-local-storage
spec:
 targetNamespaces:
 - openshift-local-storage
EOF
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
 name: local-storage-operator
 namespace: openshift-local-storage
spec:
 channel: "${majorVersion}"  # <-- Channel should be used corresponding to the OCP version being used.
 installPlanApproval: Automatic
 name: local-storage-operator
 source: redhat-operators  # <-- Modify the name of the redhat-operators catalogsource if not default
 sourceNamespace: openshift-marketplace
EOF
sleep 80
cat <<EOF | oc apply -f -
apiVersion: local.storage.openshift.io/v1alpha1
kind: LocalVolumeDiscovery
metadata:
 name: auto-discover-devices
 namespace: openshift-local-storage
spec:
 nodeSelector:
   nodeSelectorTerms:
     - matchExpressions:
       - key: cluster.ocs.openshift.io/openshift-storage
         operator: In
         values:
           - ""
EOF
sleep 40
#oc get localvolumediscoveries -n openshift-local-storage
#oc get localvolumediscoveryresults -n openshift-local-storage
cat << EOT >localvolumeset.yaml
apiVersion: local.storage.openshift.io/v1alpha1
kind: LocalVolumeSet
metadata:
 name: local-block
 namespace: openshift-local-storage
spec:
 nodeSelector:
   nodeSelectorTerms:
     - matchExpressions:
         - key: cluster.ocs.openshift.io/openshift-storage
           operator: In
           values:
             - ""
 storageClassName: localblock
 volumeMode: Block
 fstype: ext4
 maxDeviceCount: 1  # <-- Maximum number of devices per node to be used
 deviceInclusionSpec:
   deviceTypes:
   - disk
#   - part   # <-- Remove this if not using partitions
   deviceMechanicalProperties:
   #- NonRotational #not working with VMWARE, need SSD or NVME
   - Rotational #working with VMWARE
   #minSize: 0Ti   # <-- Uncomment and modify to limit the minimum size of disk used
   #maxSize: 0Ti   # <-- Uncomment and modify to limit the maximum size of disk used
EOT
oc create -f localvolumeset.yaml
sleep 80
#oc get pv -n openshift-local-storage
#Add data disk
#Resize other disks
cat <<EOF | oc apply -f -
apiVersion: v1
kind: Namespace
metadata:
 labels:
   openshift.io/cluster-monitoring: "true"
 name: openshift-storage
spec: {}
EOF
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
 name: openshift-storage-operatorgroup
 namespace: openshift-storage
spec:
 targetNamespaces:
 - openshift-storage
EOF
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
 name: ocs-operator
 namespace: openshift-storage
spec:
 channel: "stable-${majorVersion}"  # <-- Channel should be modified depending on the OCS version to be installed. Please ensure to maintain compatibility with OCP version
 installPlanApproval: Automatic
 name: ocs-operator
 source: redhat-operators  # <-- Modify the name of the redhat-operators catalogsource if not default
 sourceNamespace: openshift-marketplace
EOF
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
 name: odf-operator
 namespace: openshift-storage
spec:
 channel: "stable-${majorVersion}" # <-- Channel should be modified depending on the OCS version to be installed. Please ensure to maintain compatibility with OCP version
 installPlanApproval: Automatic
 name: odf-operator
 source: redhat-operators  # <-- Modify the name of the redhat-operators catalogsource if not default
 sourceNamespace: openshift-marketplace
EOF
sleep 160
cat << EOT >storagecluster.yaml
apiVersion: ocs.openshift.io/v1
kind: StorageCluster
metadata:
 name: ocs-storagecluster
 namespace: openshift-storage
spec:
 manageNodes: false
 resources:
   mds:
     limits:
       cpu: "1" #default 3
       memory: "4Gi" #default 8
     requests:
       cpu: "1" #default 3
       memory: "4Gi" #default 8
 monDataDirHostPath: /var/lib/rook
 storageDeviceSets:
 - count: 1  # <-- Modify count to desired value. For each set of 3 disks increment the count by 1.
   dataPVCTemplate:
     spec:
       accessModes:
       - ReadWriteOnce
       resources:
         requests:
           storage: "${size}Gi"  # <-- This should be changed as per storage size. Minimum 100 GiB and Maximum 4 TiB
       storageClassName: localblock
       volumeMode: Block
   name: ocs-deviceset
   placement: {}
   portable: false
   replica: 2 #default 3, minimum 2
   resources:
     limits:
       cpu: "1" #default 2
       memory: "2Gi"  #default 5
     requests:
       cpu: "1" #default 2
       memory: "2Gi"  #default 5
EOT
oc create -f storagecluster.yaml
while (! kubectl get storageclass |grep ocs-storagecluster-ceph-rbd >/dev/null ) ;do
  sleep 20
done
#oc get pods -n openshift-storage
#oc get csv -n openshift-storage
#debug
#kubectl get events -n openshift-storage
#rollback last action
#oc delete -n openshift-storage storagecluster --all --wait=true
#change default class
#https://kubernetes.io/docs/tasks/administer-cluster/change-default-storage-class/
#kubectl get storageclass
kubectl patch storageclass thin -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
kubectl patch storageclass ocs-storagecluster-ceph-rbd -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
#ENABLE SNAPSHOT DEFAULT&NAME
#https://github.com/rook/rook/blob/91beb549be3720a1278c3b5c67934f46555e1db5/deploy/examples/csi/rbd/snapshotclass.yaml
cat <<EOF |kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
deletionPolicy: Delete
driver: openshift-storage.rbd.csi.ceph.com
kind: VolumeSnapshotClass
metadata:
  name: csi-hostpath-snapclass
  annotations:
    snapshot.storage.kubernetes.io/is-default-class: "true"
parameters:
  clusterID: openshift-storage
  csi.storage.k8s.io/snapshotter-secret-name: rook-csi-rbd-provisioner
  csi.storage.k8s.io/snapshotter-secret-namespace: openshift-storage
EOF

#