kubectl get pods一直显示ContainerCreating

注:本文基于CentOS 7.4编写

背景

根据yaml文件创建rc后,查询pods状态时,一直显示ContainerCreating,

[root@CentOS-7-4 /home/k8s]# kubectl get pods
NAME                 READY     STATUS              RESTARTS   AGE
redis-master-5zn2z   0/1       ContainerCreating   0          13s

解决方案

查看这个pods的详细信息,

[root@CentOS-7-4 /home/k8s]# kubectl describe pod redis-master-5zn2z
Name:		redis-master-5zn2z
Namespace:	default
Node:		192.168.0.29/192.168.0.29
Start Time:	Wed, 16 Jan 2019 07:50:04 -0500
Labels:		name=redis-master
Status:		Pending
IP:		
Controllers:	ReplicationController/redis-master
Containers:
  master:
    Container ID:		
    Image:			kubeguide/redis-master
    Image ID:			
    Port:			6379/TCP
    State:			Waiting
      Reason:			ContainerCreating
    Ready:			False
    Restart Count:		0
    Volume Mounts:		<none>
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True 
  Ready 	False 
  PodScheduled 	True 
No volumes.
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From				SubObjectPath	TypeReason		Message
  ---------	--------	-----	----				-------------	--------	------		-------
  1m		1m		1	{default-scheduler }				Normal		Scheduled	Successfully assigned redis-master-5zn2z to 192.168.0.29
  1m		18s		4	{kubelet 192.168.0.29}			Warning		FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest\""

  1m	6s	4	{kubelet 192.168.0.29}		Warning	FailedSync	Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

可见,是缺少了/etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt文件,

[root@CentOS-7-4 /home/k8s]# ll /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt
lrwxrwxrwx. 1 root root 27 May 16  2018 /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt -> /etc/rhsm/ca/redhat-uep.pem
[root@CentOS-7-4 /home/k8s]# ll /etc/rhsm/ca/redhat-uep.pem
ls: cannot access /etc/rhsm/ca/redhat-uep.pem: No such file or directory

使用yum查询/etc/rhsm/ca/redhat-uep.pem是哪个rpm包提供的,

[root@CentOS-7-4 /home/k8s]# yum provides */redhat-uep.pem
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
python-rhsm-certificates-1.19.10-1.el7_4.x86_64 : Certificates required to communicate with a
                                                : Red Hat Unified Entitlement Platform
Repo        : base
Matched from:
Filename    : /etc/rhsm/ca/redhat-uep.pem

因此,安装python-rhsm-certificates组件即可。

但事实并不如此。。。。

[root@CentOS-7-4 /home/k8s]# yum install python-rhsm-certificates -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
Package python-rhsm-certificates is obsoleted by subscription-manager-rhsm-certificates, trying to install subscription-manager-rhsm-certificates-1.21.10-3.el7.centos.x86_64 instead

python-rhsm-certificates不再被允许安装,改而使用subscription-manager-rhsm-certificates。其实用哪个倒无所谓,但是新的这个组件没有提供/etc/rhsm/ca/redhat-uep.pem文件,所以还是会出现ContainerCreating的情况。

所以只能用以下方式安装,也就是直接下载python-rhsm-certificates,然后手动安装。

[root@CentOS-7-4 /home/k8s]# yumdownloader python-rhsm-certificates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm                   |  41 kB  00:00:00     
[root@CentOS-7-4 /home/k8s]# ls
.  ..  python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm 
[root@CentOS-7-4 /home/k8s]# rpm -e subscription-manager-rhsm-certificates
[root@CentOS-7-4 /home/k8s]# rpm -ivh python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:python-rhsm-certificates-1.19.10-################################# [100%]
[root@CentOS-7-4 /home/k8s]# ll /etc/rhsm/ca/redhat-uep.pem
-rw-r--r-- 1 root root 7732 Oct 19  2017 /etc/rhsm/ca/redhat-uep.pem

同时需要注意的是,其他所有结点也都要这样安装,因为每个结点都有可能需要运行这个pod。

都安装好后,稍等一会( 一分钟左右)再查看,就可以看到running状态的pod了。

[root@CentOS-7-4 /home/k8s]# kubectl get pods
NAME                 READY     STATUS    RESTARTS   AGE
redis-master-nslvc   1/1       Running   0          12h
### 如何使用 `kubectl get pods` 查看所有 Pods 的状态 要查看 Kubernetes 集群中的所有 Pod 状态,可以按照以下方式操作: #### 基本命令结构 运行以下命令来获取当前命名空间下的所有 Pod 列表及其状态: ```bash kubectl get pods ``` 如果希望查看集群中 **所有命名空间** 下的 Pod,则需要加上 `-A` 或者 `--all-namespaces` 参数: ```bash kubectl get pods -A ``` 此命令会返回整个集群范围内所有的 Pod 及其对应的命名空间、状态和其他相关信息[^4]。 #### 解决常见问题 当执行 `kubectl get pods` 后提示 `No resources found.` 时,可能是因为当前上下文中默认的命名空间下没有任何资源存在。此时可以通过切换到其他命名空间或者全局扫描的方式解决问题[^3]。 对于某些情况下 Pod 处于 `ContainerCreating` 状态较长时间未完成初始化的情况,建议通过扩展诊断工具进一步排查原因。例如,可通过如下命令检查特定 Pod 的事件日志和描述详情: ```bash kubectl describe pod <pod-name> ``` 这有助于定位容器创建失败的具体错误信息[^2]。 另外,若始终无法发现任何可用的 Pod 资源列表,还应确认 API Server 是否配置正确以及权限设置是否适当等问题[^5]。 #### 示例输出解释 成功执行上述命令后,通常可以获得类似于下面这样的表格形式的结果: | NAME | READY | STATUS | RESTARTS | AGE | |-------------------------------|----------|-------------|------------|-----------| | example-pod | 1/1 | Running | 0 | 1h | 其中各列含义分别为: - **NAME**: Pod 名称; - **READY**: 已就绪的容器数 / 总计容器数量; - **STATUS**: 当前 Pod 运行状况 (如 Pending,Running,Succeeded,Failed,CrashLoopBackOff 等); - **RESTARTS**: 容器重启次数统计; - **AGE**: 创建至今的时间长度。 ```python def check_pods_status(): import subprocess try: result = subprocess.run(['kubectl', 'get', 'pods','-A'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if result.returncode != 0: print(f"Error occurred while fetching pods status:\n{result.stderr.decode('utf-8')}") else: print(result.stdout.decode('utf-8')) except Exception as e: print(e) check_pods_status() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值