<p>Here is the scenario -</p>
<p>I have deployed kubernetes cluster and inside that I have created an application( say test) with &quot;kubectl:1.20&quot; image. I have created required clusterrole, rolebindings and sa for this test application to have the authorization for managing deployments.
Now my requirement is that i should be able to fetch and patch the deployments which are running in the kubernetes cluster from inside the test pod as part of a cronjob.</p>
<p>When i am running the cronjob, i am getting the below mentioned error in the test pod
&quot;The connection to the server x.x.x.x:6443 was refused - did you specify the right host or port?&quot;</p>
<p>I have passed the kubeconfig file of the cluster as configmap to the test pod.</p>
<p>below is the cronjob yml file -</p>
<pre><code>kind: CronJob
apiVersion: batch/v1beta1
metadata:
  name: test
spec:
  schedule: &quot;*/2 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: local-kubectl-scale-deploy
          containers:
          - name: mycron-container
            image: bitnami/kubectl:1.20
            imagePullPolicy: IfNotPresent
            env:
              - name: KUBECONFIG
                value: &quot;/tmp/kubeconfig&quot;
            command: [ &quot;/bin/sh&quot; ]
            args: [ &quot;/var/httpd-init/script&quot;, &quot;namespace&quot; ]
            tty: true
            volumeMounts:
            - name: script
              mountPath: &quot;/var/httpd-init/&quot;
            - name: kubeconfig
              mountPath: &quot;/tmp/&quot;
          volumes:
          - name: script
            configMap:
              name: cronscript
              defaultMode: 0777
          - name: kubeconfig
            configMap:
              name: kubeconfig
          restartPolicy: OnFailure
          terminationGracePeriodSeconds: 0

  concurrencyPolicy: Replace
</code></pre>
<p>below is the kubeconfig file which i am passing as configmap inside the pod.</p>
<pre><code>kind: ConfigMap
apiVersion: v1
metadata:
  name: kubeconfig
  labels:
    app: kubeconfig
data:
  kubeconfig: |
    apiVersion: v1
    kind: Config
    clusters:
    - name: default-cluster
      cluster:
        certificate-authority-data: xxxxxxxxxxx
        server: https://x.x.x.x:6443
    contexts:
    - name: default-context
      context:
        cluster: default-cluster
        namespace: default
        user: default-user
    current-context: default-context
    users:
    - name: default-user
      user:
        token: xxxxxx        
</code></pre>
<p>Am I missing some configuration from the pod to communicate to this x.x.x.x:6443 IP?</p>

bunny
<p>To diagnose the communication error to the address we would need a lot more detail from your network configuration, but instead we can fix the cronjob.</p>
<p>The proper way of achieving that is to set a service account with prober RBAC similar to <a href="https://stackoverflow.com/a/58378834/3930971">https://stackoverflow.com/a/58378834/3930971</a></p>
