kubernetes 侧车集装箱未打印日志

qltillow  于 2022-11-21  发布在  Kubernetes
关注(0)|答案(1)|浏览(112)

我是一个新手在处理边车容器。我正在尝试做的是,我的主容器正在生成日志,我想我的边车读取日志。但当我尝试使用下面的命令读取日志

kubectl logs -f currency-exchange -c sidecar-container

上面写着

cat: can't open '/var/log/in28min/mmv2-currency-exchange-service:0.0.11- 
SNAPSHOT/access.log': No such file or directory

基本上,它不会在目录中创建access.log文件,而是创建整个文件夹结构。
下面是我的yaml文件。

apiVersion: v1
kind: Pod
metadata:
  name: currency-exchange
  labels:
    app: currency-exchange
spec:
  containers:
    - name: main-application
      image: in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT
      volumeMounts:
        - name: shared-logs
          mountPath: /var/log/in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT
    - name: sidecar-container
      image: busybox
      command: ["sh","-c","while true; do cat /var/log/in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT/access.log; sleep 30; done"]
      volumeMounts:
        - name: shared-logs
          mountPath: /var/log/in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT
  volumes:
    - name: shared-logs
      emptyDir: {}

---

# Service Configuration
# --------------------
apiVersion: v1
kind: Service
metadata:
  labels:
    app: currency-exchange
  name: currency-exchange

spec:
  ports:
  - port: 8000
    protocol: TCP
    targetPort: 8000
  selector:
    app: currency-exchange
  type: LoadBalancer

在我的主容器中,它是一个SpringBoot Microserservice,我在那里写我的日志。

package com.in28minutes.microservices.currencyexchangeservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CurrencyExchangeController {
    
    private Logger logger = LoggerFactory.getLogger(CurrencyExchangeController.class);
    
    @Autowired
    private CurrencyExchangeRepository repository;
    
    @Autowired
    private Environment environment;
    
    @GetMapping("/currency-exchange/from/{from}/to/{to}")
    public CurrencyExchange retrieveExchangeValue(
            @PathVariable String from,
            @PathVariable String to) {
        
        logger.info("retrieveExchangeValue called with {} to {}", from, to);
        
        CurrencyExchange currencyExchange 
                    = repository.findByFromAndTo(from, to);
        
        if(currencyExchange ==null) {
            throw new RuntimeException
                ("Unable to Find data for " + from + " to " + to);
        }
        
        String port = environment.getProperty("local.server.port");
        
        //CHANGE-KUBERNETES
        String host = environment.getProperty("HOSTNAME");
        String version = "v11";
        
        currencyExchange.setEnvironment(port + " " + version + " " + host);
        
        return currencyExchange;
        
    }

}

当我这样做

kubectl logs -f currency-exchange

以下附件是我的log4j.properties文件

log4j.rootLogger=DEBUG, STDOUT, file

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT/access.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n

我可以看到我的日志。但我希望我的自定义日志在一个文件访问。日志,这样我就可以利用它,并做适当的行动。帮助是赞赏。谢谢。

bkhjykvo

bkhjykvo1#

你没有从你的主容器在access.log文件中写入任何日志,这就是为什么你的sidecar不能读取任何日志,因为文件/var/log/in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT/access.log不存在。所以,sidecar的cat命令返回错误。你应该从你的主容器将日志写入文件'/var/log/in28min/mmv2-currency-exchange-service:0.0.11- SNAPSHOT/access.log'

相关问题