Error - Back-off restarting failed container while creating a service

1/1/2019

As stated in the title I am experiencing error

Back-off restarting failed container while creating a service

I've seen questions on Stack Overflow but I am still not sure how to resolve it.

This is my deployment yaml file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: book-api
spec:
  replicas: 1
  revisionHistoryLimit: 10
  template:
    metadata:
      name: book-api
      labels:
        app: book-api
    spec:
      containers:
      - name: book-api
        image: newmaster/kubecourse-books:v1
        ports:
        - name: http
          containerPort: 3000

while the service deployment file is:

kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    app: myapp
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8081
      # Port to forward to inside the pod
      targetPort: 8080
      # Port accessible outside cluster
      nodePort: 30000
  type: LoadBalancer

This is my Dockerfile:

FROM node:alpine

# Create app directory
WORKDIR /src

# Install app dependencies
COPY package.json /src/
COPY package-lock.json /src/

RUN npm install

# Bundle app source
ADD . /src

RUN npm run build

EXPOSE 3000

CMD [ "npm", "run serve" ]

I have no idea how to resolve this issue, I am newbie in the Kubernetes and DevOps world. Repo is over here: https://github.com/codemasternode/BookService.Kubecourse.git

-- newmaster
docker
kubernetes
node.js

1 Answer

1/2/2019

I tried to run your deployment locally and this is what the log shown:

kubectl log book-api-8d98bf6d5-zbv4q

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    clean-install, clean-install-test, completion, config,
    create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
    edit, explore, get, help, help-search, hook, i, init,
    install, install-ci-test, install-test, it, link, list, ln,
    login, logout, ls, outdated, owner, pack, ping, prefix,
    profile, prune, publish, rb, rebuild, repo, restart, root,
    run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami

npm <command> -h  quick help on <command>
npm -l            display full usage info
npm help <term>   search for help on <term>
npm help npm      involved overview

Specify configs in the ini-formatted file:
    /root/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@6.5.0-next.0 /usr/local/lib/node_modules/npm

It seems no command is running by default with the newmaster/kubecourse-books:v1

I guess if you want to run the default npm command, you could run the following deploy config (note the command value):

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: book-api
spec:
  replicas: 1
  revisionHistoryLimit: 10
  template:
    metadata:
      name: book-api
      labels:
        app: book-api
    spec:
      containers:
      - name: book-api
        image: newmaster/kubecourse-books:v1
        command: ["npm", "start"]
        ports:
        - name: http
          containerPort: 3000
-- Quentin Revel
Source: StackOverflow