Service
is a resource you create to make a single, constant point of entry to a group of pods providing the same service. Each service has an IP address and port that never change while the service exists. Clients can open connections to that IP and port, and those connections are then routed to one of the pods backing that service. This way, clients of a service don’t need to know the location of individual pods providing the service, allowing those pods to be moved around the cluster at any time.
Creating deployment and service
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: toad-deployment
spec:
replicas: 4
selector:
matchLabels:
app: toad
template:
metadata:
labels:
app: toad
name: toad
spec:
containers:
-
image: "clivern/toad:release-0.2.4"
name: toad-app
---
apiVersion: v1
kind: Service
metadata:
name: toad-svc
labels:
app: toad
spec:
ports:
-
port: 80
targetPort: 8080
selector:
app: toad
$ kubectl create -f toad.yaml
You’re defining a service
called toad-svc
, which will accept connections on port 80
and route each connection to port 8080
of one of the pods matching the app=toad
label selector.
You can list all service resources in your namespace and see that an internal cluster IP has been assigned to your service:
$ kubectl get svc
The kubectl exec
command allows you to remotely run arbitrary commands inside an existing container of a pod.
$ kubectl get pods
$ kubectl exec ${podName} bash
If you want all requests made by a certain client to be redirected to the same pod every time, you can set the service’s sessionAffinity
property to ClientIP
(instead of None
, which is the default).
#cloud native #kubernetes #kubernetes #kubernetes-services