port-forward 通过端口转发映射本地端口到指定的应用端口

kubectl port-forward <pod_name> <forward_port> --namespace <namespace> --address <IP默认:127.0.0.1>

1)将本地端口转发到 Pod 中的端口

kubectl port-forward redis-master 6379:6379

2)把本地8080端口转发到容器的80端口

kubectl port-forward my-nginx-master 8080:80

3)将远程pod端口转发到本地端口

kubectl port-forward monitoring-grafana --namespace=monitoring 3000:3000
配置验证--validate

如果你不确定指定的资源是否正确,你可以 kubectl 帮你验证。

$ kubectl create -f ./hello-world.yaml --validate

假设我们指定的是 entrypoint 而不是 command ,你会看到如下输出:

I0709 06:33:05.600829 14160 schema.go:126] unknown field: entrypoint
I0709 06:33:05.600988 14160 schema.go:129] this may be a false alarm,
see https:///GoogleCloudPlatform/kubernetes/issues/6842
pods/hello-world

kubectl create –validate 会警告已经检测出问题,除非缺少必须的字段或者字段值不合法,最后kubectl还是会创建出资源。一定要小心,未知的API字段会被忽略。这个pod没有 command 字段而被创建,command字段是一个可选字段,因为image镜像可以指定 Entrypoint 。访问Pod API object来查看合法字段列表。

查看命令简写
kubectl api-resorces
标签
查看所有标签:
kubectl get nodes --show-lables
设置标签:
kubectl label node xxx.xxx.xxx.xxx  type=nginx
删除标签:
kubectl label nodes xxx.xxx.xxx.xxx key-
修改覆盖标签:
kubectl label nodes xxx.xxx.xxx.xxx <key>=<value> --overwrite
污点
查看所有node上的污点:
kubectl descrile nodes |grep Taints
查看某个node污点:
kubectl descrile node xxx.xxx.xxx.xxx |grep Taints
添加污点:
kubectl taint node xxx.xxx.xxx.xxx key=value:NoSchedule
删除污点:
kubectl taint nodes xxx.xxx.xxx.xxx key:NoSchedule-   
或者
kubectl taint nodes xxx.xxx.xxx.xxx key-

1.根据启动时间降序(descending order)

kubectl get pods --sort-by=.metadata.creationTimestamp

2. 根据启动时间升序(ascending order)

kubectl get pods --sort-by=.metadata.creationTimestamp | awk 'NR == 1; NR > 1 {print $0 | "tac"}'
 kubectl get pods --sort-by=.metadata.creationTimestamp | tail -n +2 | tac
 kubectl get pods --sort-by={metadata.creationTimestamp} --no-headers | tac
 kubectl get pods --sort-by=.metadata.creationTimestamp | tail -n +2 | tail -r

3.查看集群內 Pod 的服务质量等级(QoS)

kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,QOS-CLASS:.status.qosClass

4.把Secret复制到其他namespace

kubectl get secrets -o json --namespace namespace-old | \
   jq '.items[].metadata.namespace = "namespace-new"' | \
   kubectl create-f  -

5.获取K8s的token

kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token

6.清理K8s异常pod

#clean Evicted
 kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n 
 # clean error
 kubectl get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n 
 #clean compete
 kubectl get pods --all-namespaces -o wide | grep Completed | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n

7.强制删除指定namespace下Terminating状态的pod

kubectl get pod -n $namespace |grep Terminating|awk '{print $1}'|xargs kubectl delete pod --grace-period=0 --force

8.批量强制删除集群内Terminating状态的pod

for ns in $(kubectl get ns --no-headers | cut -d ' ' -f1); do \
   for po in $(kubectl -n $ns get po --no-headers --ignore-not-found | grep Terminating | cut -d ' ' -f1); do \
     kubectl -n $ns delete po $po --force --grace-period 0; \
   done; \
 done;

9.导出干净的YAML

#需要插件kubectl-neat支持https:///itaysk/kubectl-neat
 kubectl get cm nginx-config -oyaml | kubectl neat -o yaml

10.clean unused pv

kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Used By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs -n2 bash -c 'kubectl -n ${1} delete pvc ${0}'

11.清理没有被绑定的 PVC

kubectl get pvc --all-namespaces | tail -n +2 | grep -v Bound | awk '{print $1,$2}' | xargs -L1 kubectl delete pvc -n

12.临时释放的指定namespace下的pod

适用于不想删除 Kubernetes 集群內的信息

# 方法一:通过 patch 模式
 kubectl get deploy -o name -n <NAMESPACE>|xargs -I{} kubectl patch {} -p '{"spec":{"replicas":0}}'
  
 # 方法二:通过资源伸缩副本数
 kubectl get deploy -o name |xargs -I{} kubectl scale --replicas=0 {}

13.临时关闭 Daemonsets

如果需要临时将 Daemonsets 关闭,只需要将其调度到一个不存在的 node 上即可,调整下 nodeSelector

kubectl patch daemonsets nginx-ingress-controller -p '{"spec":{"template":{"spec":{"nodeSelector":{"project/xdp":"none"}}}}}'

14.清理没有被绑定的 PV

kubectl get pv | tail -n +2 | grep -v Bound | awk '{print $1}' | xargs -L1 kubectl delete pv

15.根据pods的重启次数进行排序

kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount'

16.无缝重启deploy,daemonset,statfulset(zero downtime)

kubectl -n <namespace> rollout restart deployment <deployment-name>

17.根据overlay2目录名找容器

