无法连接到服务器:dial tcp: lookup <Server Location>: no such host

Unable to connect to the server: dial tcp: lookup <Server Location>: no such host

我开始为我们的应用程序构建一个 kubernetes 集群。我们正在使用 Azure 来提供云服务,所以我的 K8s 集群是使用 AKS 构建的。 AKs 集群是使用 Azure 的门户界面创建的。它有一个节点,我正在尝试创建一个带有单个容器的 pod 以部署到该节点。我目前遇到的困难是尝试从 Powershell 连接到 AKS 集群。 我采取的步骤是:

az login (followed by logging in)
az account set --subscription <subscription id>
az aks get-credentials --name <cluster name> --resource-group <resource group name>
kubectl get nodes

输入最后一行后,出现错误:无法连接到服务器:拨号 tcp: 查找:没有这样的主机

我还在 SO 和其他论坛上找到了一些其他的兔子洞,但老实说,我正在寻找一种直接的方法来访问我的集群,然后再进一步复杂化它。

编辑:所以最后,我删除了我正在使用的资源并启动了新版本的 AKS,现在连接没有问题。谢谢你的建议!

通常,这就是连接所需的全部内容。检查防火墙是否未阻止任何流量。此外,再次验证订阅 ID 和其他标识符并确保您使用的是正确的值。如果问题仍然存在,我建议您寻求 Azure 支持来帮助您。

将此作为社区 Wiki 发布以提高知名度。

OP提供的解决方案:

删除资源并启动新版本的 AKS。

详情可以查看文档Create a resource group, Create AKS cluster and resource create.

下次值得一试:

kubectl config use-context <cluster-name>

因为它是在类似 Github issue 中提出的。

对于 Azure 门户的任何管理操作,使用桌面 Powershell 中的 Az 模块更加方便。 Microsoft 添加了许多新的 cmdlet 来管理 AKS 和 Service Fabric 集群。

请看一下Az.Aks

你的情况:

Connect-AzAccount

Get-AzAksNodePool

无法连接到服务器:拨号 tcp:查找:没有这样的主机

错误是因为私有集群。创建 AKS 群集时启用了专用群集选项。您需要禁用此选项。

Kubectl 是一个 kubernetes 控制客户端。它是连接我们的 kubernetes 集群的外部连接提供商。我们无法从外部连接到私有集群。

相信我....只需禁用专用集群选项即可看到您的成功。谢谢。

注意:我们无法在集群创建后禁用此选项。您需要删除集群并重新对其进行改造。

当 运行 来自 jenkins 的 kubectl 命令时,我遇到了同样的问题。对我来说,这是 ~/.kube/config 的权限问题,我也给了它访问 jenkins 的权限,这为我解决了这个问题。

Gaurav 的回答非常概括。事实上,您可以参考 documentation,其中指出

The API server endpoint has no public IP address. To manage the API server, you'll need to use a VM that has access to the AKS cluster's Azure Virtual Network (VNet). There are several options for establishing network connectivity to the private cluster.

连接私有集群,只有3种方法:

  • Create a VM in the same Azure Virtual Network (VNet) as the AKS cluster.
  • Use a VM in a separate network and set up Virtual network peering. See the section below for more information on this option.
  • Use an Express Route or VPN connection.

我也遇到了这个问题,我使用的是私有集群,我在不同的 vnet 中有一台机器(堡垒)启用了对等互连,但我仍然无法连接集群(我能够SSH 和 telnet 到机器)。

然后我在堡垒主机所在的 vnet 的专用 DNS 区域中添加了一个虚拟网络 link。它对我有用,我可以访问集群。

截至目前,aks 运行 命令添加了之前发布的 fourth option to connect to private clusters extending @Darius's

  1. Use the AKS Run Command feature.
az aks command invoke -g <resourceGroup> -n <clusterName> -c "kubectl get pods -n kube-system"
az aks command invoke -g <resourceGroup> -n <clusterName> -c "kubectl get nodes"

如果您收到 (ResourceGroupNotFound) 错误,请尝试添加订阅

az aks command invoke -g <resourceGroup> -n <clusterName> --subscription <subscription> -c "kubectl get nodes"

您还可以配置默认订阅:

az account set -s <subscription>

使用私有集群时,kubernetes api-endpoint 只能在集群的 VNet 上访问。不幸的是,通过 VPN 进行连接并不容易,因为 Azure 私有 DNS 将无法用于 VPN 客户端(目前)。

