I'm new to Kubernetes, I have a docker image built and run with npm token how to convert this to deployment in Kubernetes. Below is my image and command to build and run.
#First Build
FROM node:16.17-bullseye
ARG NPM_TOKEN
WORKDIR /usr/src/app
#COPY package*.json .npmrc ./
COPY package*.json /usr/src/app/
COPY .npmrc /usr/src/app/
RUN npm update
RUN npm install
COPY . .
CMD ["npm","run","main"]
Buld command:
docker build . -t image --build-arg NPM_TOKEN=<token>
Run command:
docker run -p 3001:3001 -it -e NPM_TOKEN="Token" imageid
CodePudding user response:
I'm using azure pipelines where image is build, pushed to azure container registry, then pulled and deployed to aks (azure kubernetes service). Below is my deployment file :
apiVersion : apps/v1
kind: Deployment
metadata:
name: microa
namespace: ingress-basic
spec:
replicas: 1
selector:
matchLabels:
app: microa
template:
metadata:
labels:
app: microa
spec:
containers:
- name: microa
image: acrURL/microa
args: ["NPM_TOKEN=<token>"]
resources:
requests:
memory: "256Mi"
cpu: "1000m"
limits:
memory: "512Mi"
cpu: "1500m"
ports:
- containerPort: 3001
CodePudding user response:
I think you're going in the right direction, one thing that needs to be corrected is that the "NPM_TOKEN" is written as an environment variable in your docker script, but in your YAML file, you defined it as an arg. So the correct deployment.yaml file would be:
apiVersion : apps/v1
kind: Deployment
metadata:
name: microa
namespace: ingress-basic
spec:
replicas: 1
selector:
matchLabels:
app: microa
template:
metadata:
labels:
app: microa
spec:
containers:
- name: microa
image: acrURL/microa
env:
- name: NPM_TOKEN
value: "<token>"
resources:
requests:
memory: "256Mi"
cpu: "1000m"
limits:
memory: "512Mi"
cpu: "1500m"
ports:
- containerPort: 3001
Additionally, you have to port forward the container port to your localhost in order to access your application, the command below is equivalent to "-p 3001:3001" in your docker run command:
kubectl port-forward -n ingress-basic pods/microa 3001:3001
Once the port-forward is set up, you would be able to access your application at http://localhost:3001 .