• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

k8s相关工作中常用命令

开发技术 开发技术 2周前 (05-13) 9次浏览

==================================== prometheus ===================================================

reload prometheus

curl -X POST http://10.130.16.246:31190/-/reload

prometheus tsdb命令帮助

./tsdb –help-long

tsdb 查看block信息

./tsdb ls /data
./tsdb ls -h /data

tsdb 备份

./tsdb dump

tsdb 分析block

./tsdb analyze –limit=20 /data 01F0V6SQ072HVVBVKW2SN73AV8

==================================== k8s ==========================================================
官方命令参考:
https://kubernetes.io/zh/docs/reference/kubectl/cheatsheet/

容器外执行命令

kubectl exec etcd-master01 -n kube-system “ls”

使用patch更新 资源yaml

需要写一个yaml文件片段, 格式和资源文件一样

kubectl patch deployment retainkeys-demo –patch “$(cat patch-file-retainkeys.yaml)”

列出以标签过滤后的 Pod 的所有容器

要获取匹配特定标签的 Pod,请使用 -l 参数。以下匹配仅与标签 app=nginx 相符的 Pod。

kubectl get pods –all-namespaces -o=jsonpath=”{..image}” -l app=nginx

列出以命名空间过滤后的 Pod 的所有容器

要获取匹配特定命名空间的 Pod,请使用 namespace 参数。以下仅匹配 kube-system 命名空间下的 Pod。

kubectl get pods –namespace kube-system -o jsonpath=”{..image}”

查看pod 标签

kubectl get pods –show-labels -n monitoring

查看node的标签

kubectl get no –show-labels

查看pod中容器name,根据标签

kubectl get po -n monitoring -o jsonpath=”{..name}” -l app=pod-gpu-metrics-exporter

进入指定容器 -c

kubectl exec -it pod-gpu-metrics-exporter-65r8f -n monitoring -c pod-nvidia-gpu-metrics-exporter sh

service无法访问,查看endpoints

kubectl get ep -n monitoring

替换重启pod,实现configmap热更新

kubectl get po prometheus-0 -n monitoring -o yaml |kubectl replace –force -f –

查看节点labels

kubectl get node –show-labels=true

设置节点lable

kubectl label node 10.73.1.120 nstack/gpu-model=2080ti –overwrite

强制删除 termining pod

kubectl delete pod ha-70-zookeeper-1 –force –grace-period=0

Pod 仍处于 Unknown 状态

kubectl patch pod -p ‘{“metadata”:{“finalizers”:null}}’

查看yaml字段的解释

kubectl explain pods.spec.nodeName

rke k8s组件log:

/var/lib/rancher/rke

驱逐pod,命令node节点开始释放所有pod,并且不接收新的pod进程

kubectl drain [node-name] –force –ignore-daemonsets –delete-local-data

查看api信息

kubectl get –raw /apis/metrics.k8s.io | jq .
kubectl get –raw /api/v1/namespaces/kubeflow/services/ptcnowivuxhvsed | jq .

进入pod指定的container中

kubectl exec -it prometheus-cloud-0 -n monitoring sh -c busybox

获取 Pod 的唯一标识 uid,可通过命令

kubectl get pod pod名 -o jsonpath={.metadata.uid}

查看卷的持久化信息

findmnt /var/lib/kubelet/pods/24373a77-badf-4a3b-bc8b-eb5c1e97979c/volumes/kubernetes.io~csi/pvc-9a12e07e-67f4-477d-85b2-e3b74d39d18f/mount

get api资源

kubectl get –raw /api/v1/namespaces/kube-system/endpoints/kube-controller-manager | jq .

查看证书有限期

(rke部署)

for i in /etc/kubernetes/ssl/*.pem; do echo $i; openssl x509 -in $i -text -noout|egrep “Not Before|Not After”;echo “———–“; done

(kubeadm部署)

for i in /etc/kubernetes/pki/*.crt;do echo $i; openssl x509 -in $i -text -noout|egrep “Not Before|Not After”;echo “———–“;done

查看serviceaccount 用户 secret

kubectl get secret
kubectl describe secret default-token-jtqck

kubectl 自动补全

source <(kubectl completion bash)
echo “source <(kubectl completion bash)” >> ~/.bashrc

k8s secret、sa token查看

kubectl get secret
kubectl describe secret default-token-jtqck
==================================== k8s service ======================================================

k8s service & DNS

endpoints ip

pod_name.svc_name.namespace_name.svc.cluster.local

rabbitmq-0.rabbitmq.monitoring.svc.cluster.local

cluster ip (pod间通信)

svc_name.namespace_name.svc.cluster.local

rabbitmq.monitoring.svc.cluster.local

sh-4.4# curl prometheus.monitoring.svc.cluster.local:9090
Found.

pod 间通信(待确定)

pod_name.svc_name

rabbitmq-0.rabbitmq

svc_name.namespace_name.svc

prometheus.monitoring.svc

==================================== etcd ======================================================
https://rancher.com/docs/rancher/v2.x/en/troubleshooting/kubernetes-components/etcd/

查看集群成员状态

etcdctl –write-out=table member list

查看端点状态(表格形式输出)

etcdctl –write-out=table –endpoints=$ENDPOINTS endpoint status
etcdctl -w table endpoint status

docker外查看端点状态(也能查看节点角色)

docker exec -it etcd sh -c “etcdctl –write-out=table endpoint status”

列出集群所有节点状态

docker exec -it etcd sh -c “etcdctl -w table endpoint –cluster status”

在使用–cluster时候,ETCDCTL_ENDPOINTS,否者会报4001端口的endpoint错

docker exec -e ETCDCTL_ENDPOINTS=$(docker exec etcd /bin/sh -c “etcdctl member list | cut -d, -f5 | sed -e ‘s/ //g’ | paste -sd ‘,'”) etcd etcdctl endpoint status –write-out table

endpoint 健康状态

docker exec -it etcd sh -c “etcdctl -w table endpoint health”

测试集群性能

etcdctl check pref

手动切换leader

etcdctl move-leader

rke etcd debug level

docker run –net=host -v $(docker inspect kubelet –format ‘{{ range .Mounts }}{{ if eq .Destination “/etc/kubernetes” }}{{ .Source }}{{ end }}{{ end }}’)/ssl:/etc/kubernetes/ssl:ro appropriate/curl -s -XPUT -d ‘{“Level”:”DEBUG”}’ –cacert $(docker exec etcd printenv ETCDCTL_CACERT) –cert $(docker exec etcd printenv ETCDCTL_CERT) –key $(docker exec etcd printenv ETCDCTL_KEY) $(docker exec etcd printenv ETCDCTL_ENDPOINTS)/config/local/log

etcd info(default) level

docker run –net=host -v $(docker inspect kubelet –format ‘{{ range .Mounts }}{{ if eq .Destination “/etc/kubernetes” }}{{ .Source }}{{ end }}{{ end }}’)/ssl:/etc/kubernetes/ssl:ro appropriate/curl -s -XPUT -d ‘{“Level”:”INFO”}’ –cacert $(docker exec etcd printenv ETCDCTL_CACERT) –cert $(docker exec etcd printenv ETCDCTL_CERT) –key $(docker exec etcd printenv ETCDCTL_KEY) $(docker exec etcd printenv ETCDCTL_ENDPOINTS)/config/local/log

列出所有 前缀是/ 的keys, 其实就是所有的keys

etcdctl get –prefix –keys-only /

列出所有 configmaps(即前缀是/registry/configmaps)

etcdctl get –prefix –keys-only /registry/configmaps

列出3个前缀是/registry/minions的keys

etcdctl get –prefix –keys-only –limit=3 /registry/minions

获取一个key的value

etcdctl get -w json /registry/persistentvolumes/pvc-1a08fef0-46cd-4724-b91b-b53bacc720e3 | jq .

默认100ms超时配置

–heartbeat-interval and –election-timeout

改logs level 为Warning之上

https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/configuration.md#–log-level

备份

etcdctl –write-out=table snapshot save snapshot.db

查看备份状态

etcdctl –write-out=table snapshot status snapshot.db
==================================== storage ========================================================

csi log查看

kubectl logs instorage-csi-node-d7kr2 -c instorage-csi –tail=100 |grep ‘NodeStageVolume’

客户端,iscsi target查看

iscsiadm -m session

客户端,发现target

iscsiadm -m discovery -t sendtargets -p IP:port
iscsiadm -m discovery -t sendtargets -p 192.168.100.254

客户端,挂载target

iscsiadm -m node -T targetname –login

客户端,卸载target

iscsiadm -m node -T targetname –logout

target服务端,查看target

tgt-admin –show

查看设备路径

ll /dev/disk/by-path

查看多路径状态

multipath -ll

查看多路径信心

multipath -v3

查看多路径服务的状态

mpathconf

dd命令创建指定大小的文件

dd if=/dev/zero of=test bs=1M count=1000

删除单路径设备

echo 1 > /sys/block/sdX/device/delete

删除多路径设备

multipath -f /dev/dm-X

以下命令显示所有设备映射器设备及其主、副号码。副号码确定 dm 设备的名称。例如:副号码 3 与多路径的设备 /dev/dm-3 对映

dmsetup ls
mpathd (253:3)

删除多路径failed faulty running

multipath -ll | grep sd | grep failed | awk -F”:” ‘{print $4}’ | awk ‘{print $2}’ | while read LUN ;do echo 1 > /sys/block/${LUN}/device/delete; done

删除多路径设备,但是in use的不会被删除,相当于刷新

multipath -F

列出存储池

icfs osd lspools

列出rbd images

rbd -p stack ls

查看设备依赖

lsblk -s
==================================== docker ========================================================

harbor验证

配置docker/etc/docker/daemon.json,添加insecure-registries,否则会出现x509证书问题

{
“registry-mirrors”: [“https://registry.docker-cn.com&#8221;,”http://hub-mirror.c.163.com”%5D,
“insecure-registries” : [“registry.navinfo.com”,”https://gcr.io”%5D,
“live-restore”: true
}

登录harbor

docker login registry.navinfo.com

导出已有镜像为tar文件

docker save csiplugin:v2.2.1 -o csiplugin-v2.2.1.tar

加载镜像到本地

docker load -i csiplugin-v2.2.1.tar

给镜像打tag (仓库tag)

docker tag csiplugin:v2.2.1 registry.navinfo.com/instorage/csiplugin:v2.2.1

推送到远程仓库

docker push registry.navinfo.com/instorage/csiplugin:v2.2.1

构建images(在dockerfile目录下)

docker build -t csiplugin:v2.2.0 .

打tag

docker tag csiplugin-v2.2.0.tar:v2.2.0 registry.navinfo.com/instorage/csiplugin:v2.2.0

push镜像到harbor,宿主节点配置了证书

docker push registry.navinfo.com/instorage/csiplugin:v2.2.0

在执行清除镜像之前先查看镜像和容器的数量

docker system df

删除无容器使用的镜像,外加条件:创建于10天前、限于应用程序image

docker image prune -a –force –filter “label=type=appimage” –filter “until=240h”

删除所有不使用的镜像

docker image prune –force –all或者docker image prune -f -a

Docker prune 命令

删除 所有未被 tag 标记和未被容器使用的镜像:

docker image prune

删除 所有未被容器使用的镜像:

docker image prune -a

删除 所有停止运行的容器:

docker container prune

删除 所有未被挂载的卷:

docker volume prune

删除 所有网络:

docker network prune

删除 docker 所有资源:

docker system prune

========================================== clush =========================================

从本地拷贝到远程服务器组

clush -g test –copy /root/test.txt –dest /root/

从远程服务器拷贝到本地

clush -g test -rcopy /root/test.txt –dest /root/

排除某个节点

clush -g test -x 10.73.1.128 -rcopy /root/test.txt –dest /root/

========================================== virtualbox =========================================

关机

VBoxManage.exe controlvm -h
VBoxManage.exe controlvm nineep poweroff

启动

VBoxManage.exe startvm nineep –type headless

列出正在运行的vm

VBoxManage.exe list runningvms

========================================== istio ============================================

查看xds状态

istioctl proxy-status

查看xds配置

istioctl proxy-config

分析和诊断网格

istioctl analyze
istioctl experimental describe pod gcr03xzshy28zx0-v1-55b8f7c6c8-d2q69 -n 24f3af992e9e4ad28e7954fa4fdafb07-127

istio web 工具: ControlZ

========================================== influxdb ==========================================

检查数据

influx_inspect verify -dir /data/influxdb/
influx_inspect verify-seriesfile -dir /data/influxdb/data/

登录数据库

influx -database prometheus -host ‘localhost’ -port ‘8086’

查看measurements=metrics(类似于table) metric_name=table_name, label=field, label_value=field_value

show measurements

查看时序数据

show series

查看时序数据的tag (metrics label)

show tag keys

查看metrics name, 及其value 数据类型

show field keys

查询10.60.34.23:9100节点的noad_load1

select * from node_load1 where instance=’10.60.34.23:9100′

rebalance

curl -X POST ‘http://127.0.0.1:7076/rebalance?circle_id=0&operation=add&ha_addrs=10.60.191.251:7076&#8217;
http://www.blog.sun-iot.xyz/posts/61185b64#数据同步机制

debug

curl -XPOST http://127.0.0.1:8086/debug/vars

========================================== ansible ===============================================

dry run

ansible-playbook -i inventory/storage.ini deploy-exporter.yml -t r1-xsky -C

列出tags执行的task

ansible-playbook -i inventory/all.ini test.yml -t exporter_config_resolv —list-tasks

注意tags 和skip tags的先后顺序

ansible-playbook -i inventory/all.ini deploy-exporter.yml —list-tasks –skip-tags r2-xsky -t node_exporter


程序员灯塔
转载请注明原文链接:k8s相关工作中常用命令
喜欢 (0)