k8s相關工作中常用命令


==================================== 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","http://hub-mirror.c.163.com"],
"insecure-registries" : ["registry.navinfo.com","https://gcr.io"],
"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'
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM