简体   繁体   English

如何在 .NET Core 中将 kubernetes secret 设置为连接字符串密码?

[英]How to set kubernetes secret as as connection string password in .NET Core?

I have a connection string in my .NET Core application that is used to connect to a database.我的 .NET Core 应用程序中有一个连接字符串,用于连接到数据库。

{
    "ConnectionStrings": {
        "BloggingDatabase": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
    },
}

I can use plain text password in development.我可以在开发中使用纯文本密码。 But in kubernetes deployment, I am creating secrets as environment variables.但是在 kubernetes 部署中,我将秘密创建为环境变量。

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  db_password: MWYyZDFlMmU2N2Rm

and pod like this:和这样的吊舱:

apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: my_app:v1
    env:
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: db_password

But my connection string is not separated the password and username.但是我的连接字符串没有将密码和用户名分开。 So how can I replace it in Kubernetes deployment?那么如何在 Kubernetes 部署中替换它呢?

As per the k8's official doc , you need to create a config file that holds your username and password as secret.根据k8 的官方文档,您需要创建一个配置文件,将您的用户名和密码保密。 As per your config you missed to add the username to which the password should hold.根据您的配置,您错过了添加密码应该保存的用户名。

Below is the sample configuration, change accordingly:以下是示例配置,相应地进行更改:

apiVersion: v1
kind: Secret
metadata:
  name: test-secret
type: Opaque
data:
  username: bXktYXBw
  password: Mzk1MjgkdmRnN0pi

Refer to this doc for more information and use cases.有关更多信息和用例,请参阅此文档

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

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