Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Last active July 15, 2022 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshavardhana/cd1e3cdfe7c286398d8e6cdc07b8d416 to your computer and use it in GitHub Desktop.
Save harshavardhana/cd1e3cdfe7c286398d8e6cdc07b8d416 to your computer and use it in GitHub Desktop.
Local PV automation
[pvs]
# hostname local to this system on k8s
node=host1
# capacity for all devices this is common for all devices.
capacity=400Gi
# list of devices to format in command separated list.
devices=/dev/sdb
[minio]
release=RELEASE.2022-07-15T03-44-22Z
# this capacity must match device capacity.
capacity=400Gi
from optparse import OptionParser
import configparser
try:
import sh
except Exception:
print("sh package missing please install via 'pip install sh'")
provisioner = """
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: minio-local-pv
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
"""
localpv = """
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: minio-pv{diski}
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
capacity:
storage: {capacity}
local:
path: /minio/disk{diski}
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- {node}
persistentVolumeReclaimPolicy: Retain
storageClassName: minio-local-pv
"""
statefulSet = """
---
apiVersion: v1
kind: Service
metadata:
name: minio
spec:
type: NodePort
ports:
- port: 9000
selector:
app: minio
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: minio
spec:
updateStrategy:
type: RollingUpdate
podManagementPolicy: "Parallel"
serviceName: "minio"
replicas: 1
selector:
matchLabels:
app: minio
template:
metadata:
labels:
app: minio
spec:
# imagePullSecrets:
# - name: regcred
containers:
- name: minio
image: quay.io/minio/minio:{release}
imagePullPolicy: Always
args:
- server
- /export
ports:
- containerPort: 9000
volumeMounts:
- name: minio-pv1
mountPath: /export
volumeClaimTemplates:
- metadata:
name: minio-pv1
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: {capacity}
storageClassName: minio-local-pv
"""
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-f", "--config", dest="filename",
default="config.ini", help="deployment config file",
metavar="FILE")
def format_mount(device, label):
print("minio: formatting '{0}' with xfs, label='{1}'".format(device, label))
sh.mkfs("-t", "xfs", "-L", "{0}".format(label), "-f", device)
sh.mkdir("-p", "/minio/{0}".format(label))
sh.tee(sh.echo("LABEL={0}".format(label), "/minio/{0}".format(label), "xfs", "noatime", "0", "0"), "-a", "/etc/fstab")
print("minio: mounting '{0}' at '/minio/{1}'".format(device, label))
sh.mount("-a")
def main():
(opts, args) = parser.parse_args()
config = configparser.ConfigParser()
config.read(opts.filename)
devices = config['pvs']['devices'].split(',')
total_devices = 1
for device in devices:
format_mount(device.strip(), "disk{0}".format(total_devices))
total_devices += 1
print("minio: generating local PV config 'minio-pv.yaml'")
with open('minio-pv.yaml', 'w') as f:
f.write(provisioner)
node = config['pvs']['node']
ni = 0
for i in range(1, total_devices):
f.write(localpv.format(ni=ni, node=node.strip(), diski=i, capacity=config['pvs']['capacity']))
ni += 1
print("minio: generating statefulset config 'minio-ss.yam'")
with open('minio-ss.yaml', 'w') as f:
f.write(statefulSet.format(release=config["minio"]["release"], capacity=config["minio"]["capacity"]))
print("minio: applying local PV config")
sh.kubectl("apply", "-f", "minio-pv.yaml")
print("minio: deployming MinIO statefulset")
sh.kubectl("apply", "-f", "minio-ss.yaml")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment