K8s更改配置映射并更新应用日志级别 [英] K8s Change config map and update app log level

查看:471
本文介绍了K8s更改配置映射并更新应用日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改在K8S上运行的Golang应用程序上的日志配置, 我在本地尝试了以下代码,它可以按预期工作 我正在使用毒蛇监视配置文件的更改

I want to change config of log on Golang application which run on K8S, I’ve tried the following code locally and it works as expected I'm using viper to watch for config file changes

这是带有日志配置的配置图

This is the config map with the log configuration

apiVersion: v1
kind: ConfigMap
data:
  config.yaml: 'log.level: error'
metadata:
  name: app-config
  namespace: logger

在部署Yaml中,我添加了以下内容

In the deployment yaml I’ve added the following

...
spec:
  containers:
    - name: gowebapp
      image: mvd/myapp:0.0.3
      ports:
        - containerPort: 80
      envFrom:
        - configMapRef:
            name: app-config

这是代码

package configuration

import (
   "fmt"
   "os"
   "strings"

   "github.com/fsnotify/fsnotify"
   "github.com/sirupsen/logrus"
   "github.com/spf13/viper"
)

const (
   varLogLevel     = "log.level
"
   varPathToConfig = "config.file"
)

type Configuration struct {
   v *viper.Viper
}

func New() *Configuration {
   c := Configuration{
      v: viper.New(),
   }

   c.v.SetDefault(varPathToConfig, "./config.yaml")
   c.v.SetDefault(varLogLevel, "info")
   c.v.AutomaticEnv()
   c.v.SetConfigFile(c.GetPathToConfig())
   err := c.v.ReadInConfig() // Find and read the config file
   logrus.WithField("path", c.GetPathToConfig()).Warn("loading config")
   if _, ok := err.(*os.PathError); ok {
      logrus.Warnf("no config file '%s' not found. Using default values", c.GetPathToConfig())
   } else if err != nil { // Handle other errors that occurred while reading the config file
      panic(fmt.Errorf("fatal error while reading the config file: %s", err))
   }
   setLogLevel(c.GetLogLevel())
   c.v.WatchConfig()
   c.v.OnConfigChange(func(e fsnotify.Event) {
      logrus.WithField("file", e.Name).Warn("Config file changed")
      setLogLevel(c.GetLogLevel())
   })
   return &c
}

// GetLogLevel returns the log level
func (c *Configuration) GetLogLevel() string {
   s := c.v.GetString(varLogLevel)
   return s
}

// GetPathToConfig returns the path to the config file
func (c *Configuration) GetPathToConfig() string {
   return c.v.GetString(varPathToConfig)
}

func setLogLevel(logLevel string) {
   logrus.WithField("level", logLevel).Warn("setting log level")
   level, err := logrus.ParseLevel(logLevel)
   if err != nil {
      logrus.WithField("level", logLevel).Fatalf("failed to start: %s", err.Error())
   }
   logrus.SetLevel(level)
}

现在,当我再次应用yaml文件并将值从error更改为warndebug等时, 没什么变化...知道我在这里错过了什么吗?

Now when I apply the yaml file again and changing the value from error to warn or debug etc Nothing change … any idea what I miss here ?

我在K8S仪表板中看到配置映射已分配给应用程序,并且当我更改该值时,我看到环境已更改...

I see in the K8S dashboard that the config map is assigned to the application and when I change the value I see that the env was changed...

更新

在本地运行时,我使用以下配置进行测试 但是在使用配置映射时,我根据配置映射的规范使用了data条目...

when run it locally I use the following config just for testing but when using config map I've used the data entry according to the spec of configmap ...

apiVersion: v1
kind: ConfigMap
log.level: 'warn'
#data:
#  config.yaml: 'log.level: error'
metadata:
  name: app-config

这是配置环境在k8s仪表板中的外观

推荐答案

envFrom从配置映射中创建环境变量.没有文件更改.如果您执行该容器操作,您可能会看到一个名为config.yaml或CONFIG.YAML或类似名称的环境变量(不知道它是否适用于点).

envFrom creates environment variables from the config map. There is no file that changes. If you exec into the container you'll probably see an environment variable named config.yaml or CONFIG.YAML or similar (don' t know if it works with dots).

如果将config.yaml作为文件挂载到pod中,则可能会更好,例如

You are probably better of if you mount config.yaml as a file inside your pods, like this Add ConfigMap data to a Volume

这篇关于K8s更改配置映射并更新应用日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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