<p>Is it possible to map Kubernetes service to a specific port for a group of pods (deployement)?
E.g. I have service (just as an example)</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 8081
    targetPort: 8081
</code></pre>

<p>And I want this service be available as <a href="http://localhost:8081/" rel="nofollow noreferrer">http://localhost:8081/</a> in my pods from some specific deployment. </p>

<p>It seems to me that I saw this in K8S docs several days ago, but I can not find this right now. </p>

Denis
<p>It may be beneficial to review your usage of K8s services. If you had exposed a deployment of pods as a service, then your service will define the port mappings, and you will be able to access your service on its cluster DNS name on the service port. </p>

<p>If you must access your service via localhost, I am assuming your use case is some tightly coupled containers in your pod. In which case, you can define a "containerPort" in your deployment yaml, and add the containers that need to communicate with each other on localhost in the same pod. </p>

<p>If by <code>localhost</code> you are referring to your own local development computer, you can do a <code>port-forward</code>. As long as the port-forwarding process is running, you can access the pods' ports from your <code>localhost</code>. Find more on <a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="nofollow noreferrer">port-forwarding</a>. Simple example: </p>

<pre><code>kubectl port-forward redis-master-765d459796-258hz 6379:6379 
# or
kubectl port-forward service/redis 6379:6379 
</code></pre>

<p>Hope this helps!</p>
