So as I was studying to get my CKAD certificate, I was kind of worried about forgetting yaml definitions for Kubernetes resources during my exam, I accumulated the list below of some commands needed to generate/create K8s resources using Imperative commands, to serve as a reference for me and for anyone who is preparing for the CKAD exam.
kubectl run mypod1 --generator=run-pod/v1 --image nginx
This will create a single Pod named mypod1 with Nginx as an image.
kubectl run mypod1 --generator=run-pod/v1 --image nginx --dry-run -o yaml >mypod.yaml
Note the “–dry-run” flag which informs K8s not to create the resource, and the “-o yaml” which output a yaml definition, this will generate a yaml file named mypod.yaml which contains a Pod named mypod1 with the image set to Nginx.
kubectl create deployment mydeploy --image nginx
This will create a deployment with replicas set to 1, In order to scale this deployment later we can run the following command
kubectl scale deployment mydeploy --replicas 3
This will scale the deployment pods to 3 instances.
kubectl create deployment mydeploy --image nginx --dry-run -o yaml> mydeploy1.yaml
This will generate a yaml file named mydeploy1.yaml.
kubectl expose deployment mydeploy --type LoadBalancer --name myservice --port 80
this will create a Loadbalancer service named myservice to expose mydeploy on port 80.
kubectl expose deployment mydeploy --type LoadBalancer --name myservice --port 80 --dry-run -o yaml>myservice.yaml
kubectl create configmap mycon1 --from-literal Version=v1 --from-literal Type=FrontEnd
This will create a ConfigMap with the following data:
kubectl create configmap mycon1 --from-literal Version=v1 --from-literal Type=FrontEnd --dry-run -o yaml > mycon1.yaml
This will only generate the yaml file.
Accessing Pod through Bash/PowerShell:
for Linux Pods:
kubectl exec -it mypod1 — /bin/bash
for Windows Pods:
kubectl exec -it mypod1 powershell
using a similar technique you can create or generate yamls for K8s Resources.