dongpo0409 2019-09-14 15:28
浏览 484

K8s更改配置映射并更新应用日志级别

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

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

This is the code

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)
}

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 ?

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...

update

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

This is how the config env looks in k8s dashboard

enter image description here

  • 写回答

3条回答 默认 最新

  • duanlaican1849 2019-09-14 15:55
    关注

    I understand that viper can help with live changing out of configuration without restarting your app using the OnConfigChange event, but have you tried setting the log level in your base ConfigMap and then starting up the app, just to make sure it's not an issue with the OnConfigChange event firing and your particular config in k8s (and not your local environment where you've tested it works).

    Lastly, what is the difference between your local test environment (where this is working) and the other environment where this is not working?

    Are there any environment variables that might be affecting this differently in the one environment?

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog