Home > Enterprise >  Kubernetes (GKE) names , labels, selectors, matchLables in manifest files
Kubernetes (GKE) names , labels, selectors, matchLables in manifest files

Time:02-06

I have a question about labels and names, in this example manifest file

apiVersion: apps/v

1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

I can see that the name of the deployment is "nginx-deployment" and the pod name is "nginx"? or is it the running container? Then I see in the console that the pods would have a hash attached to the end of its name, I believe this is the revision number? I just want to decipher the names from the labels from the matchLables, so for example I can use this service manifest to expose the pods with a certain label:

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 60000
    targetPort: 80

will this service expose all pods with the selector : app:nginx ?

thanks

CodePudding user response:

The Deployment has your specifed name "nginx-deployment".

However you did not define a Pod with a fixed name, you define a template for the pods managed by this deployment.

The Deployment manages 3 pods (because of your replicas 3), so it will use the template to build this three pods.

There will also be a Replica Set with a hash and this will manage the Pods, but this is better seen by following the example below.

Since a deployment can manage multiple pods (like your example with 3 replicas) or needing one new Pod when updating them, a deployment will not exactly use the name specified in the template, but will always append a hash value to keep them unique.

But now you would have the problem to have all Pods loadbalanced behind one Kubernetes Service, because they have different names.

This is why you denfine a label "app:nignx" in your template so all 3 Pods will have this label regardless of there name and other labels set by Kubernetes.

The Service uses the selector to find the correct Pods. In your case it will search them by label "app:nginx".

So yes the Service will expose all 3 Pods of your deployment and will loadbalance trafic between them.

You can use --show-labels for kubectl get pods to see the name and the assigned labels.

For a more complete example see: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

  • Related