You need to enable JavaScript to run this app. C# Kubernetes客户端连接AKS集群出现SSL连接失败问题排查
优惠活动
大模型
产品
解决方案
定价
更多

C# Kubernetes客户端连接AKS集群出现SSL连接失败问题排查

代码实现逻辑
var clientId = ".........";
var clientSecret = ".........";
var tenantId = "xxxxxxxxxxxx.onmicrosoft.com";
var subscriptionId = "...........";
var clusterName = "aks-k8s";
var resourceGroupName = "test";

var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId,
                    clientSecret,
                    tenantId,
                    AzureEnvironment.AzureGlobalCloud);

var azure = Azure.Configure()
                 .Authenticate(credentials)
                 .WithSubscription(subscriptionId);

IKubernetesCluster kubernetesCluster = azure.KubernetesClusters
    .GetByResourceGroup(resourceGroupName, clusterName);

if (kubernetesCluster != null)
{
    var buffer = kubernetesCluster.UserKubeConfigContent;
    var configStream = new MemoryStream(buffer);

    var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(configStream, null, null);
    IKubernetes client = new Kubernetes(config);

    var nodes = await client.ListNodeAsync();

    foreach (var node in nodes.Items)
    {
        //update my database with this list.....
    }
}
else
{
    Console.WriteLine(clusterName + " cluster does not exits");
}
部署后报错信息
The SSL connection could not be established, see inner exception.

at System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface secModule, String package, CredentialUse intent, SCHANNEL_CRED scc)   
at System.Net.Security.SslStreamPal.AcquireCredentialsHandle(CredentialUse credUsage, SCHANNEL_CRED secureCredential)   
at System.Net.Security.SslStreamPal.AcquireCredentialsHandle(X509Certificate certificate, SslProtocols protocols, EncryptionPolicy policy, Boolean isServer)   
at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint)   
at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output)   
at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count)   
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest)   
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)   
at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---   
at System.Net.Security.SslState.ThrowIfExceptional()   
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)   
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)   
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)   
at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__47_1(IAsyncResult iar)   
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---   
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
问题根因
  • 本地运行环境的系统证书存储内置了可信任的根CA,能正常完成AKS API Server的TLS证书校验
  • Azure Web App/Function的托管沙箱采用隔离的证书存储,不默认包含AKS API Server证书链对应的根/中间CA,导致TLS握手阶段凭证校验失败
  • 托管环境默认仅启用TLS 1.2及以上版本协议,若客户端协商的TLS版本不兼容,会直接触发握手失败
  • 若未配置VNet集成,应用的出站流量经过Azure边缘代理时可能存在证书重签逻辑,导致和AKS API Server的证书不匹配
修复操作步骤
  • 构建Kubernetes客户端配置时,不依赖系统证书存储的自动加载,将kubeconfig内置的CA证书手动绑定到客户端配置:
    var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(configStream, null, null);
    // 强制加载kubeconfig中内置的CA证书,跳过系统证书存储查询
    config.SkipTlsVerify = false;
    // 显式指定客户端支持的TLS版本,避免协议协商失败
    config.TlsVersion = System.Net.Security.SslProtocols.Tls12 | System.Net.Security.SslProtocols.Tls13;
    
  • Windows托管的Web App/Function需将AKS对应的CA证书安装到应用隔离的受信任根证书存储,不要依赖全局系统证书
  • 若AKS为私有集群,需为Web App/Function配置同VNet的区域集成,确保能直连AKS私有API Server端点
  • 检查应用的出站网络配置,确认无强制出站代理、应用级网关对TLS链路的截断重签

内容的提问来源于stack exchange,提问作者dinesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.31 12:36:01