如何将基于容器的应用程序的日志存储在像天青文件这样的卷中?

时间:2018-08-16 10:14:05

标签: azure .net-core azure-files azure-kubernetes

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

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

  

kubectl exec -it apiapplication-121213-121212-bash

     

df -h

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

3 个答案:

答案 0 :(得分:1)

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

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 :(得分: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 中调用上述方法,这就是为什么每当我启动该应用程序时,都会立即使用“ 登录,并在2018年8月30日星期四下午18.43 PM.txt”创建一个新文件。 ”位于我的名为“ WebApplication”的子文件夹下,该子文件夹已在/mnt/azure位置的Kubernetes中的容器上创建。

一旦在/mnt/azure位置的kuberneres的Pod中创建了文件,那么我就必须在需要的地方写一些虚拟日志,例如:

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

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

答案 2 :(得分:-1)

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

相关问题