Django kubernetes connection Max retries exceeded with url

Viewed 94

I'm trying to create a kubernetes job inside google cloud using django and when I call my rest_api view I'm getting this error:

ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 
0x7fc745e58580>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /apis/batch/v1/ 
(Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc745e58580>: Failed to establish a new connection: [Errno 111] Connection refused'))

Here is my kubec-config.yaml:

apiVersion: v1
kind: Config
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: ---
  name: gke_---_europe-west1_cluster-1
users:
- name: cluster-1
current-context: gke_---_europe-west1_cluster-1
contexts:
- context:
    cluster: gke_---_europe-west1_cluster-1
    user: gke_---_europe-west1_cluster-1
  name: gke_---_europe-west1_cluster-1

spec:
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cluster-1
            image: cluster-1:v5
            env:
            imagePullPolicy: IfNotPresent
            command: ['./Renderer']
            args: ['project.json']
          restartPolicy: OnFailure

I got many of the information here by running kubectl config view inside google cloud and just copy and pasted them here.

And this is my APIview:

class ProjectExportView(APIView):
    def get(self, request, uuid):
        jobs = ["", "", ""]
        try:
            project = Project.objects.get(uuid=uuid)
            from utils.gke import kube_test_credentials, kube_cleanup_finished_jobs, kube_delete_empty_pods, kube_create_job
            import os

            # Testing Credentials
            kube_test_credentials()
            # We try to cleanup dead jobs (READ THE FUNCTION CODE!)
            kube_cleanup_finished_jobs()
            kube_delete_empty_pods()
            # Create a couple of jobs
            for i in range(3):
                jobs[i] = kube_create_job(project.manifest)
            # This was to test the use of ENV variables.
            logging.info("Finshed! - ENV: {}".format(os.environ["VAR"]))

        except:
            logging.info("ProjectExportView failed for thumbnail for the project uuid : {uuid}")
            exception_message = traceback.format_exc()
            logging.info(f"Exception message : {exception_message}")

        return Response(
                    {
                        "uuid"      : uuid,
                        "Job1"      : jobs[0],
                        "Job2"      : jobs[1],
                        "Job3"      : jobs[2],
                    }
                )

I probably get the error at kube_test_credentials() which looks like this:

...
with open("utils/kube-config.yaml", "r", encoding="utf-8") as file:
    config.load_kube_config(file)
# Setup K8 configs
configuration = kubernetes.client.Configuration()
api_instance = kubernetes.client.BatchV1Api(kubernetes.client.ApiClient(configuration))
...
def kube_test_credentials():
    try: 
        api_response = api_instance.get_api_resources()
        logging.info(api_response)
    except ApiException as e:
        print("Exception when calling API: %s\n" % e)

Which is there to check for connectivity issues. What could be the issue here? Am i supposed to put info about the service account insice my kubec-config.yaml file? If so, how do i do that?

Edit: I exposed the port on dockerfile. Still the same error.

0 Answers
Related