How to store the logs of container based application in their volume like azure files?

8/16/2018

I worked on developing container based applications using .net core programming language and deployed those are in azure kubernetes service. Also implemented the functionality for creating the persistent volume with azure files by following this documentation.

Up to now everything working fine and I am able to see the file share mounted in respective pods by run following commands in Command Prompt.

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

df -h

But I want to create new file with pod name and current Date Time for example(apiapplication-121213-121212_16-08-2018) in mounted file share. After that I want to store logs of container applications in the newly created file in mounted file share.

-- Pradeep
.net-core
azure
azure-files
azure-kubernetes

3 Answers

8/31/2018

Finally I resolved my issue by writing the below lines of code in my current project.

 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;
        }
    }
}

I am invoking the above method in the Startup.cs that’s why whenever I launch the application then immediately a new file created with “Logs on Thu 08.30.2018 At 18.43 PM.txt” under my subfolder named as "WebApplication" which is already created on a pod in Kubernetes at /mnt/azure location.

Once the file created on a pod in kuberneres at /mnt/azure location, then I have to write some dummy logs wherever I need for example:

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

Note: But in the above code I am not getting the pod name dynamically. I just used static name as "WebApplication" for subfolder creation.

-- Pradeep
Source: StackOverflow

8/16/2018

You could manually write a script that gets your the information you need: Pod name, and current Date and Time.

1 -Start by getting these parameters and store them in variables

2- Create folders

3- Store and map logs to these folders.

1 - you could use the following command to get the pod names:

kubectl get pods

Store the name(s) in variables.

2 - use a code similar to the below:

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

All you have to do is plugin the variable name from 1 to the code in 2.

3- I'm not sure what logs you want to route the folders, but this should be straight forward.

-- Adam Smith
Source: StackOverflow

8/20/2018

I couldn't understand what you are trying to achieve, but you can always find container stdout content at /var/log/containers/ at the individual host. To reach your Azure Storage Account you can use az cli utility. If you want to execute commands at given time you can always use cron in most of the Linux distributions.

-- rfum
Source: StackOverflow