天天看点

kubernetes集群问题排查 1. 查看系统Event事件 2. 查看容器日志 3. 查看k8s服务日志 4. 常见问题

<b>版权声明</b>:本文为博主原创文章,未经博主允许不得转载。如需转载请联系本人,并标明出处和作者。

kubectl describe pod &lt;podname&gt; --namespace=&lt;namespace&gt;  

该命令可以显示pod创建时的配置定义、状态等信息和最近的event事件,事件信息可用于排错。例如当pod状态为pending,可通过查看event事件确认原因,一般原因有几种:

没有可用的node可调度

开启了资源配额管理并且当前pod的目标节点上恰好没有可用的资源

正在下载镜像(镜像拉取耗时太久)

kubectl describe还可以查看其它k8s对象:node,rc,service,namespace,secrets。

kubectl describe pod &lt;podname&gt; --namespace=&lt;namespace&gt;

kubectl describe node

name:           runtime2.foshan2.wae.haplat.net  

labels:         kubernetes.io/hostname=runtime2.foshan2.wae.haplat.net,namespace/test=true  

creationtimestamp:  fri, 01 apr 2016 17:34:16 +0800  

phase:  

conditions:  

  type      status  lastheartbeattime           lasttransitiontime          reason              message  

  ────      ──────  ─────────────────           ──────────────────          ──────              ───────  

  ready     true    sat, 08 apr 2017 14:15:41 +0800     sun, 26 mar 2017 08:58:04 +0800     kubeletready            kubelet is posting ready status  

  outofdisk     false   sat, 08 apr 2017 14:15:41 +0800     fri, 01 apr 2016 17:34:16 +0800     kubelethassufficientdisk    kubelet hassufficient disk space available  

addresses:  221.5.100.100,221.5.100.100  

capacity:  

 memory:    134975102976  

 pods:      40  

 cpu:       32  

system info:  

 machine id:            120de474f77e4d75a670a74eea6d1e45  

 system uuid:           1c929431-8d94-11e1-bd1d-001e6744d094  

 boot id:           2a154beb-86e4-40e3-acce-4f83b1ea5ed2  

 kernel version:        3.10.0-229.20.1.el7.x86_64  

 os image:          centos linux 7 (core)  

 container runtime version: docker://1.8.2-el7.centos  

 kubelet version:       v1.1.1-wae2-12  

 kube-proxy version:        v1.1.1-wae2-12  

externalid:         runtime2.foshan2.wae.haplat.net  

non-terminated pods:        (6 in total)  

  namespace         name                    cpu requests    cpu limits  memory requests     memory limits  

  ─────────         ────                    ────────────    ──────────  ───────────────     ─────────────  

  acp               acp-ui-1-4-0-16j3a          4 (12%)     4 (12%)     8589934592 (6%)     8589934592 (6%)  

  acp               acp-ui-1-4-1-kou20          4 (12%)     4 (12%)     8589934592 (6%)     8589934592 (6%)  

  acp               acp-ui-api-1-4-0-u3w4f          4 (12%)     4 (12%)     8589934592 (6%)     8589934592 (6%)  

  cloud-eye         cloud-eye-dim2-1-6-6-1-9g16i        4 (12%)     4 (12%)     34359738368 (25%)   34359738368 (25%)  

  cloud-wst         cloud-wst-ceba-1-12-0-1-hjdk2       2 (6%)      2 (6%)      8589934592 (6%)     8589934592 (6%)  

  cms-fd            cms-fd-schedule-3-9-1-3-wzqa1       8 (25%)     8 (25%)     21474836480 (15%)   21474836480 (15%)  

