C#应用程序无法访问kubernetes服务?

时间:2020-02-10 20:05:19

标签: c# asp.net-core kubernetes

我的C#应用​​程序似乎有问题,我将其容器化并放入pod中,似乎无法通过集群访问服务?

我做了一个简单的代码示例

using System;
using System.Net.NetworkInformation;

namespace pingMe
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Ping ping = new Ping();
            PingReply pingresult = ping.Send("example-service.default.svc");
            if (pingresult.Status.ToString() == "Success")
            {
                Console.WriteLine("I can reach");
            }
        }
    }
}

应该能够在集群中ping通

PS C:\Helm> kubectl apply -f https://k8s.io/examples/service/access/hello-application.yaml
deployment.apps/hello-world created
PS C:\Helm> kubectl expose deployment hello-world --type=ClusterIP --name=example-service
service/example-service exposed
PS C:\Helm> kubectl get service/example-service
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
example-service   ClusterIP   10.97.171.216   <none>        8080/TCP   14s
PS C:\Helm> kubectl get deployment.apps/hello-world
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
hello-world   2/2     2            2           61s

但是在集群中我得到了:

Hello World!
Unhandled exception. System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known
   at System.Net.Dns.InternalGetHostByName(String hostName)
   at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
   at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   --- End of inner exception stack trace ---
   at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress)
   at pingMe.Program.Main(String[] args) in /src/pingMe/Program.cs:line 12

为什么...添加端口号无济于事。

1 个答案:

答案 0 :(得分:1)

表面上一切正常。我将通过消除可能性开始调试。您可以从“工作人员”窗格中ping ClusterIP而不是服务名称吗?您可以在任何服务或Pod上成功进行DNS查找吗?如果您可以访问群集IP,但是ping或解析服务名称仍然失败,则DNS似乎更可疑。