-
Notifications
You must be signed in to change notification settings - Fork 0
/
useful-kubectl-commands
56 lines (29 loc) · 2.3 KB
/
useful-kubectl-commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Create new deployment yaml
kubectl create deploy --image=nginx nginx --replicas=4 --dry-run=client -o yaml >deployment.yaml
# Create new service yaml
kubectl expose pod nginx --name nginx-service --port=80 --type=NodePort --dry-run=client -o yaml >service.yaml
# Change replica count of all deployments in a specifis namespace
kubectl get deploy -n live -o name | xargs -I % kubectl scale % --replicas=1 -n live
# Scale all deployments which include "api" in deployment name
kubectl get deploy -n live | awk '{print $1}' | grep api | xargs -I % kubectl scale deploy % --replicas=1 -n live
Delete all pods in a specific namespace
kubectl get pod -n live | awk '{print$1}' | xargs -I % kubectl delete pod % -n live
Delete specific pod grep by name
kubectl get job | grep janitor | awk '{print $1}' | xargs kubectl delete job
Delete Evicted Pods in a specific namespace
ns=live
kubectl get pod -n $ns | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n $ns
##### Change All Persistent volumes Claim Policy Delete to Retain
kubectl get pv | grep Delete | awk '{print$1}' | xargs -I % kubectl patch pv % -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
### If a resource is immutable, replace command can be used to patch the resource
kubectl patch clusteradmissionpolicy user-group-psp --type=merge -p '{"spec":{"mode": "monitor"}}' --dry-run=client -o yaml | kubectl replace -f - --force
Make Available Released Persistent Volume. To Reuse it again.
kubectl patch pv NAME -p '{"spec":{"claimRef": null}}'
Finalize Persistent Volme from kubernetes
kubectl patch pv NAME -p '{"metadata":{"finalizers":null}}'
##### Search for pod status CrashLoopBackOff in all namespaces and delete it
kubectl delete pod `kubectl get pod -A | awk '$4 == "CrashLoopBackOff" {print $2 " -n " $1}'`
##### Search for pod status Error in all namespaces and delete it
kubectl delete pod `kubectl get pod -A | awk '$4 == "Error" {print $2 " -n " $1}'`
### command to list down the deployment name, CPU requests, and memory requests in all namespaces:
kubectl get deployments --all-namespaces -o custom-columns="NAMESPACE:.metadata.namespace,DEPLOYMENT:.metadata.name,CPU_REQUESTS:.spec.template.spec.containers[*].resources.requests.cpu,MEMORY_REQUESTS:.spec.template.spec.containers[*].resources.requests.memory" --no-headers