我可以使用Kubernetes创建文件或替换文件的内容吗? [英] Can I create a file or replace content of a file using Kubernetes?

查看:90
本文介绍了我可以使用Kubernetes创建文件或替换文件的内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React应用程序,该应用程序使用在构建步骤中准备的静态文件托管在Nginx容器中.我遇到的问题是,API URL随后被硬编码在js文件中,并且当我要将应用程序部署到不同的环境时遇到问题.

I have a react application that is hosted in a nginx container using static files that are prepared in a build step. The problem I run in to is that the API URL is then hard coded in the js files and I get a problem when I want to deploy the application to different environments.

因此,基本上,我已经在公共目录中放置了一个带有localhost API URL变量的config.js文件,然后将其加载到index.html文件部分的应用程序中.这适用于本地环境.当我想将其部署到测试或生产环境时,就会出现问题.

So basically I have put a config.js file with the localhost API URL variable in the public directory which is then loaded in the application in the section of the index.html file. This works for the local environment. The problem comes when I want to deploy it to the test or production environment.

我发现可以使用带有卷挂载的configMap,但是据我了解,这需要我为每个环境预先准备一个文件.我希望能够使用在Azure DevOps库中设置的变量来填充API URL值.

I have found out that it is possible to use a configMap with volume mounts, but that requires me to prepare one file for each environment in advance as I understand it. I want to be able to use the variables I have set in my Azure DevOps library to populate the API URL value.

所以我的问题是,是否可以使用Kuberentes/Helm替换nginx容器中config.js文件中的值,还是可以利用Azure DevOps管道任务替换pre-准备好config.js文件并使用Kubernetes挂载它?

So my question is if there is a way to replace the values in the config.js file in the nginx container using Kuberentes/Helm or if I can make use of a Azure DevOps pipeline task to replace the content of a pre-prepared config.js file and mount that using Kubernetes?

不确定我想做什么,但希望您能理解...

Not sure if it is clear what I want to do, but hopefully you can understand it...

config.js

config.js

window.env = {
    API_URL: 'http://localhost:8080'
};

index.html

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>My application</title>
    <!--
      config.js provides all environment specific configuration used in the client
    -->
    <script src="%PUBLIC_URL%/config.js"></script>
  </head>
  ...

推荐答案

我最终要做的是像这样设置它:

What I ended up doing was setting it up like this:

首先,我添加了 configmap.yaml 来生成config.js文件

First I added a configmap.yaml to generate the config.js file

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-frontend
data:
  config.js: |-
    window.env = {
      API_URL: "{{ .Values.service.apiUrl }}"
    }

Values.service.apiUrl来自打包和部署Helm图表"任务--set service.apiUrl=$(backend.apiUrl)

Values.service.apiUrl comes from the arguments provided in the "Package and deploy Helm charts" task --set service.apiUrl=$(backend.apiUrl)

然后我在 deployment.yaml 中添加了卷挂载以替换nginx容器中的config.js文件

Then I added a volume mount in the deployment.yaml to replace the config.js file in the nginx container

...
containers:
  ...
  volumeMounts:
    - name: config-frontend-volume
      readOnly: true
      mountPath: "/usr/share/nginx/html/config.js"
      subPath: "config.js"
volumes:
  - name: config-frontend-volume
    configMap:
      name: config-frontend

这可以解决问题,现在我可以根据要部署到的环境从Azure DevOps管道控制变量.

This did the trick and now I can control the variable from the Azure DevOps pipeline based on the environment I'm deploying to.

这篇关于我可以使用Kubernetes创建文件或替换文件的内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