<p>When I'm trying to execute the following command</p>
<pre><code>[&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;cqlsh cassandra.my-namespace.svc.cluster.local -f /path/to/schema.cql&quot;]
</code></pre>
<p>from my Job, I am receiving the following error:</p>
<pre><code>Traceback (most recent call last):
  File &quot;/usr/bin/cqlsh.py&quot;, line 2443, in &lt;module&gt;
    main(*read_options(sys.argv[1:], os.environ))
  File &quot;/usr/bin/cqlsh.py&quot;, line 2421, in main
    encoding=options.encoding)
  File &quot;/usr/bin/cqlsh.py&quot;, line 485, in __init__
    load_balancing_policy=WhiteListRoundRobinPolicy([self.hostname]),
  File &quot;/usr/share/cassandra/lib/cassandra-driver-internal-only-3.11.0-bb96859b.zip/cassandra-driver-3.11.0-bb96859b/cassandra/policies.py&quot;, line 417, in __init__
socket.gaierror: [Errno -2] Name or service not known
</code></pre>
<p>My Job is defined as Helm Hook with <code>post-install</code> annotation. My Cassandra Pod is defined using StatefulSet.</p>
<pre><code>kind: StatefulSet
metadata:
  name: cassandra
spec:
  serviceName: cassandra
  replicas: 1
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      containers:
        - name: cassandra
          image: cassandra:3
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 7000
              name: intra-node
            - containerPort: 7001
              name: tls-intra-node
            - containerPort: 7199
              name: jmx
            - containerPort: 9042
              name: cql
          env:
            - name: CASSANDRA_SEEDS
              value: cassandra-0.cassandra.default.svc.cluster.local
            - name: MAX_HEAP_SIZE
              value: 256M
            - name: HEAP_NEWSIZE
              value: 100M
            - name: CASSANDRA_CLUSTER_NAME
              value: &quot;Cassandra&quot;
            - name: CASSANDRA_DC
              value: &quot;DC1&quot;
            - name: CASSANDRA_RACK
              value: &quot;Rack1&quot;
            - name: CASSANDRA_ENDPOINT_SNITCH
              value: GossipingPropertyFileSnitch
          volumeMounts:
            - name: cassandra-data
              mountPath: /var/lib/cassandra/data
  volumeClaimTemplates:
    - metadata:
        name: cassandra-data
        annotations:  # comment line if you want to use a StorageClass
          # or specify which StorageClass
          volume.beta.kubernetes.io/storage-class: &quot;&quot;   # comment line if you
          # want to use a StorageClass or specify which StorageClass
      spec:
        accessModes: [&quot;ReadWriteOnce&quot;]
        resources:
          requests:
            storage: 1Gi
</code></pre>
<p>And this is my Service:</p>
<pre><code>kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra
spec:
  clusterIP: None
  ports:
    - port: 9042
  selector
    app: cassandra
</code></pre>
<p>When I run the <code>cqlsh</code> command manually from the container, everything works. Unfortunately, the automated solution throws the mentioned error.</p>
<p>Am I missing something in the Service configuration? I have thought since I am connecting to service from the Pod created by Job, it should work.</p>
<p>EDIT:
Job looks like this:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: init-db
spec:
  template:
    metadata: 
      name: init-db
      annotations: 
        &quot;helm.sh/hooks&quot;: postn-install
    spec:
      containers:
      - name: cqlsh
        image: &lt;cassandra-image&gt;
        command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;cqlsh cassandra.my-namespace.svc.cluster.local -f /path/to/schema.cql&quot;]
        volumeMounts:
        - name: cass-init
          mountPath: /etc/config
    volumes:
      ...
</code></pre>
<p>And here is the output of <code>etc/resolv.conf</code>:</p>
<pre><code>nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndtos:5
</code></pre>

Forin
<p>The error you posted indicates that wherever you're running the cqlsh command, it cannot resolve the service name.</p>
<p>Depending on how your k8s cluster is configured and where the job runs (inside the same k8s cluster or external), you'll need to expose access to the pods with <code>Ingress</code> or <code>NodePort</code>.</p>
<p>Aleš Nosek has a good explanation of how to access pods in <a href="http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/" rel="nofollow noreferrer">his blog post here</a>. Cheers!</p>
