forked from devopsyuva/kubernetes_latest_manifest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcuts
111 lines (83 loc) · 5.43 KB
/
shortcuts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
How to ping all pods at once?
=============================
kubectl get po -o json | jq .items[].status.podIP -r | fping
Sample output:
root@kubernetesmanager:~# kubectl get po --all-namespaces -o json | jq .items[].status.podIP -r | fping
10.244.52.9 is alive
192.168.0.50 is alive
192.168.0.51 is alive
10.244.52.8 is alive
10.244.52.7 is alive
192.168.0.50 is alive
192.168.0.50 is alive
192.168.0.50 is alive
192.168.0.50 is alive
192.168.0.51 is alive
192.168.0.50 is alive
10.244.119.11 is alive
10.244.119.10 is alive
root@kubernetesmanager:~#
For rollingupdates:
===================
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate"}}}'
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":3}}}}'
List all images running on the cluster:
=======================================
kubectl get pods --all-namespaces -o jsonpath="{..image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" | tr -s '[[:space:]]' '\n' | uniq -c
List images from specific node for the pods with status Running, Completed and Succeeded
kubectl get pods --all-namespaces --field-selector=status.phase=Running --field-selector=status.phase=Completed --field-selector=status.phase=Succeeded --field-selector=spec.nodeName=<node> -o jsonpath="{.items[*].spec.containers[*].image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c | sort | uniq -c | awk -F' ' '{sum+=$1;}END{print sum;}'
Sample output:
kubernetesui/dashboard:v2.0.0-rc7root@kubernetesmanager:~# kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" | tr -s '[[:space:]]' '\n' | uniq -c
1 calico/kube-controllers:v3.11.2
2 calico/node:v3.11.2
2 k8s.gcr.io/coredns:1.6.7
1 k8s.gcr.io/etcd:3.4.3-0
1 k8s.gcr.io/kube-apiserver:v1.18.2
1 k8s.gcr.io/kube-controller-manager:v1.18.2
2 k8s.gcr.io/kube-proxy:v1.18.2
1 k8s.gcr.io/kube-scheduler:v1.18.2
1 kubernetesui/metrics-scraper:v1.0.4
1 kubernetesui/dashboard:v2.0.0-rc7
root@kubernetesmanager:~#
Patching:
=========
$ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # Partially update a node
# Update a container's image; spec.containers[*].name is required because it's a merge key
$ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# Update a container's image using a json patch with positional arrays
$ kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
Editing kubernetes objects:
===========================
$ kubectl edit svc/docker-registry # Edit the service named docker-registry
$ KUBE_EDITOR="nano" kubectl edit svc/docker-registry # Use an alternative editor
Deleting resources:
===================
$ kubectl delete -f ./pod.json # Delete a pod using the type and name specified in pod.json
$ kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
$ kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
$ kubectl -n my-ns delete po,svc --all # Delete all pods and services in namespace my-ns
Nodes and cluster:
==================
$ kubectl cordon my-node # Mark my-node as unschedulable
$ kubectl drain my-node # Drain my-node in preparation for maintenance
$ kubectl uncordon my-node # Mark my-node as schedulable
$ kubectl top node my-node # Show metrics for a given node
$ kubectl cluster-info # Display addresses of the master and services
$ kubectl cluster-info dump # Dump current cluster state to stdout
$ kubectl cluster-info dump --output-directory=/path/to/cluster-state # Dump current cluster state to /path/to/cluster-state
# If a taint with that key and effect already exists, its value is replaced as specified.
$ kubectl taint nodes foo dedicated=special-user:NoSchedule
Output format Description
-o=custom-columns=<spec> Print a table using a comma separated list of custom columns
-o=custom-columns-file=<filename> Print a table using the custom columns template in the <filename> file
-o=json Output a JSON formatted API object
-o=jsonpath=<template> Print the fields defined in a jsonpath expression
-o=jsonpath-file=<filename> Print the fields defined by the jsonpath expression in the <filename> file
-o=name Print only the resource name and nothing else
-o=wide Output in the plain-text format with any additional information, and for pods, the node name is included
-o=yaml Output a YAML formatted API object
Reference: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/
https://unofficial-kubernetes.readthedocs.io/en/latest/user-guide/kubectl-cheatsheet/
kubeadm join 10.128.0.2:6443 --token nmuqj4.5zs6b65k003y339f \
--discovery-token-ca-cert-hash sha256:8a7f8a5b9d1a372117eca6c4ccb8522f872ff23e05b975569bbd76e37dc167e0