<p>I am having a hard time understanding Kubernetes networking and would like someone to explain it a little simpler than the documentation does.</p>
<p>My test service is below and contains all that I understand so far about ports and networking in Openshift/Kubernetes.<br />
It is returning a 503 right now.</p>
<pre class="lang-yaml prettyprint-override"><code># A Service can map any incoming port to a targetPort. 
# By default and for convenience, the `targetPort` 
# is set to the same value as the port field
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  ports:
    - name: HTTP
      # port is the stable port the Service exposes inside 
      # the cluster — other Pods in the cluster send traffic
      # to this port
      port: 5000 
      protocol: TCP
      
      # if you don’t specify a targetPort value, 
      # it defaults to the value specified in port

      # targetPort is the port that the container, 
      # within the Pod, listens on for incoming traffic.
      targetPort: 80 
      selector:
        app: my-app
</code></pre>
<pre class="lang-bash prettyprint-override"><code>route.yml - targetPort: 80
deployment.yml - containerPort: 80
</code></pre>

Justin Reddick
<p>You can read out the tiny code snippets under my Github account to get familiar with K8S networking in nutshell: <a href="https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/05-Services" rel="nofollow noreferrer">https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/05-Services</a></p>
<p>The lab will describe in detail exactly what you are asking.</p>
<ul>
<li>You can also print out the information (which you already know I assume)</li>
</ul>
<pre class="lang-bash prettyprint-override"><code>kubectl explain pod.spec.containers.ports
</code></pre>
<hr />
<p>To answer your question:</p>
<ul>
<li>The easiest way to &quot;find&quot; out what is going on is to use CLI to create the resources and examine the output</li>
</ul>
<pre class="lang-bash prettyprint-override"><code># Create ns
kubectl create ns codewizard

# Create the deployment
kubectl create deployment \
        nginx             \
        -n codewizard     \
        --image=nginx

# Create the service
kubectl expose deployment \
        nginx             \
        -n codewizard     \
        --port 5000       \
        --target-port 80  \
        --type ClusterIP  # default

# Check the service
kubectl get svc -n codewizard -o wide

# check to verify that the pod is connected
kubectl get      endpoints -n codewizard
kubectl describe endpoints -n codewizard
</code></pre>
<p><a href="https://i.stack.imgur.com/vkSNi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vkSNi.png" alt="enter image description here" /></a></p>
<hr />
<h1>Lets see the output</h1>
<h3>Service</h3>
<pre class="lang-bash prettyprint-override"><code># Get the service yaml 
kubectl get svc nginx -n codewizard -o yaml
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>### Partial...
apiVersion: v1
kind: Service
...
spec:
  ports:
  - port: 5000
    protocol: TCP
    targetPort: 80
</code></pre>
<h3>Lets try to test our network</h3>
<pre class="lang-bash prettyprint-override"><code># Create the 2nd deployment for our test
kubectl create deployment \
        multitool         \
        -n codewizard     \
        --image=praqma/network-multitool

# Lets get the pod name so we can test the connection to nginx
POD_NAME=$(kubectl get pods          \
                 --no-headers      \
                 -n codewizard     \
                 -o custom-columns=&quot;:metadata.name&quot; \
                 | grep multitool)

# Test the connection 
kubectl exec -n codewizard $POD_NAME -- curl -svL nginx:5000
</code></pre>
<p><a href="https://i.stack.imgur.com/ntHd7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ntHd7.png" alt="enter image description here" /></a></p>
