Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be the fastest way to generate persistentVolume, persistentVolumeClaim, and storageClass correct yaml file from kubectl?

Tags:

kubernetes

Two years ago while I took CKA exam, I already have this question. At that time I only could do was to see k8s.io official documentation. Now just curious on generating pv / pvc / storageClass via pure kubectl cli. What I look for is similar to the similar logic as deployment, for example:

$ kubectl create deploy test --image=nginx --port=80 --dry-run -o yaml
W0419 23:54:11.092265   76572 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: test
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: test
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
status: {}

Or similar logic to run a single pod:

$ kubectl run test-pod --image=nginx --port=80 --dry-run -o yaml
W0419 23:56:29.174692   76654 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: nginx
    name: test-pod
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

So what should I type in order to generate pv / pvc / storageClass yaml? The current only declarative fastest way:

cat <<EOF | kubectl create -f -
<PV / PVC / storageClass yaml goes here>
EOF

Edited: Please note that I look any fast way to generate correct pv / pvc / storageClass template without remembering specific syntax thru cli, and not necessary via kubectl.

like image 573
Ming Hsieh Avatar asked Oct 31 '25 19:10

Ming Hsieh


2 Answers

TL;DR:

Look, bookmark and build index your brain in all yaml files in this Github directory (content/en/examples/pods) before the exam. 100% legal according to CKA curriculum.

https://github.com/kubernetes/website/tree/master/content/en/examples/pods/storage/pv-volume.yaml

Then use this form during exam:

kubectl create -f https://k8s.io/examples/pods/storage/pv-volume.yaml

In case you need edit and apply:

# curl
curl -sL https://k8s.io/examples/pods/storage/pv-volume.yaml -o /your/path/pv-volume.yaml
# wget
wget -O /your/path/pv-volume.yaml https://k8s.io/examples/pods/storage/pv-volume.yaml

vi /your/path/pv-volume.yaml
kubectl apply -f /your/path/pv-volume.yaml

Story:

Actually after look around for my own answer, there's an article floating around that suggest me to bookmark these 100% legal pages:

https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-persistentvolume

https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#creating-a-cron-job

https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod/

Note that:

kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml
  1. kubectl could create objects from URL
  2. Where is the original https://k8s.io pointing to?
  3. What else I could benefit from?

Then after digging up the page above "pods/storage/pv-volume.yaml" code, the link points to:

https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/storage/pv-volume.yaml

Which direct to:

https://github.com/kubernetes/website/tree/master/content/en/examples/pods

So https://k8s.io is a shorten uri as well as a http 301 redirect to https://github.com/kubernetes/website/tree/master/content/en to help the exam candidate easy to produce (not copy-n-paste) in the exam terminal.

like image 61
Ming Hsieh Avatar answered Nov 02 '25 11:11

Ming Hsieh


There is no kubectl command to create a resource like PV, PVC, and storage class.

From certificate points of view, you have go over k8.io and look for the PV, PVC, and storage class under the task link.

Under task link, most of the YAML will be the same and for now, this is one of the fastest ways in exam.

like image 28
Gupta Avatar answered Nov 02 '25 13:11

Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!