kubernetes创建yaml,pod服务一直处于 ContainerCreating状态的原因查找与解决

在看《k8s权威指南》实战过程中遇到了一个问题:
我打算发布一个mysql-rc.yaml到集群中,我的mysql-rc.yaml文件是这样的

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  #定义RC标签选择
  selector:
    app: mysql
  #定义Pod模板
  template:
    metadata:
      labels:
        app: mysql
    spec:
    #Pod内容器定义
      containers:
      - name: mysql
        image: mysql
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"

发布到集群出现问题:

[root@kubecon ~]# kubectl create -f mysql-rc.yaml
replicationcontroller "mysql" created

[root@kubecon ~]# kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
mysql     1         1         0         1m


[root@kubecon ~]# kubectl get pods
NAME          READY     STATUS              RESTARTS   AGE
mysql-bwnlk   0/1       ContainerCreating   0          1m

pod服务一直处于 ContainerCreating状态

Solution

查看相关日志

[root@kubecon ~]# kubectl describe po  mysql-bwnlk
Name:       mysql-bwnlk
Namespace:  default
Node:       127.0.0.1/127.0.0.1
Start Time: Thu, 04 Jan 2018 10:20:45 +0800
Labels:     app=mysql
Status:     Pending
IP:     
Controllers:    ReplicationController/mysql
Containers:
  mysql:
    Container ID:   
    Image:      daocloud.io/library/mysql
    Image ID:       
    Port:       3306/TCP
    State:      Waiting
      Reason:       ContainerCreating
    Ready:      False
    Restart Count:  0
    Volume Mounts:  <none>
    Environment Variables:
      MYSQL_ROOT_PASSWORD:  123456
Conditions:
  Type      Status
  Initialized   True 
  Ready     False 
  PodScheduled  True 
No volumes.
QoS Class:  BestEffort
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From            SubObjectPath   Type        Reason      Message
  --------- --------    -----   ----            -------------   --------    ------      -------
  3m        3m      1   {default-scheduler }        Normal      Scheduled   Successfully assigned mysql-bwnlk to 127.0.0.1
  3m        20s     5   {kubelet 127.0.0.1}     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)"

  2m    8s  10  {kubelet 127.0.0.1}     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\""

关键问题出现

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)"

看到registry.access.redhat.com/rhel7/pod-infrastructure:latest我感觉很怪,我之前已经设置好仓库为grc.io,为什么去拉取这个镜像,怀疑是不是什么没有安装好。

尝试运行docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest,提示redhat-ca.crt: no such file or directory。

[root@kubecon ~]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
Trying to pull repository registry.access.redhat.com/rhel7/pod-infrastructure ... 
open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory

ls查看该文件是个软连接,链接目标是/etc/rhsm

[root@kubecon ~]# ls -l /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt
lrwxrwxrwx 1 root root 27 1月   3 21:06 /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt -> /etc/rhsm/ca/redhat-uep.pem

查看没有rhsm

[root@kubecon ~]# ls /etc/rhsm
ls: 无法访问/etc/rhsm: 没有那个文件或目录

尝试安装yum install rhsm

yum install rhsm -y
#如果上一步没有源的话,请试试
#yum -y install python-rhsm
#如果上一步还不能安装rhsm的话请下载rpm包安装
#https://www.rpmfind.net/linux/rpm2html/search.php?query=python-rhsm
#wget https://www.rpmfind.net/linux/centos/7.4.1708/os/x86_64/Packages/python-rhsm-1.19.9-1.el7.x86_64.rpm
#rpm -ivh python-rhsm-*.rpm

安装之后查看产生了/etc/rhsm文件夹,再次运行kubectl get pods
问题解决~

[root@kubecon ~]# kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
mysql-bwnlk   1/1       Running   0          52m

推荐阅读更多精彩内容