get all resources in a cluster
kubectl get all -A
Sort cpu or memory of pods or nodes(Find the pod who consumm more than others)
install server metrics
wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
add those line into file yaml
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
like
spec:
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
image: registry.k8s.io/metrics-server/metrics-server:v0.6.4
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
then apply the file
kubectl apply -f components.yaml
then get sorted informations
kubectl top pod | awk '{ print $1 ";" $2 ";" $3}' > sort.in ; sort -t";" -V -k3 sort.in
-t is seprator
-V alphanumeric
-k3 column 3 to be sorted
sort.in the file input
Info cluster service CIDR...
kubectl cluster-info dump | grep -i service-cluster
kubectl cluster-info dump | grep -i cluster-cidr
Change image of a deployment
create a deployment with image
kubectl create deploy nginx-deploy --image=nginx:1.16 --replicas=1
change the image of the deployment
kubectl set image deployment nginx-deploy nginx=nginx:1.17
get all pod in a specific node
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>
Migrate pods where are in a specific node to another node
unschedule pods with cordon command to not let other pods to come to this node.
kubectl cordon node-1
drain node(vider) to exclude pods to other pods that are scheduled. drain containt the cordon command naturally
kubectl drain node-2 --force
get node scheduled with command uncordon
kubectl uncordon node-1
Make a node unavailable
kubectl drain node-01 --force
or
kubectl drain node-01 --grace-period 0
Creation pod:
kubectl run static-busybox --image=busybox --command "sleep 1000" --dry-run=client -o yaml
Création deploy
kubectl create deployment hr-web-app --image=redis --replicas=3 --dry-run=client -o yaml
Scale a Deploy or RS
kubectl scale deploy/test2 --replicas=3
Expose a pod (deploy or rs)
kubectl expose pod messaging --name=messaging-service --type=ClusterIP --port=6379
Create a static pod (important because static pod does not scheduled by api, you should put it in manifests)
kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -o yaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml
Create deploy with replicas
kubectl create deployment hr-web-app --image=kodekloud/webapp-color --replicas=2
Correct a pod description with edit command. when you edit a pod, a temp file is created and you should force
kubectl edit pod namepod
kubectl replace -f /tmp/kubectl-edit-xxxx.yaml --force
Expose a deploy with a specific NodePort. after this command you should change the port wanted in the file
kubectl expose deployment hr-web-app --type=NodePort --port=8080 --name=hr-web-app-service --dry-run=client -o yaml > hr-web-app-service.yaml
Get OSImage of nodes and put in the file
kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.osImage}' > /opt/outputs/nodes_os_x43kj56.txt
Create a persistent volume
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-analytics
spec:
capacity:
storage: 100Mi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
hostPath:
path: /pv/data-analytics
Lens pour gérer son Cluster
-
Télécharger et installer Lens Open : Lien
-
Récupérer la conf de votre cluster depuis Kub avec la commande :
kubectl config view --minify --raw
Or
kubectl config view --flatten > out.txt
- Ajouter la conf dans la fenêtre ci-dessous
Use JsonPath to extract informations with kubectl
https://kubernetes.io/docs/reference/kubectl/jsonpath/
The list bellow can help you, but for more details please see the link above
kubectl config view
Result :
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://111.97.79.11:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
namespace: ns-0001-example-prod
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: DATA+OMITTED
client-key-data: DATA+OMITTED
kubectl config view -o jsonpath='{..namespace}'
result : ns-0001-example-prod