Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将基于容器的应用程序的日志像azure文件一样存储在卷中?_Azure_.net Core_Azure Files_Azure Aks - Fatal编程技术网

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

如何将基于容器的应用程序的日志像azure文件一样存储在卷中?,azure,.net-core,azure-files,azure-aks,Azure,.net Core,Azure Files,Azure Aks,我致力于使用.net核心编程语言开发基于容器的应用程序,并在azure kubernetes服务中部署了这些应用程序。还通过以下方式实现了使用azure文件创建持久卷的功能 到目前为止,一切正常,通过在命令提示符下运行以下命令,我可以看到文件共享装载在相应的pod中 kubectl exec-it apiapplication-121213-121212——bash df-h 但我想在挂载的文件共享中创建一个新文件,其中包含pod名称和当前日期时间,例如(apiapplication-12121

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

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

kubectl exec-it apiapplication-121213-121212——bash

df-h


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

您可以手动编写一个脚本,获取所需信息: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);
}

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


3-我不确定您想要路由文件夹的日志是什么,但这应该是直截了当的。

我不明白您想要实现什么,但您总是可以在单个主机的
/var/log/containers/
上找到容器标准内容。要访问Azure存储帐户,您可以使用
az
。如果您想在给定的时间执行命令,您可以在大多数Linux发行版中始终使用cron。

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

 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”的子文件夹下创建一个新文件,其中包含“2018年8月30日星期四下午18时43分的日志”它已经在Kubernetes的一个pod上创建,位于
/mnt/azure
位置

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

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

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

谢谢Adam,但我想使用.NET核心语言动态地在Azure文件共享中创建具有PodName和DateTime的新文件(即装入kubernetes Pods作为驱动器)。我不清楚您的问题。如果您无法在Azure文件共享中创建该文件,或者不知道如何使用.NET Core?@CharlesXu MSFT创建该文件,我的问题是我想在Azure文件共享中创建具有kubernetes pod名称的新文件,该文件已安装在kubernetes pod服务器中,并具有类似于(Z://)的驱动器。并且还希望将一些带有当前日期时间的日志发送到位于kubernetes吊舱中装载的azure文件共享中的新创建的文件中。