排查pod故障

#查看pod是否正常
kubectl get pods -n fronted
#常见pod排查命令
kubectl logs <pod name> 有助于检索 Pod 中容器的日志
kubectl describe pod <pod name>  对检索与 Pod 相关的事件列表很有用
kubectl get pod <pod name>  -o yaml 可提取 Kubernetes 中存储的 Pod 的 YAML 定义;
kubectl exec -ti <pod name> bash 可在 Pod 中的一个容器运行一个交互式命令
#常见错误
##启动时的错误包括:
ImagePullBackoff
ImageInspectError
ErrImagePull
ErrImageNeverPull
RegistryUnavailable
InvalidImageName
##运行中的错误包括:
CrashLoopBackOff
RunContainerError
KillContainerError
VerifyNonRootError
RunInitContainerError
CreatePodSandboxError
ConfigPodSandboxError
KillPodSandboxError
SetupNetworkError
TeardownNetworkError

#常见错误原因
#ImagePullBackOff  镜像拉取错误(无效的镜像,指定了不存在标签,registry凭据)
kubectl create secret docker-registry harbor-pull-secret-188-dev --docker-server=http://harbor.wuliuhub.com --docker-username=admin --docker-password=Harbor12345 -n ms

#CrashLoopBackOff  (应用程序中存在错误,容器配置错误,liveness探针失败)
kubectl logs podname --previous  (如果容器启动太快,打印前一个容器信息)
#RunContainerError(1.查看是否有不存在的卷,如configmap,secret 2.将只读挂在为读写) ---> kubectl describe pod <pod-name> 命令检查和分析这个错误
#pending状态  (1.没有足够的资源调度失败 2.当前命名空间具有ResourceQuota 对象,创建 Pod 将使命名空间超过配额  3.绑定了一个处于pending状态的pvc)
kubectl get events --sort-by=.metadata.creationTimestamp #如果ResouceQuota不足
#pod处于未就绪状态(“就绪”探针失败时,则 Pod 未连接到服务,并且没有流量转发到该实例)

排查service故障

#如果pod已经就绪,忍让无法收到程序响应,则查看service是否正确.
#Service 会根据 Pod 的标签将流量路由到 Pod。因此,应该先检查 Service 定位了多少个 Pod,检查service对应的endpoint
[root@k8s-master-01 ~]# kubectl  describe svc serviceName -n 空间名称
Name:              h5sdk
Namespace:         fronted
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"h5sdk","namespace":"fronted"},"spec":{"ports":[{"port":80,"target...
Selector:          app=h5sdk
Type:              ClusterIP
IP:                10.1.103.142
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.3.180:80,10.244.3.181:80,10.244.4.110:80
Session Affinity:  None
Events:            <none>

#加入endpoint为空
1.pod没有正确的标签是否在正确的命名空间
2.service的selector标签错误
#如果不为空,则看下targetPort是否错误
kubectl port-forward service/php-fpm 9000:9000 -n lnmp
[root@k8s-master-01 ~]# kubectl port-forward service/h5sdk 3000:80 -n fronted  (3000是你想在计算机上打开的端口,80是service暴露端口)
Forwarding from 127.0.0.1:3000 -> 80
Forwarding from [::1]:3000 -> 80

ingress故障排查

#1.0如果到这个地方,那就意味着pod运行正常且service可以正常分发流量到pod,开始排查ingress故障
[root@k8s-master-01 ~]# kubectl  describe ingress h5sdk -n fronted
Name:             h5sdk
Namespace:        fronted
Address:          
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host                Path  Backends
  ----                ----  --------
  h5game.xxxx.com  
                      /   h5sdk:80 (10.244.3.180:80,10.244.3.181:80,10.244.4.110:80)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"networking.k8s.io/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"h5sdk","namespace":"fronted"},"spec":{"rules":[{"host":"h5game.xxx.com","http":{"paths":[{"backend":{"serviceName":"h5sdk","servicePort":80},"path":"/"}]}}]}}

Events:  <none>

#2.0如果ingress能够看到endpoint,仍然无法使用 (1.将ingress暴露到公网的方式  2.将集群暴露到公网的方式)
#2.1检索ingress端口
[root@k8s-master-01 ~]# kubectl  get pods -n ingress-nginx
NAME                             READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-24m74   1/1     Running   0          20d
nginx-ingress-controller-qtgjw   1/1     Running   1          156d
nginx-ingress-controller-wldp7   1/1     Running   1          156d

[root@k8s-master-01 new2]# kubectl  describe pod ingress-nginx-controller-gdck8 -n ingress-nginx|grep Ports
    Ports:         80/TCP, 443/TCP, 8443/TCP
    Host Ports:    0/TCP, 0/TCP, 0/TCP
    
#2.2如果还没有解决,则可以查看ingress controller日志 

查看已存在的secret

kubectl get secret -n namespace

secret查看具体条目的key

kubectl describe secret mysql-pass -n namespace

查看具体的value

kubectl edit secret mysql-pass -n namespace

# 然后进行base64反编码
echo -n 编码的内容 | base64 --decode

获取deployment

kubectl get deployment

滚动重启

kubectl restart deployment deployment-name -n 命令空间
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