We are using the native Kubernetes LeaderElection, but it appears to only work within the cluster. Is there a way to make this work across multiple clusters?
Official Client: https://github.com/kubernetes-client/csharp/tree/master/src/KubernetesClient/LeaderElection
/* EXAMPLE CODE */
public Task StartAsync(CancellationToken cancellationToken)
{
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var kubeCfg = KubernetesClientConfiguration.BuildDefaultConfig();
var client = new Kubernetes(kubeCfg);
var leaderLock = new LeaseLock(client, kubeCfg.Namespace, "leader-lock", Environment.GetEnvironmentVariable("HOSTNAME"));
var config = new LeaderElectionConfig(leaderLock)
{
LeaseDuration = TimeSpan.FromSeconds(15),
RenewDeadline = TimeSpan.FromSeconds(10),
RetryPeriod = TimeSpan.FromSeconds(2)
};
_leaderElector = new LeaderElector(config);
_leaderElector.OnStartedLeading += () => _logger.LogInformation("Got Leader Lock");
_leaderElector.OnNewLeader += leader => _logger.LogInformation("Leader changed to: {leader}", leader);
_leaderElector.OnStoppedLeading += () => _logger.LogInformation("Lost Leader Lock");
_leaderElectorTask = Task.Run(async () =>
{
while (!_cts.IsCancellationRequested)
{
try
{
await _leaderElector.RunAsync(_cts.Token);
}
catch (TaskCanceledException)
{
}
catch (Exception ex)
{
await Task.Delay(TimeSpan.FromSeconds(10), _cts.Token);
}
}
});
return Task.CompletedTask;
}