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

4/19/2021

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.

-- Ming Hsieh
kubernetes

2 Answers

4/19/2021

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.

-- Gupta
Source: StackOverflow

5/4/2021
<h2>TL;DR:</h2>

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
<h2>Story:</h2>

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.

-- Ming Hsieh
Source: StackOverflow