Verified Solution

[StackOverflow/kubernetes] kubectl : connection refused

Sponsored Content
### ROOT CAUSE The "kubectl connection refused" error typically occurs due to one of the following reasons: 1. The Kubernetes API server is not running or is not accessible (e.g., due to network issues, incorrect configuration, or the node not being properly joined to the cluster). 2. Incorrect configuration in the `kubeconfig` file (e.g., wrong server URL, invalid credentials, or misconfigured context). 3. Firewall rules blocking access to the API server port (typically 6443). 4. The node is not properly registered with the cluster (e.g., a worker node failed to join). ### CODE FIX 1. **Check Cluster Status**: ```bash kubectl get nodes ``` Ensure all nodes are in the "Ready" state. If not, investigate node issues. 2. **Verify API Server**: ```bash sudo systemctl status kubelet ``` Check if the kubelet service is running. If not, restart it: ```bash sudo systemctl restart kubelet ``` 3. **Inspect kubeconfig**: ```bash kubectl config view ``` Look for the correct `server` URL, `cluster` name, and `user` credentials. Ensure the context is properly set: ```bash kubectl config current-context ``` 4. **Test Network Connectivity**: From the node running `kubectl`, test connectivity to the API server: ```bash curl https://:6443 ``` Replace `` with the actual IP of your Kubernetes API server. If blocked, check firewall rules on both the node and the master. 5. **Rejoin Nodes (if necessary)**: If a node is not ready, rejoin it using the bootstrap token or certificate: ```bash kubeadm join :6443 --token --discovery-token-ca-cert-hash sha256: ``` 6. **Review Logs**: Check the API server logs for errors: ```bash journalctl -u kubelet -f ``` If the issue persists, validate the Kubernetes version compatibility between `kubectl` and the cluster, and ensure TLS certificates are properly configured.
Deploy on DigitalOcean ($200 Credit)

Related Fixes

[StackOverflow/go] Accessing the individual words of the content a file
[StackOverflow/kubernetes] Reconciliation from DB -> Kubernetes or DB as a caching layer
[microsoft/vscode] Copilot chat OTel child spans are missing tool definitions