0

My Kubernetes cluster is on AWS. I have setup the context using commands to connect to the Kubernetes cluster but still I am getting the following error:

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

Please note that kubectl and aws-iam-authenticator are installed and the config file is located in the home directory of the user inside .kube folder.

I have verified by running the command that aws sts get-caller-identity that I am connected to the AWS via cli.

Following are the configuration steps that I have taken:

export CLUSTER=abcd.com
export API_ENDPOINT=abcd.com
export TEAM_NAMESPACE=abcd

kubectl config set-cluster $CLUSTER --server=$API_ENDPOINT

kubectl config set-credentials $CLUSTER \
  --exec-api-version="client.authentication.k8s.io/v1beta1" \
  --exec-command="aws-iam-authenticator" \
  --exec-arg="token" \
  --exec-arg="-i" \
  --exec-arg="$CLUSTER"

kubectl config set-context $CLUSTER --cluster=$CLUSTER --namespace=$TEAM_NAMESPACE --user=$CLUSTER
kubectl config use-context $CLUSTER

Please share suggestions to solve this issue.

Fahad
  • 5
  • 3
  • It looks like `kubectl` is attempting to connect to `localhost` rather than to `$API_ENDPOINT`. If you look at `kubectl config view --minify`, do the `context` and `cluster` have the expected values? – larsks Oct 23 '22 at 14:04
  • Kubectl config is normally stored in `.kube`, not `.ssh`, but that can be changed using `$KUBECONFIG`. Can you check where your config file is expected to be? Plus, do what @larsks said. – Software Engineer Oct 23 '22 at 15:00
  • @larsks the output of the command "kubectl config view --minify" is showing the config file output that is saved in .kube folder of the home directory. I also used the command " kubectl get pods --kubeconfig ~/.kube/config" to force the selection of the config file but no sucess. – Fahad Oct 23 '22 at 19:18
  • @SoftwareEngineer sorry for the typo, the config file is saved in the .kube folder of the home directory – Fahad Oct 23 '22 at 19:18
  • 2
    When I had a look at the config file, the server portion was empty "". So I manually edited the config file and added in value of the API Server there. The issue got resolved by Gods will. Thank you for the support. – Fahad Oct 23 '22 at 19:26

1 Answers1

0

I had a look at the config file by using the command "kubectl config view --minify" and found that somehow the server field was empty. I edited the file using vi editor and set the API server value manually there. It resolved the issue.

Fahad
  • 5
  • 3