allocated resources:  

  (total limits may be over 100%, i.e., overcommitted. more info: http://releases.k8s.io/head/docs/user-guide/compute-resources.md)  

  cpu requests  cpu limits  memory requests     memory limits  

  ────────────  ──────────  ───────────────     ─────────────  

  26 (81%)  26 (81%)    90194313216 (66%)   90194313216 (66%)  

no events.  

kubectl describe rc --namespace=rmp

[root@node5 ~]# kubectl describe rc --namespace=rmp  

name:       rmp-web-2-15-3-1  

namespace:  rmp  

image(s):   registry.wae.haplat.net/rmp/rmp-web:2.15.3-1  

selector:   app=rmp-web,appversion=2.15.3-1  

labels:     app=rmp-web,appversion=2.15.3-1,env=product,zone=foshan2  

replicas:   1 current / 1 desired  

pods status:    1 running / 0 waiting / 0 succeeded / 0 failed  

no volumes.  

kubectl describe namespace

[root@node5 ~]# kubectl describe namespace  

name:   acp  

labels: &lt;none&gt;  

status: active  

resource quotas  

 resource       used        hard  

 ---            ---     ---  

 cpu            24      20  

 memory         51539607552 53687091200  

 persistentvolumeclaims 0       10  

 pods           6       10  

 replicationcontrollers 6       10  

 resourcequotas     1       1  

 secrets        2       10  

 services       6       10  

no resource limits.  

kubectl describe service --namespace=rmp

[root@node5 ~]# kubectl describe service --namespace=rmp  

name:           rmp-web-2-15-3-1  

namespace:      rmp  

labels:         app=rmp-web,appversion=2.15.3-1,waeenv=product,waezone=foshan2  

selector:       app=rmp-web,appversion=2.15.3-1  

type:           clusterip  

ip:         10.254.201.163  

port:           port-l7-tcp-80  80/tcp  

endpoints:      10.0.68.240:80  

session affinity:   none  

1、查看指定pod的日志

kubectl logs &lt;pod_name&gt;

kubectl logs -f &lt;pod_name&gt; #类似tail -f的方式查看

2、查看上一个pod的日志

kubectl logs -p &lt;pod_name&gt;

3、查看指定pod中指定容器的日志

kubectl logs &lt;pod_name&gt; -c &lt;container_name&gt;

<code>[root@node5 ~]</code><code># kubectl logs --help</code>

<code>print the logs </code><code>for</code> <code>a container </code><code>in</code> <code>a pod. if the pod has only one container, the container name is optional.</code>

<code>usage:</code>

<code>  </code><code>kubectl logs [-f] [-p] pod [-c container] [flags]</code>

<code>aliases:</code>

<code>  </code><code>logs, log</code>

<code>examples:</code>

<code># return snapshot logs from pod nginx with only one container</code>

<code>$ kubectl logs nginx</code>

<code># return snapshot of previous terminated ruby container logs from pod web-1</code>

<code>$ kubectl logs -p -c ruby web-1</code>

<code># begin streaming the logs of the ruby container in pod web-1</code>

<code>$ kubectl logs -f -c ruby web-1</code>

<code># display only the most recent 20 lines of output in pod nginx</code>

<code>$ kubectl logs --</code><code>tail</code><code>=20 nginx</code>

<code># show all logs from pod nginx written in the last hour</code>

<code>$ kubectl logs --since=1h nginx</code>

其中kubernetes组件包括:

kube-apiserver

kube-controller-manager

pod扩容相关或rc相关

kube-scheduler

kubelet

pod生命周期相关:创建、停止等

etcd

也可以通过指定日志存放目录来保存和查看日志

--logtostderr=false:不输出到stderr

--log-dir=/var/log/kubernetes:日志的存放目录

--alsologtostderr=false:设置为true表示日志输出到文件也输出到stderr

--v=0:glog的日志级别

--vmodule=gfs*=2,test*=4:glog基于模块的详细日志级别

kubectl describe &lt;pod_name&gt; --namespace=&lt;namespace&gt;

查看该pod的事件。

正在下载镜像但拉取不下来(镜像拉取耗时太久)[一般都是该原因]

解决方法:

查看该pod所在宿主机与镜像仓库之间的网络是否有问题,可以手动拉取镜像

删除pod实例,让pod调度到别的宿主机上

kubectl get pods中pod状态一会running,一会不是,且restarts次数不断增加。

一般原因为容器启动命令不是阻塞式命令,导致容器运行后马上退出。

非阻塞式命令:

本身cmd指定的命令就是非阻塞式命令

将服务启动方式设置为后台运行

1、将命令改为阻塞式命令(前台运行),例如:zkserver.sh start-foreground

nohup $java_home/bin/java $java_opts -cp $classpath com.cnc.open.processor.main &amp;

改为:

$java_home/bin/java $java_opts -cp $classpath com.cnc.open.processor.main

文章参考:

《kubernetes权威指南》

继续阅读