<p>I have a kubernetes service, and I wanna it to target pods.</p>

<p>This is how I define my service:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    selector:
            app: myapp
    ports:
        - port: 77
          targetPort: 80
          nodePort: 32766
    type: NodePort
</code></pre>

<p>and this is how I define my deployment:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
    name: my-deployment
spec:
    replicas: 5
    selector:
        matchLabels:
            app: myapp
    template:
        metadata:
            name: my-pod
            labels:
                app: myapp
        spec:
            containers:
                - name: httd
                  image: httpd
                  imagePullPolicy: Always
                  ports:
                      - containerPort: 80
</code></pre>

<p>Basically what I did is linking port 80 in the pod to port 77 in the service to port 32766 in the node.</p>

<p>I already know that my container is running on port 80 because If I do this:</p>

<pre><code>docker run -p 8989:80 httpd
</code></pre>

<p>and ask for localhost:8989 I can see the page.</p>

<p>If I do kubetctl get services I get:</p>

<pre><code>NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        &lt;none&gt;        443/TCP        46h
my-service   NodePort    10.100.157.161   &lt;none&gt;        77:32766/TCP   20m
</code></pre>

<p>I tried calling:</p>

<pre><code>10.100.157.161:32766
10.100.157.161:77
</code></pre>

<p>But both give connection error.</p>

<p>What did I miss?</p>

Jack Thomson
<p>Use the below YAMLs. It works</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: my-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - image: httpd
        name: httpd
        imagePullPolicy: Always
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: myapp
  ports:
  - port: 77
    targetPort: 80
    nodePort: 32766
  type: NodePort
</code></pre>

<pre><code>master $ kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        &lt;none&gt;        443/TCP        15m
my-service   NodePort    10.110.219.232   &lt;none&gt;        77:32766/TCP   12s
master $
master $
master $ curl 10.110.219.232:77
&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
master $
master $
master $ curl $(hostname -i):32766
&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
master $
</code></pre>
