Here is an example of a Kubernetes Service YAML file:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Let’s break down the different sections of this YAML file:
apiVersion
: This specifies the API version that the Service object is using.kind
: This tells Kubernetes what kind of object we’re creating, in this case a Service.metadata
: This section provides information about the Service, such as its name.spec
: This section defines the specification for the Service.selector
: This specifies which Pods the Service should target. In this case, it targets any Pod with the labelapp: my-app
.ports
: This section defines the ports that the Service will listen on and forward traffic to. In this case, it listens on port 80 and forwards traffic to port 8080 on the target Pods.type
: This specifies the type of Service.ClusterIP
creates a virtual IP address within the cluster that other services can use to access the Service.
This Service YAML file creates a Service named my-service
that targets Pods with the label app: my-app
. It listens on port 80 and forwards traffic to port 8080 on the target Pods. The Service is of type ClusterIP
, meaning it creates a virtual IP address that other services can use to access it within the cluster.