Kubernetes - Services ποΈ
Description π
A service
is a way to expose an application running on a set of pods
as a network service
. It is services
that enables connectivity between pods
and external clients.
Types of Services
π¦π©πͺ
NodePort
- makes an internal port accessible on the
node
- uses three ports
- one port on the
node
, thenode port
- the
node
ports range is between 30000-32767
- the
- one port on the
pod
, thetarget port
- one port on the
service
, theservice port
- makes an internal port accessible on the
ClusterIP
- creates a virtual IP inside the
cluster
that allows otherservices
to communicate with it - helps provide a single interface to a set of pods
- creates a virtual IP inside the
LoadBalancer
- provisions a
load balancer
- only works on cloud providers that support native
load balancers
- only works on cloud providers that support native
- provisions a
More on ClusterIP
β¨
CusterIP
is used to expose a service
to other pods
in the cluster
. It provides a single interface that other pods
can use to access the service
. This enables the ability to easily and efficiently scale the service
by adding or removing pods
as needed.
Creating a Service
π οΈ
-
basic
service
definitionapiVersion: v1 kind: Service metadata: name: webapp-service spec: type: NodePort ports: # only port is needed, other fields are optional - targetPort: 8080 port: 8080 # port on the service object nodePort: 30080 # selector is used to find the pods to route traffic to selector: name: simple-webapp
what do you do when you have multiple
pods
?when the
service
is created, it will select allpods
that match theselector
and route traffic to them. No additional configuration is needed. π Itβs magic. π
Basic Commands
π
- get list of
services
kubectl get services
- get descriptive information on the desired
service
kubectl describe service <service-name>
- access
service
in browser running onminikube
minikube service <service-name> --url
Examples π§©
-
sample
NodePort service
definitionapiVersion: v1 kind: Service metadata: name: myapp-service spec: type: NodePort ports: - targetPort: 80 port: 80 nodePort: 30008 selector: app: myapp type: front-end
-
sample
ClusterIP service
definitionapiVersion: v1 kind: Service metadata: name: back-end spec: type: ClusterIP ports: - targetPort: 80 port: 80 selector: app: myapp type: back-end
-
sample
service
fordeployment
withweb-hooks
apiVersion: v1 kind: Service metadata: name: webhook-server namespace: webhook-demo spec: selector: app: webhook-server ports: - port: 443 targetPort: webhook-api