Quantcast
Viewing all articles
Browse latest Browse all 102

Kubernetes: kubectl exec command different behaviour from C# program

I have a Kubernetes cluster with a deployment workload resource and a storage mounted (persistent volume claim).The deployment resource runs a container with a Minecraft server, containing file server.properties.

Wanted Behaviour

I want to change a value from that file, from a C# application, using kubectl exec and sed command:

kubectl exec deployment/test-mc -- sed -i 's|^gamemode=.*|gamemode=creative|' server.properties

If I launch this command from a terminal (not from the application) it works just fine, and the file gets updated with the new value. To check that I used kubectl exec deployment/test-mc -- cat server.properties.

Problem

When I try launching the same command from the C# application, it doesn't work. That's the code I use to run the command:

private string ExecCommand(string serverName, string command){    var processStartInfo = new ProcessStartInfo()    {        FileName = "kubectl",        Arguments = $"exec deployment/{serverName} -- {command}",        RedirectStandardOutput = true,        UseShellExecute = false    };    var process = new Process()    {        StartInfo = processStartInfo    };    process.Start();    var commandOutput = process.StandardOutput.ReadToEnd();    process.WaitForExit();    return commandOutput;}public void ChangeGamemode(string serverName, string newGamemode){    Console.WriteLine($"Command: sed -i 's|^gamemode=.*|gamemode={newGamemode}|' server.properties");    var commandOutput = ExecCommand(serverName, $"sed -i 's|^gamemode=.*|gamemode={newGamemode}|' server.properties");    Console.WriteLine($"Output: {commandOutput}");}

But when I check the content of server.properties using kubectl exec deployment/test-mc -- cat server.properties, the value is still unchanged.

Further Information

I know for sure that I can access the deployment resource, since I tried creating a file from the C# application and it succeeded:

// From C# ApplicationExecCommand(serverName, "touch test.txt");

File list from terminal:

> kubectl exec deployment/test-mc -- ls -latotal 46748[...]-rw-rw-rw-  1 minecraft minecraft     1313 Jul  3 10:46 server.properties-rw-r--r--  1 root      root             0 Jul  3 11:23 test.txt[...]

Any help will be kindly appreciated :)


Viewing all articles
Browse latest Browse all 102

Trending Articles