From the official example of Kubernetes
documentation site on deploying a Wordpress
application with mysql:
The service definition of mysql
:
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
The deployment definition of mysql
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
My question is the following:
The Deployment
definition, has a matchLabel
selector
, so that it will match the pod defined below that has the app: wordpress
and tier:mysql
labels.
Why the Service
selector
does not require a matchLabel
directive for the same purpose? What is the "selection" of service performed upon?
The Service
is a concept that makes your container (in this case hosting wordpress) available on a given port. It maps an external port (the Node's
port) to and internal port (the container/pod's port). It does this by using the Pod's
networking capabilities. The selector is a way of specifying in the service which Pod
the port should be opened on. The Deployment
is actually just a way of grouping things together - the Pod
itself holds the Wordpress container, and the port that's defined in the service is available through the Pod
networking.
This is a simple explanation, there are different kinds of services.
According to the K8S documentation on Labels and Selectors.
The API currently supports two types of selectors: equality-based and set-based.
Newer resources, such as Job, Deployment, Replica Set, and Daemon Set, support set-based requirements as well.
Looks like new resources like Deployment support more featured set-based (with matchLabels) and the old resources like Services follow the old equality-based (without matchLabels).