nodejs kubernetes unable to perform rolling update

10/14/2019

To deploy an express node.js api in production I wanted to use kubernetes These steps were followed:

  • created cluster in google cloud
  • clone the code to the cluster from git
  • docker build -t gcr.io/[GCLOUDID]/app:v1 .
  • docker pushgcr.io/[GCLOUDID]/app:v1
  • kubectl run app --image=gcr.io/[GCLOUDID]/app:v1

This runs fine, but when i want to update the code, i'm kinda lost. i tried to build a v2(docker build -t gcr.io/[GCLOUDID]/app:v2 . ) and setting the image via kubectl set image deployments/app app=gcr.io/[GCLOUDID]/app:v2

this method throws all kind of errors(imagepullbackoff, crashloopbackoff for example)

this dockerfile was used:

FROM node:11-alpine

WORKDIR /

COPY . /
RUN rm -rf node_modules
RUN rm -rf package-lock.json

RUN apk update && apk upgrade \
   && apk add --no-cache git \
   && apk --no-cache add --virtual builds-deps build-base python \
   && npm rebuild bcrypt --build-from-source

# Install imagemagick with support to native library
RUN apk add --no-cache file
RUN apk --update add imagemagick

RUN npm install 

ENV TYPE=docker

EXPOSE 80

CMD [ "npm", "start" ]
-- Achiel Volckaert
google-cloud-platform
google-kubernetes-engine
kubernetes
node.js

1 Answer

10/14/2019

Use kubernetes declarative syntax by building a yaml which embeds your container in a deployment. This is cleaner and will keep you organized. Whenever you want to update, just change the image name and run kubectl apply -f deployment.yaml. Not only will you update the image but also do it in a blue green way. Check this example https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
-- Rodrigo Loza
Source: StackOverflow