In this tutorial, we will explore some of the most important and basic Kubernetes commands for yamls
For Kubernetes 1.18 & above Versions Only
Kubernetes commands to generate Pod Yamls
Generate pod yaml file
ubuntu@lb:~$ kubectl run nginx --image=nginx --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
ubuntu@lb:~$
Generate pod yaml file with restartPolicy: Never
ubuntu@lb:~$ kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
ubuntu@lb:~$
Generate pod yaml file with labels app=web and env=dev
ubuntu@lb:~$ kubectl run nginx --image=nginx -l="app=web,env=dev" --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
app: web
env: dev
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
ubuntu@lb:~$
Generate pod yaml with env variables env hello=world and me=aarav
ubuntu@lb:~$ kubectl run nginx --image=nginx --env="hello=world" --env="me=aarav" --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- env:
- name: hello
value: world
- name: me
value: aarav
image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
ubuntu@lb:~$
Generate pod yaml with below parameters:
- env variable hello=world
- labels app=web
- cpu limit 200m
- memory 250Mi
- Pod should restart on Failure
ubuntu@lb:~$ kubectl run nginx --image=httpd --restart=OnFailure --env="hello=world" -l="app=web" --limits="cpu=200m,memory=150Mi" --dry-run=client -o yaml
Flag --limits has been deprecated, has no effect and will be removed in 1.24.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
app: web
name: nginx
spec:
containers:
- env:
- name: hello
value: world
image: httpd
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: OnFailure
status: {}
ubuntu@lb:~$
Generate pod yaml file & Service yaml file together
ubuntu@lb:~$ kubectl run nginx --image=nginx --port=80 --expose --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
name: nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx
status:
loadBalancer: {}
---
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
ubuntu@lb:~$
Create Pod
## Note: remove --dry-run -o yaml from all above commands to create pods ex as below.
ubuntu@lb:~$ kubectl run nginx --image nginx
pod/nginx created
ubuntu@lb:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 9s
ubuntu@lb:~$
Kubernetes commands to Generate Deployment Yamls
ubuntu@lb:~$ kubectl create deployment apache --image httpd --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: apache
name: apache
spec:
replicas: 1
selector:
matchLabels:
app: apache
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: apache
spec:
containers:
- image: httpd
name: httpd
resources: {}
status: {}
ubuntu@lb:~$
Deployments
ubuntu@lb:~$ kubectl create deployment apache --image httpd
deployment.apps/apache created
ubuntu@lb:~$
##Verify the deployment
ubuntu@lb:~$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
apache 1/1 1 0 11s
ubuntu@lb:~$
## List the pods of the nginx deployment
ubuntu@lb:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
apache-6d96f8c8d-72zw9 1/1 Running 0 17s
nginx 1/1 Running 0 3m50s
ubuntu@lb:~$
Scale deployment named apache to 3 replicas
ubuntu@lb:~$ kubectl scale deploy apache --replicas=3
deployment.apps/apache scaled
## List the deployment
ubuntu@lb:~$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
apache 3/3 3 3 4m43s
## List the pods for apache deployment
ubuntu@lb:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
apache-6d96f8c8d-2bm9d 1/1 Running 0 107s
apache-6d96f8c8d-686hp 1/1 Running 0 107s
apache-6d96f8c8d-72zw9 1/1 Running 0 6m16s
Replication Controller
Create a replication controller named Tomcat
vim replicaton-ctrl.yml
kubectl create -f replicaton-ctrl.yml --dry-run=client -o yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: tomcat
namespace: default
spec:
replicas: 3
selector:
app: tomcat
template:
metadata:
labels:
app: tomcat
name: tomcat
spec:
containers:
- image: tomcat
name: tomcat-cont
ports:
- containerPort: 8080
kubectl create -f replicaton-ctrl.yml
##List the replication contoller
kubectl get rc
NAME DESIRED CURRENT READY AGE
tomcat 3 3 3 4m24s
Scale Replication controller
kubectl scale rc tomcat --replicas 5
replicationcontroller/tomcat scaled
## List rc
kubectl get rc tomcat
NAME DESIRED CURRENT READY AGE
tomcat 5 5 5 7m46s
Replicasets
Create a Replicaset named httpd
vi rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: httpd
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: rep1
image: httpd
┌──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl create -f rs.yml
replicaset.apps/httpd created
##List the replicasets
┌──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl get rs
NAME DESIRED CURRENT READY AGE
httpd 3 3 3 2m3s
Scale the replicaset to 5
┌──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl scale rs httpd --replicas 5
replicaset.apps/httpd scaled
##List replicasets
┌──(ved㉿lb)-[~]
└─$ kubectl get rs httpd
NAME DESIRED CURRENT READY AGE
httpd 5 5 5 3m2s
Statefulsets
Create a Statefulset
(ved㉿lb)-[~/kuber-yamls]└─$ vim statefulset.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx
spec:
replicas: 2
serviceName: nginx
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx-cont
image: nginx
ports:
- containerPort: 8080
┌──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl create -f statefulset.yml
statefulset.apps/nginx created
#List the statefulsets
┌──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl get statefulsets
NAME READY AGE
nginx 2/2 53s
Scale statefulesets to 5
──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl scale statefulsets nginx --replicas=5
statefulset.apps/nginx scaled
#List the statefulsets after scaling
┌──(ved㉿lb)-[~/kuber-yamls]
└─$ kubectl get statefulset nginx
NAME READY AGE
nginx 5/5 4m4s
Conclusion
We have explore different Kubernetes imperative command and Declarative yaml in this tutorial. Stay tuned for more K8’s.