I am creating several microservices on Azure (Kubernetes) and I have the following problem: if I do not put this command inside the YAML container it shows the message of BackOff or CrashloopBack and does not leave there.
The command I place is this:
command: [ "sleep" ]
args: [ "infinity" ]
This is the error that shows if I do not put this code
Warning BackOff 7s (x4 over 37s) kubelet, aks-agentpool-29153703-2 Back-off restarting the failed container
My DockerFile for one of this microservices:
FROM node:10
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD npm start EXPOSE 6060
My YAML:
apiVersion: apps/v1
kind: Deployment
metadata:
name: permis-deployment
labels:
app: permis-deployment
spec:
replicas: 1
selector:
matchLabels:
app: permis
template:
metadata:
labels:
app: permis
spec:
containers:
- name: permis
image: myacr.azurecr.io/permission-container:latest
command: [ "sleep" ]
args: [ "infinity" ]
ports:
- containerPort: 6060
apiVersion: v1
kind: Service
metadata:
name: permis-service
spec:
selector:
app: permis
ports:
- protocol: TCP
port: 6060
targetPort: 6060
type: LoadBalancer
Can you tell me what I am doing wrong or what is wrong?
Thank you!
If your app is running fine, change the Dockerfile by this one and re-create the image. Should work:
FROM node:10
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 6060
CMD ["npm", "start"]
i suggest you do the following:
containers:
- args:
- -ec
- sleep 1000
command:
- /bin/sh
invoking sleep directly didnt work for me
Related
I'm trying to deploy and image from Azure Cloud Register into a Kubernete cluster in Azure.
When I'm running a YAML file I get this error
[72320:0209/093322.154:ERROR:node_bindings.cc(289)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
The image is an application developed with Node & Express, it's just a "Hello World"
Here my Deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sharepointbackend-dev
namespace: dev
labels:
app: sharepointbackend-dev
spec:
replicas: 1
selector:
matchLabels:
app: sharepointbackend-dev
template:
metadata:
labels:
app: sharepointbackend-dev
spec:
containers:
- name: samplecontainer
image: sharepointbackendcontainerregister.azurecr.io/sharepointbackend:dev
imagePullPolicy: Always
ports:
- name: http
containerPort: 3000
protocol: TCP
And my DockerFile
FROM node:lts-alpine
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "tsconfig.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
RUN npm install\
&& npm install typescript -g
COPY . .
RUN tsc
EXPOSE 3000
RUN chown -R node /usr/src/app
USER node
CMD ["node", "dist/app.js"]
Locally, I can run succesfully. I build an image in my Docker Desktop and run it in a browser.
Any help?
Thanks in advance
I am using Azure Kubernetes cluster and using below as dockerfile.The container is deployed successfully in a Pod.
FROM node:12 as build-stage
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
RUN npm install
COPY ./ /app/
ARG URI
ENV REACT_APP_URI=$URI
RUN npm run build
EXPOSE 80
CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
deployment yml file:
kind: Deployment
apiVersion: apps/v1
metadata:
name: m-app
spec:
replicas: 1
selector:
matchLabels:
app: m-app
template:
metadata:
labels:
app: m-app
spec:
containers:
- name: metadata-app
image: >-
<url>
imagePullPolicy: Always
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: dockersecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: dockersecret
key: password
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: m-app
annotations:
service.beta.kubernetes.io/azure-load-balancer-internal: "true"
spec:
type: LoadBalancer
selector:
app: m-app
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
I would use the above yml file for the deployment and I want to access the app via pvt IP Address . By running the above yml I would get the service m-app with an External private IP but it is not accessible.
Then I tried with NodePort and for the same I replace above LoadBalancer snippet with below:
kind: Service
apiVersion: v1
metadata:
name: m-app
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
nodePort: 31000
selector:
app: m-app
Again I can not access the app from my browser with :
Could someone please assist. I suspected an issue with the Dockerfile as well and used different Dockerfile but no luck.(Please ignore yml indentations if any)
Finally the issue got fixed . I added below snippet in the Dockerfile:
FROM httpd:alpine
WORKDIR /var/www/html
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf
COPY --from=build-stage /app/build/ .
along with:
FROM node:12 as build-stage
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
RUN npm install
COPY ./ /app/
ARG URI
ENV REACT_APP_URI=$URI
RUN npm run build
I have a simple reactjs application and I am going to deploy this in my kubernetes cluster.
The Dockerfile for the reactjs application looks like below:
# build env
FROM node:13.12.0-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . ./
RUN npm run build
# production env
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
And I want to pass two environment variables through kubernetes as below:
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
selector:
matchLabels:
app: frontend
replicas: 1
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: 19950818/k8s:frontend
imagePullPolicy: Always
ports:
- containerPort: 80
env:
- name: REACT_APP_BACKEND_URL
valueFrom:
configMapKeyRef:
name: backend-configs
key: backend.url
- name: REACT_APP_BACKEND_PORT
valueFrom:
configMapKeyRef:
name: backend-configs
key: backend.port
I am accessing these variables with the NodeJs as below:
let url = process.env.REACT_APP_BACKEND_URL
let port = process.env.REACT_APP_BACKEND_PORT
But How can I modify the Dockerfile mentioned above to pass these two variable?
The js you have shown:
let url = process.env.REACT_APP_BACKEND_URL
let port = process.env.REACT_APP_BACKEND_PORT
runs in the user's browser, not in kubernetes or docker, therefore the environment variables you set on the server do not exist at the time the code is run. The kubernetes/docker container only serves the javascript as a file to the user's browser.
Basically, you cannot use environment variables in this way.
Hi All,
My node.js app is failing while I am trying to deploy it in Kubernetes using a docker image. The container in Kubernetes pod is getting created but it's immediately getting terminated, after executing the command "npm start" Here is the content of my dockerfile:
FROM node:13.12.0-alpine
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY nodejs/package.json ./
RUN npm install
RUN npm update
COPY nodejs/ .
COPY . ./
CMD ["npm", "start"]
Here is the content of the yaml file:
kind: Service
apiVersion: v1
metadata:
name: nodejs-service
spec:
selector:
app: nodejs
type: NodePort
ports:
- port: 3000
targetPort: 3000
nodePort: 30016
selector:
app: nodejs
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nodejs
name: nodejs-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- image: 336319716199.dkr.ecr.ap-south-1.amazonaws.com/ddp/nodejs-frontend:106
name: frontend-nodejs
command: ["npm", "start"]
ports:
- containerPort: 3000
Any suggestion will be highly appreciated ! Thanks in advance !
I am trying to setup a sample React application wired to a NodeJS backend as two pods in Kubernetes. This is the (mostly) the default CRA and NodeJS application with Express i.e. npx create-react-app my_app.
Both application runs fine locally through yarn start and npm app.js respectively. The React application uses a proxy defined in package.json to communicate with the NodeJS back-end.
React package.json
...
"proxy": "http://localhost:3001/"
...
React Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN yarn
COPY . .
CMD [ "yarn", "start" ]
NodeJS Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD [ "node", "app.js" ]
ui-deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sample-ui
namespace: my_namespace
spec:
replicas: 1
selector:
matchLabels:
app: my_namespace
component: sample-ui
template:
metadata:
labels:
app: my_namespace
component: sample-ui
spec:
containers:
-
name: sample-ui
image: xxx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
name: http
protocol: TCP
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
server-deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sample-server
namespace: my_namespace
spec:
replicas: 1
selector:
matchLabels:
app: my_namespace
component: sample-server
template:
metadata:
labels:
app: my_namespace
component: sample-server
spec:
containers:
-
name: sample-server
image: xxx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3001
name: http
protocol: TCP
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
ui-service
apiVersion: v1
kind: Service
metadata:
name: sample-ui
namespace: my_namespace
labels: {app: sample-ui}
spec:
type: LoadBalancer
selector:
component: sample-ui
ports:
- name: listen
protocol: TCP
port: 3000
server-service
apiVersion: v1
kind: Service
metadata:
name: sample-server
namespace: my_namespace
labels: {app: sample-server}
spec:
selector:
component: sample-server
ports:
- name: listen
protocol: TCP
port: 3001
Both services run fine on my system.
get svc
sample-server ClusterIP 10.19.255.171 <none> 3001/TCP 26m
sample-ui LoadBalancer 10.19.242.42 34.82.235.125 3000:31074/TCP 26m
However, my deployment for the CRA crashes multiple time despite indicating it is still running.
get pods
sample-server-598776c5fc-55jsz 1/1 Running 0 42m
sample-ui-c75ccb746-qppk2 1/1 Running 4 2m38s
I suspect that my React Dockerfile is improperly configured but I'm not sure how to write it to work with a NodeJS backend in kubernetes.
a) How can I setup my Dockerfile for my CRA such that it will run in a pod?
b) How can I setup my docker services and pods such that they communicate?
You will have to use come API gateway in front of your server or you can use ambassador from kubernetes.
Then you can get your client connected to server.
a) How can I setup my Dockerfile for my CRA such that it will run in a
pod?
React docker file is looking good you need to check why container of pod is failing.
Using kubectl describe pod <POD name> or debug more logs using the command kubectl logs <pod name>
How can I setup my docker services and pods such that they
communicate?
For this, you are on right track, how server and frontend will communicate in Kubernetes using the service name.
This might weird at first level but Kubernetes DNS takes care of it.
How if you have two service frontend (sample-ui) and backend (sample-server)
sample-ui will send the request to sample-server so they get connected that way.
You can also try this by going inside the sample-ui POD(container)
kubect exec -it sample-ui-c75ccb746-qppk2 -- /bin/bash
now you are inside of sample-ui container let's send request to sample-server from here
if curl not exist you can install it using the apk install curl or apt-get install curl or yum install curl
curl http://sample-server:3001
Magic you might see response from server.
So your while flow goes like
user coming to frontend load balancer service > calling sample-ui service > internally inside kubernetes cluster now your sample-ui calling the sample-server
All the service that you create inside the K8s will be accesible by it's name.