The purpose of a Service is to set up networking in a Kubernetes cluster.
There are four types of Services.
- ClusterIP
- NodePort - Mostly used for development purposes
- LoadBalancer
- Ingress
Here’s a sample Service config file.
apiVersion: v1
kind: Service
metadata:
name: client-node-port
spec:
# Specifying the type of Service
type: NodePort
# An array of ports
ports:
# port specifies the port that other pods can access to
- port: 3000
# targetPort specifies the port being used for a container
targetPort: 8080
# nodePort specifies the port mapped to the targetPort
nodePort: 4000
# selector will basically look for Pods that have a matching key-value pair.
# Pods that meet the criteria will use client-node-port as their Service.
selector:
component: web