Dotnet Core 项目生产环境配置文件在K8s上的部署
一般来说生产环境的配置文件appsetting.Production.json不会放到源代码库中。传统方式下,该文件可以在项目发布后手动添加到生产环境下。在Docker环境下,镜像中不宜包含此类敏感信息,可以通过K8s的Secret和Volume或者env变量来实现.
- 首先修改项目的Startup.cs
1 public class Startup 2 { 3 private IHostingEnvironment CurrentEnvironment { get; set; } 4 5 6 public Startup(IConfiguration configuration,IHostingEnvironment env) 7 { 8 9 //Configuration = configuration; 10 11 var builder = new ConfigurationBuilder() 12 .SetBasePath(env.ContentRootPath) 13 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 14 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 15 .AddJsonFile("secrets/appsettings.secrets.json", optional: true) 16 .AddEnvironmentVariables(); 17 Configuration = builder.Build(); 18 19 CurrentEnvironment = env; 20 21 } 22 }
- 运行以下命令创建secrete
kubectl create secret generic secret-appsettings --from-file=./appsettings.secrets.json
- 编写pod.yml
apiVersion: v1 kind: Pod metadata: name: web labels: app: web spec: containers: - name: web image: xxx/project:latest ports: - containerPort: 80 volumeMounts: - name: secrets mountPath: /app/secrets readOnly: true env: - name: "ASPNETCORE_ENVIRONMENT" value: "Staging" imagePullSecrets: - name: regsecret volumes: - name: secrets secret: secretName: secret-appsettings
posted on 2019-03-18 15:01 leonworld2011 阅读(688) 评论(1) 编辑 收藏 举报