您的位置 首页 k8s

K8S故障排查技巧: (k8s 命令)

xshell  快捷命令:

k8s命令

 kubectl  explain po.spec.volumes.nfs

 

0 部署项目:

kubectl apply -f service.yml

使用 kubectl apply 命令时,会在 Kubernetes 集群中创建或更新资源。如果想要删除资源,需要使用 kubectl delete 命令。

删除:

kubectl delete -f <manifest-file.yaml>

 

1-日志

kubectl logs -n

2-删除pod

kubectl delete pod -n

3-描述

kubectl describe pod -n 

4-获取所有的pod

kubectl get po -A

5-

kubectl get po -A

6- 获取名称空间

kubectl get ns

7- 获取nodes

kubectl get nodes

8-

kubectl get sts -A

9-

kubectl get sts -A

10-

 kubectl get cs

 

11-pv

kubectl get pv -A

12-获取没有运行的 pod

kubectl get po -A | grep -v Running | grep -v Completed

13- ks 安装日志

kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

14-待释放资源

 kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n

15- 存储类

kubectl get storageclass

16-velero 日志

kubectl logs deployment/velero -n velero

17- k8s创建namespace

kubectl create namespace my-namespace

service.yaml  连接

 

强制删除:

1- 强制删除 pod

kubectl delete pod PODNAME --force --grace-period=0 -n namespace
kubectl delete pod -n langfuse-gpt                   langfuse-675997786d-jc56r  --force
--grace-period=0 将宽限期设置为0秒,这表示不等待正常终止。

--force 强制立即删除Pod,可能导致数据丢失,并可能中断当前正在执行的操作。

 

 

2、强制删除 namespace

kubectl delete namespace NAMESPACENAME --force --grace-period=0   -n namespace

3、强制删除 pvc

kubectl patch pvc pvc-name -p '{"metadata":{"finalizers":null}}' -n namespace

 

kubelet:

重启:

systemctl restart kubelet

 

demoset

kubectl get daemonset -n kubesphere-monitoring-system

 

1,DaemonSet 与 Deployment 的区别

Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。

DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。

查看全部 的yaml

kubectl get ds node-exporter -n kubesphere-monitoring-system  -o yaml > ds-all.yaml

 

 

K8S故障排查技巧:

 

kubectl version --short

kubectl cluster-info

kubectl get componentstatus

kubectl api-resources -o wide --sort-by name

kubectl get events -A

kubectl get nodes -o wide

kubectl get pods -A -o wide

kubectl run a --image alpine --command -- /bin/sleep 1d

kubectl edit svc my-release-milvus   #编辑  svc 信息

 

		- kubectl describe
		- kubectl explain
		- kubectl exec
		
		- kubectl logs
		- kubectl cp
		- po.spec.container.command
		- po.spec.container.args

 

1- 获取 所有项目 

kubectl get namespace

2-  获取存储类 PVC

kubectl get pvc -A

 

 

1.kubectl describe

查看资源的详细信息,根据事件信息获取当前资源的状态,从而给出解决方案。
[root@master231 pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
image-resources-stress-04   0/1     Pending   0          2m13s

 

三种查看方式:

kubectl describe -f 06-pods-resources.yaml
kubectl describe pod image-resources-stress-04
kubectl describe pod/image-resources-stress-04

# 观察当前的Node字段名称,和"kubectl get nodes "的NAME是否相同。
# 如下所示,没有发现时间,此时可能是调度的节点不存在,请对比Node名称是否正确。
kubectl get nodes
kubectl get pods -o wide

 

2.kubectl logs

通过容器启动的日志查看。
kubectl logs huaxiangpod
kubectl logs -f linux-web  # 实时查看某个Pod的日志
kubectl logs -p -f linux-web   # pod中容器的上一个实例的日志,前提是该容器未被删除。
kubectl logs --since=2m -f linux-web   # 查看某个容器最近2分钟内产生的日志
kubectl logs -f -c wp baimei-wp   # 查看指定容器的日志。

 

3.kubectl cp

 

 kubectl cp /etc/hosts baimei-wp:/haha -c db  # 将本地的"/etc/hosts"拷贝到pod中db容器的/haha路径。
kubectl cp /etc baimei-wp:/haha-dir -c db  # 将本地的目录拷贝到容器的指定路径。

 

kubectl cp baimei-wp:/haha xixi -c db   # 将pod的db容器的/haha文件拷贝到宿主机。
tar cf - * | kubectl exec -i baimei-wp -c db  -- tar xf - -C /tmp/  # 将当前目录下的所有文件打包,并解压到指定pod的容器的指定路径。

 

4.kubectl exec:

连接到指定的容器查看。
 kubectl get pods
kubectl exec -it linux-web -- sh  # 当一个Pod内只有一个容器时,无需使用-c选项。

 

kubectl exec -it baimei-wp -c wp -- bash  # 当Pod内有多个容器时,可以使用-c选项连接到指定的容器。若不指定,默认连接到第一个容器。

 

5-故障排查之command和args的区别

 

apiVersion: v1
kind: Pod
metadata:
  name: linux86-command-args-03
spec:
  # hostNetwork: true
  containers:
  - name: web
    image: harbor.baimei.com/baimei-web/nginx:1.25.1-alpine
    # 类似于Dockerfile的ENTRYPOINT指令。
    # command: ["tail","-f","/etc/hosts"]
    # 类似于Dockerfile的CMD指令。
    # args: ["sleep","3600"]
    command:
    - "tail"
    - "-f"
    args:
    - "/etc/hosts"

 

 

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章