如何将基于容器的应用程序的日志存储在其容量中,如azure文件?

问题描述 投票:-1回答:3

我使用.net核心编程语言开发基于容器的应用程序,并将其部署在azure kubernetes服务中。还通过遵循此documentation实现了使用azure文件创建持久卷的功能。

到目前为止一切正常,我可以通过在命令提示符中运行以下命令来查看安装在相应pod中的文件共享。

kubectl exec -it apiapplication-121213-121212 - bash

df -h

但我想在已安装的文件共享中创建具有pod名称和当前日期时间的新文件(例如apiapplication-121213-121212_16-08-2018)。之后,我想将容器应用程序的日志存储在已安装文件共享中新创建的文件中。

azure .net-core azure-files azure-kubernetes
3个回答
1
投票

您可以手动编写一个脚本来获取所需的信息:Pod名称和当前日期和时间。

1 - 通过获取这些参数并将它们存储在变量中来启动

2-创建文件夹

3-将日志存储并映射到这些文件夹。

1 - 您可以使用以下命令获取pod名称:

kubectl get pods

将名称存储在变量中。

2 - 使用类似下面的代码:

public void CreateDirectory(string prefix)
{
    string dirName = prefix + " on " + DateTime.Now.ToString("ddd MM.dd.yyyy 'At' HH:mm tt");

    //Improved version of the above:
    string dirName = string.Format("{0} on {1:ddd MM.dd.yyy 'At' HH:mm tt}", prefix, DateTime.Now);

    Directory.CreateDirectory(dirName);
}

Source of the code

您所要做的就是将变量名从1插入到2中的代码中。

3-我不确定您要将文件夹路由到哪些日志,但这应该是直截了当的。


1
投票

最后,我通过在当前项目中编写以下代码行解决了我的问题。

 public class StoreLogsintoFileShare
{
    public static string pathString = string.Empty;
    public static void CreateSubFolderAndFile()
    {
        // Specify a name for your top-level folder.
        string currentFolderPath = "/mnt/azure";

        string subFolderName = "WebApplication";
        string date = DateTime.Now.ToString("ddd MM.dd.yyyy");
        string time = DateTime.Now.ToString("HH.mm tt");
        string format = "{0} on {1} At {2}.txt";
        string fileName = string.Format(format, "Logs", date, time);

        // To create a string that specifies the path to a subFolderName under your 
        // top-level folder, add a name for the subFolderName to folderName.
        pathString = System.IO.Path.Combine(currentFolderPath, subFolderName);

        //Create the subfolder. 
        System.IO.Directory.CreateDirectory(pathString);

        // Use Combine again to add the file name to the path.
        pathString = System.IO.Path.Combine(pathString, fileName);

        // Verify the path that you have constructed.
        Debug.WriteLine("Path to my file: {0}\n", pathString);

        // Check that the file doesn't already exist. If it doesn't exist, create
        // the file and write logs in to it.
        // DANGER: System.IO.File.Create will overwrite the file if it already exists.
        if (!System.IO.File.Exists(pathString))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(pathString))
            {

            }
        }
        else
        {
            Debug.WriteLine("File \"{0}\" already exists.", fileName);
            return;
        }
    }
}

我在Startup.cs中调用上面的方法,这就是为什么每当我启动应用程序然后立即创建一个新文件时,在我的子文件夹下创建了一个名为“WebApplication”的子文件夹“Thus 08.30.2018 18.43 PM.txt”在/mnt/azure位置的Kubernetes的一个吊舱上。

一旦文件在kazneres位于/mnt/azure位置的pod上创建,那么我必须在我需要的地方写一些虚拟日志,例如:

await System.IO.File.AppendAllLinesAsync(StoreLogsintoFileShare.pathString, new[] {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),ex.ToString()});

注意:但在上面的代码中我没有动态获取pod名称。我只是使用静态名称作为“WebApplication”来创建子文件夹。


-1
投票

我无法理解你想要实现的目标,但你总能在个人主机的/var/log/containers/上找到容器标准内容。要访问Azure存储帐户,您可以使用az cli utility。如果要在给定时间执行命令,可以在大多数Linux发行版中始终使用cron。

© www.soinside.com 2019 - 2024. All rights reserved.