"The connection to the server localhost:8080 was refused - did you specify the right host or port?"

"The connection to the server localhost:8080 was refused - did you specify the right host or port?"

我正在尝试创建我的集群的 ec2 实例。我已经安装了 kubectl,这是我的服务和工作负载 yaml 文件

services.yaml

apiVersion: v1
kind: Service
metadata:
  name: stockapi-webapp

spec:
  selector:
    app: stockapi

  ports:
    - name: http
      port: 80

  type: LoadBalancer

workloads.yaml

apiVersion: v1
kind: Deployment
metadata:
  name: stockapi
spec:
  selector:
    matchLabels:
      app: stockapi
  replicas: 1
  template: # template for the pods
    metadata:
      labels:
        app: stockapi
    spec:
      containers:
      - name: stock-api
        image: public.ecr.aws/u1c1h9j4/stock-api:latest

当我尝试 运行

kubectl apply -f workloads.yaml

我认为这是一个错误

The connection to the server localhost:8080 was refused - did you specify the right host or port?

我也尝试将 services.yaml 中的端口更改为 8080,但也没有解决问题

当您在客户端上没有 ~/.kube/config 文件或没有正确配置/您 运行 kubectl 命令时,会出现此错误。

kubectl 从 ~/.kube/config 文件读取 clusterinfo 和要连接的端口。

如果您使用的是 eks,请按以下步骤创建 config 文件 aws eks create kubeconfig file