如何修复Kubernetes创建容器错误

时间:2019-10-15 08:54:54

标签: docker kubernetes

当我尝试从docker镜像创建pod时,出现create container错误。这是我的pod.yml文件

apiVersion: v1
kind: Pod
metadata:
  name: client
spec:
  containers:
    - image: es-tutorial_web
      imagePullPolicy: Never
      name: es-web
      ports:
        - containerPort: 3000
    - image: es-tutorial_filebeat
      imagePullPolicy: Never
      name: es-filebeat

docker-compose.yml

version: '3.7'
services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    container_name: test-app
    working_dir: /usr/src/app
    command: /bin/bash startup.sh
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000"
    networks:
      - logs
  filebeat:
    build:
      context: .
      dockerfile: filebeat/Dockerfile
    container_name: test-filebeat
    volumes:
      - .:/usr/src/app
    depends_on:
      - web
    networks:
      - logs

networks:
  logs:
    driver: bridge

kubectl获得豆荚

client                               1/2     CreateContainerError   0          24m

kubectl描述客户

Name:         client
Namespace:    default
Priority:     0
Node:         minikube/10.0.2.15
Start Time:   Tue, 15 Oct 2019 15:29:02 +0700
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"client","namespace":"default"},"spec":{"containers":[{"image":"es-tut...
Status:       Pending
IP:           172.17.0.8
Containers:
  es-web:
    Container ID:   
    Image:          es-tutorial_web
    Image ID:       
    Port:           3000/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CreateContainerError
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5ftqt (ro)
  es-filebeat:
    Container ID:   docker://4174e7eb5bf8abe7662698c96d7945a546503f3c5494cad2ae10d2a8d4f02762
    Image:          es-tutorial_filebeat
    Image ID:       docker://sha256:4e3d24ef67bb05b2306eb49eab9d8a3520aa499e7a30cf0856b8658807b49b57
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 15 Oct 2019 15:29:03 +0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5ftqt (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-5ftqt:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-5ftqt
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  13m                   default-scheduler  Successfully assigned default/client to minikube
  Normal   Pulled     13m                   kubelet, minikube  Container image "es-tutorial_filebeat" already present on machine
  Normal   Created    13m                   kubelet, minikube  Created container es-filebeat
  Normal   Started    13m                   kubelet, minikube  Started container es-filebeat
  Warning  Failed     11m (x11 over 13m)    kubelet, minikube  Error: Error response from daemon: No command specified
  Normal   Pulled     3m26s (x50 over 13m)  kubelet, minikube  Container image "es-tutorial_web" already present on machine

Dockerfile

...
RUN apt-get update && apt-get install -y curl

RUN curl -sL "https://deb.nodesource.com/setup_12.x" | bash - && apt-get install -y nodejs && echo 'node' > node
RUN mkdir -p /usr/src/app
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN chmod +x startup.sh
RUN npm install -g nodemon

startup.sh

if [ ! -d /usr/src/app/node_modules ]; then
  echo "Install dependencies..."
  cd /usr/src/app && npm install --no-bin-links
fi
cd /usr/src/app && nodemon -L bin/www

我哪里错了?请帮助我

1 个答案:

答案 0 :(得分:1)

我相信您在Dockerfile中缺少CMDENTRYPOINT。他们需要运行容器。

应将其设置为计划在执行容器时运行的默认命令。

如果startup.sh是您运行该应用程序的脚本,请尝试以下操作:

ENTRYPOINT /usr/src/app/startup.sh

或将您的Dockerfile修改为:

# ...
WORKDIR /usr/src/app
RUN chmod +x startup.sh
RUN npm install -g nodemon
RUN test ! -d /usr/src/app/node_modules && npm install --no-bin-links
ENTRYPOINT ["/usr/src/app/nodemon", "-L", "bin/www"]