docker ps -q | xargs docker inspect --format '{{.Name}}, {{.State.Pid}}, {{.Id}}, {{.GraphDriver.Data.WorkDir}}'

18.通过变量组合展示容器绑定端口列表

docker inspect --format '{{/*通过变量组合展示容器绑定端口列表*/}}已绑定端口列表:{{println}}{{range $p,$conf := .NetworkSettings.Ports}}{{$p}} -> {{(index $conf 0).HostPort}}{{println}}{{end}}' Web_web_1

19.查询指定网络下的容器名称,如果存在输出容器名称,如果没有,输出With No Containers

docker inspect --format '{{range .Containers}}{{.Name}}{{println}}{{else}}With No Containers{{end}}' bridge

20.通过索引序号读取默认网关

docker inspect bridge --format '{{/*查看网络的默认网关*/}}{{(index .IPAM.Config 0).Gateway}}'

21.查看容器是否配置了容器策略

docker ps -q | xargs docker inspect --format '{{if not .State.Restarting}}{{.Name}}容器没有配置重启策略{{end}}'

22.查看容器当前的运行状态

docker inspect --format '{{or .State.Status .State.Restarting}}' configuration-center

23.显示所有容器的IP

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)

24.显示所有容器的mac地址

docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' $(docker ps -a -q)

25.显示所有容器的名称,并分离出反斜杠

docker inspect --format='{{.Name}}' $(docker ps -aq)|cut -d"/" -f2

26.创建临时可调式POD

kubectl run ephemeral-busybox \
   --rm \
   --stdin \
   --tty \
   --restart=Never \
   --image=lqshow/busybox-curl:1.28 \
   -- sh

27.获取容器的日志路径

docker inspect --format='{{.LogPath}}' docker-test1

28.调试coredns

kubectl run -it --rm --restart=Never --image=infoblox/dnstools:latest dnstools

29.查看资源使用情况

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c "echo {} ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve --;"

30.查看资源总情况

kubectl get no -o=custom-columns="NODE:.metadata.name,ALLOCATABLE CPU:.status.allocatable.cpu,ALLOCATABLE MEMORY:.status.allocatable.memory"

31.查看CPU分配情况

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo -n "{}\t"|tr "\n" " " ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- | grep cpu | awk '\''{print $2$3}'\'';'

32.查看内存分配

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo "{}\t"|tr "\n" " " ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- | grep memory | awk '\''{print $2$3}'\'';'

33.查看所有镜像

kubectl get pods -o custom-columns='NAME:metadata.name,IMAGES:spec.containers[*].image'

34.线程数统计

printf "    ThreadNUM  PID\t\tCOMMAND\n" && ps -eLf | awk '{$1=null;$3=null;$4=null;$5=null;$6=null;$7=null;$8=null;$9=null;print}' | sort |uniq -c |sort -rn | head -10

35.设置环境变量

kubectl set env deploy <DEPLOYMENT_NAME> OC_XXX_HOST=bbb

36.端口映射

  1. 将 localhost:3000 的请求转发到 nginx-pod Pod 的 80 端口
kubectl port-forward nginx-po 3000:80
  1. 将 localhost:3201 的请求转发到 nginx-web service 的 3201 端口
kubectl port-forward svc/nginx-web 3201

37.配置默认storageclass

kubectl patch storageclass <your-class-name> -p '{"metadata": {"annotations":{"/is-default-class":"true"}}}'

38.在多个pod中运行命令

kubectl get pods -o name | xargs -I{} kubectl exec {} -- <command goes here>

39. 查看容器名

kubectl get po calibre-web-76b9bf4d8b-2kc5j -o json | jq -j ".spec.containers[].name"

40.进入容器namespace

docker ps | grep APP_NAME
 docker inspect CONTAINER_ID | grep Pid
 nsenter -t PID -n

41.查找非 running 状态的 Pod

kubectl get pods -A --field-selector=status.phase!=Running | grep -v Complete

42.获取节点列表及其内存容量

kubectl get no -o json | jq -r '.items | sort_by(.status.capacity.memory)[]|[.metadata.name,.status.capacity.memory]| @tsv'

43.使用交互 shell 访问匹配到 标签的 Pod

# 案例1
 kubectl exec -i -t $(kubectl get pod -l <KEY>=<VALUE> -o name |sed 's/pods\///') -- bash
  
 # 案例2
 kubectl exec -i -t $(kubectl get pod -l <KEY>=<VALUE> -o jsonpath='{.items[0].metadata.name}') -- bash

44.获取每个节点的Pod数量

kubectl get po -o json --all-namespaces | jq '.items | group_by(.spec.nodeName) | map({"nodeName": .[0].spec.nodeName, "count": length}) | sort_by(.count)'

45.复制secret到其他namespace下

比如使用证书,镜像凭证等.

kubectl get secret <SECRET-NAME> -n <SOURCE-NAMESPACE> -oyaml | sed "/namespace:/d" | kubectl apply --namespace=<TARGET-NAMESPACE> -f -

46.重置集群节点

  1. 将节点标记为不可调度,确保新的容器不会调度到该节点
kubectl cordon <NODE-NAME>
  1. Master 节点上将需要重置的节点驱逐, 除了 deemonset
kubectl drain <NODE-NAME> --delete-local-data --force --ignore-daemonsets
  1. 删除节点
kubectl delete node <NODE-NAME>
  1. 在需要重置节点上执⾏重置脚本,注意,如果在 Master 主节点执⾏ kubeadm reset,则需要重新初始化集群
kubeadm reset


转载:https://blog.csdn.net/justlpf/article/details/131407059