但是,可以将 kubectl 直接连接到 api-端点的 IP 地址,但这需要您忽略证书错误,因为我们直接使用 IP。

如果您编辑 .kube/config 并将服务器地址更改为 IP 号。然后用这样的东西调用 kubectl

kubectl get all --all-namespaces --insecure-skip-tls-verify

对我来说,当我尝试将新 Linux 用户连接到我在 AWS 中的 Elastic Kubernetes 集群时遇到了这个问题。

我设置了一个名为 jenkins-user 的新用户,然后我尝试 运行 下面的命令来获取 pods:

kubectl get pods

然后我会运行进入下面的错误:

Unable to connect to the server: dial tcp: lookup 23343445ADFEHGROGMFDFMG.sk1.eu-east-2.eks.amazonaws.com on 198.74.83.506:53: no such host

我是这样解决的:

问题是因为我没有在新 linux 用户 (jenkins-user) 的 kube 配置文件中设置 Kubernetes 集群的上下文。

我所要做的就是首先为这个新用户安装 aws-cli(将它安装到这个新用户的主目录中)。然后 运行 命令 aws configure 配置必要的凭据。虽然,由于我已经为 Linux 系统上的其他用户设置了 aws-cli,我只是使用以下命令将 ~/.aws 目录从现有用户复制到 jenkins-user 主目录:

sudo cp -R /home/existing-user/.aws/ /home/jenkins-user/

接下来,我必须为 Kubernetes 配置创建一个上下文,它将使用以下命令为 jenkins-user 创建一个新的 ~/.kube/config 文件:

aws eks --region my-aws-region update-kubeconfig --name my-cluster-name

接下来,我检查了 kube 配置文件以确认我的上下文已使用以下命令添加:

sudo nano /.kube/config

这次我运行下面的命令成功了:

kubectl get pods

资源:Create a kubeconfig for Amazon EKS

您可以使用 az aks command invoke 在专用 AKS 群集上 运行 kubectl 命令。有关详细信息,请参阅 this

至于为什么您可能想要 运行 专用 AKS 群集,请阅读 this

您只需将“--admin”附加到查询,如下所示。

az aks get-credentials --name <cluster name> --resource-group <resource group name> --admin

我在重启 kubernetes 集群后也遇到了这个问题,但事实证明我只是等待的时间不够长,大约 10 分钟后,“kubectrl”命令又开始工作了。

如果您将 AWS 与 kops 一起使用,那么这可能会对您有所帮助

mkdir autoscaler
cd autoscaler/
git clone https://github.com/kubernetes/autoscaler.git

创建一个名为 ig-policy.json 的文件,内容为

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "autoscaling:DescribeAutoScalingGroups",
                "autoscaling:DescribeAutoScalingInstances",
                "autoscaling:DescribeLaunchConfigurations",
                "autoscaling:SetDesiredCapacity",
                "autoscaling:TerminateInstanceInAutoScalingGroup"
            ],
            "Resource": "*"
        }
    ]
}

然后你需要创建iam策略

aws iam create-policy --policy-name ig-policy --policy-document file://ig-policy.json

并将上面带有用户 id 的创建 iam 策略附加到集群名称

aws iam attach-role-policy --policy-arn arn:aws:iam::005935423478650:policy/ig-policy --role-name nodes.testing.k8s.local

然后更新集群

kops update cluster testing.k8s.local --yes

然后运行

kops rolling-update cluster

创造私人旅程并不容易,但它有美丽的景色,所以我鼓励任何人去那里。 我在 terraform 中完成了这一切,所以有些名称可能与 portal/azure CLI 中的名称略有不同。

我就是这样做的:

  1. 私有 DNS 区域,名称为 privatelink.westeurope.azmk8s.io
  2. 将放置 AKS 的 VNET(我们称之为 vnet-access
  3. 要从中访问 AKS 的虚拟网络
  4. 专用 AKS(private_dns_zone_id 设置为 dns 区域形式的第一个点)
  5. 虚拟网络link(在专用 DNS 区域中,从点 3 指向 VNET)
  6. 点 2 和点 3 之间的网络对等互连。

这应该允许 vnet-access 中的任何机器首先解析 DNS,然后 - 访问集群...

但是...如果您想从本地计算机到达那里,这是另一种设置。还好微软有这样的教程here


如果您发现仍有问题 - 请在评论中提出错误,我会尝试调整我的答案来解决这个问题。