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 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型