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
Related
I am deploying nodejs application on kubernetes, After deployment pod is up and running, but when I am trying to access the application through ingress it is giving 502 bad gateway error.
Dockerfile
FROM node:14
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 3123
CMD [ "node", "index.js" ]
Deployment.yaml
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "node-development"
namespace: "development"
spec:
selector:
matchLabels:
app: "node-development"
replicas: 1
template:
metadata:
labels:
app: "node-development"
spec:
containers:
-
name: "node-development"
image: "xxx"
imagePullPolicy: "Always"
env:
-
name: "NODE_ENV"
value: "development"
ports:
-
containerPort: 47033
service.yaml
---
apiVersion: "v1"
kind: "Service"
metadata:
name: "node-development-service"
namespace: "development"
labels:
app: "node-development"
spec:
ports:
-
port: 47033
targetPort: 3123
selector:
app: "node-development"
ingress.yaml
---
apiVersion: "networking.k8s.io/v1"
kind: "Ingress"
metadata:
name: "node-development-ingress"
namespace: "development"
annotations:
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
spec:
rules:
-
host: "xxxx"
http:
paths:
-
backend:
service:
name: "node-development"
port:
number: 47033
path: "/node-development/(.*)"
pathType: "ImplementationSpecific"
With ingress or even with the pod cluster ip I am not being able to access application it is throwing 502 bad gateway nginx
Issue got resolved, I am using SSL in my application as a result it was not re-directing with the given ingress url.
Need to add below annotation in ingress.yaml file.
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
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.
I'm trying to access my Angular front-end deployed to an AKS cluster at Path / with service lawyerlyui-service. The cluster is using nginx deployed via HELM with the official chart (https://kubernetes.github.io/ingress-nginx)
I have other backend .net core services deployed and I can access these via the ingress.
However when i try access the Angular application at https://uat.redactedapp.co.za i get the following error (taken from nginx pod logs)
Below are the configurations and log for nginx, Dockerfile, deployment.yml and ingress.yml
NGINX Log
2021/01/29 20:31:59 [error] 1304#1304: *7634340 connect() failed (111: Connection refused) while connecting to upstream, client: 10.244.0.1,
server: uat.redactedapp.co.za, request: "GET / HTTP/2.0", upstream: "http://10.244.0.88:80/", host: "uat.redactedapp.co.za"
Dockerfile
FROM node:8.12.0-alpine
EXPOSE 80
RUN npm -v
RUN mkdir -p /usr/src
WORKDIR /usr/src
# To handle 'not get uid/gid'
RUN npm config set unsafe-perm true
RUN npm install -g \
typescript#2.8.3 \
#angular/compiler-cli \
#angular-devkit/core
RUN npm install -g #angular/cli#7.0.3
RUN ln -s /usr/src/node_modules/#angular/cli/bin/ng /bin/ng
COPY package.json /usr/src/
RUN npm install
COPY . /usr/src
CMD ["ng", "build", "--configuration", "uat"]
Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: redacted-ingress
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "GET, PUT, POST, DELETE, PATCH, OPTIONS"
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
nginx.ingress.kubernetes.io/proxy-body-size: 50m
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
tls:
- hosts:
- uat.redactedapp.co.za
secretName: secret-tls
rules:
- host: uat.redactedapp.co.za
http:
paths:
- path: /otp-api(/|$)(.*)
pathType: Prefix
backend:
service:
name: otpapi-service
port:
number: 80
- path: /search-api(/|$)(.*)
pathType: Prefix
backend:
service:
name: searchapi-service
port:
number: 80
- path: /notifications-api(/|$)(.*)
pathType: Prefix
backend:
service:
name: notificationsapi-service
port:
number: 80
- path: /user-api(/|$)(.*)
pathType: Prefix
backend:
service:
name: userapi-service
port:
number: 80
- path: /insurance-api(/|$)(.*)
pathType: Prefix
backend:
service:
name: insuranceapi-service
port:
number: 80
- path: /client-api(/|$)(.*)
pathType: Prefix
backend:
service:
name: clientsapi-service
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: lawyerlyui-service
port:
number: 80
Deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: lawyerlyui
spec:
selector:
matchLabels:
app: lawyerlyui
replicas: 1
template:
metadata:
labels:
app: lawyerlyui
spec:
containers:
- name: lawyerlyui
image: redacted.azurecr.io/lawyerly:latest
ports:
- containerPort: 80
imagePullSecrets:
- name: uat-acr-auth
---
apiVersion: v1
kind: Service
metadata:
name: lawyerlyui-service
spec:
type: ClusterIP
selector:
app: lawyerlyui
ports:
- name: http
protocol: TCP
# Port accessible inside cluster
port: 80
# Port to forward to inside the pod
targetPort: 80
So after doing so more reading an looking at #mdaniels comments I've created a new Dockerfile.uat. The previous file was only building the src code but never serving it.
As I understand, you need to serve the built Angular code, this is no longer giving me a 502 Bad Gateway.
# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM node:10.8.0 as build-stage
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY ./ /app/
ARG configuration=uat
EXPOSE 80
RUN npm run build --configuration $configuration
# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
#Copy ci-dashboard-dist
COPY --from=build-stage /app/dist/lfa/ /usr/share/nginx/html
#Copy default nginx configuration
COPY ./nginx/nginx-custom.conf /etc/nginx/conf.d/default.conf
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 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