简体   繁体   English

Kubernetes 部署。yaml

[英]Kubernetes Deployment.yaml

I have a docker image built and run with npm token how to convert this to deployment in Kubernetes.我有一个使用 npm 令牌构建和运行的 docker 映像,如何将其转换为 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

I'm using azure pipelines where image is build, pushed to azure container registry, then pulled and deployed to aks (azure kubernetes service).我正在使用构建映像的 azure 管道,推送到 azure 容器注册表,然后拉取并部署到 aks(天蓝色 kubernetes 服务)。 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

I'm using azure pipelines where image is build, pushed to azure container registry, then pulled and deployed to aks (azure kubernetes service).我正在使用构建映像的 azure 管道,推送到 azure 容器注册表,然后拉取并部署到 aks(天蓝色 kubernetes 服务)。 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

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.我认为您正朝着正确的方向前进,需要纠正的一件事是“NPM_TOKEN”在您的 docker 脚本中作为环境变量编写,但在您的 YAML 文件中,您将其定义为 arg。 So the correct deployment.yaml file would be:所以正确的 deployment.yaml 文件应该是:

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:此外,您必须将容器端口转发到您的本地主机才能访问您的应用程序,下面的命令等效于 docker 运行命令中的“-p 3001:3001”:

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.一旦设置了端口转发,您就可以在 http://localhost:3001 访问您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM