Kubernetes manifest service for deployment

1/23/2021

So final manifest will be next :

apiVersion: v1
kind: Service
metadata:
  name: apiserver-service
  labels:
    app: apiserver
spec:
  selector:
    app: apiserver
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30005
  type: NodePort

It will work for defining specific targetport

-- Kwaker
kubernetes
manifest

2 Answers

1/23/2021

Service is an abstract way to expose your application running on a set of Pods. This one is a manifest for creating a service, here targetPort: 8080 is the pod port. In this manifest there are basically two parts, one is metadata which gives the service name and also give it a label. Then the spec part, which is short form of specification, it basically the specification of the service, here the selector is given, and also the ports are specified here, port represents the service port, targetPort represents the port on which the service will send requests. By nodePort the outside world (from outside the cluster) can communicate to the service, and finally type represents the type of the service. If the type = NodePort then it is basically means from outside the cluster the service will expose a port (nodePort).

apiVersion: v1
kind: Service
metadata:
  name: apiserver-service
  labels:
    app: apiserver
spec:
  selector:
    app: apiserver
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30005
  type: NodePort
-- Sahadat Hossain
Source: StackOverflow

1/23/2021

The first example in Kubernetes Service documentation Defining a Service contains what you ask, a Service where port: and targetPort: is different.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
-- Jonas
Source: StackOverflow