K8S Resources - klagan/learning GitHub Wiki

Getting started

Labels

Labels are basically tags. A dimension to categorise the cluster resources. Below is an example of a service resource named web being assigned a label of env with a value of prod.

The inference of this assignation is that the service resource is for a production environment. If all cluster resources designated for production were labelled this way we would have a way of selecting them which will be detailed later.

  apiVersion: v1
  kind: Service
  metadata:
    name: web
    labels:
      env: prod
  spec:
    ports:
      - port: 80
      selector:
        component: eShop

Selectors

The selector section is used to reference other cluster resources. We can do this by:

  • a selector
  • a match label
  • a matchLabel

Selector

Some cluster resources like a service do not have provisions for any other selector than a label selector the matchLabel key is redundant.

  apiVersion: v1
  kind: Service
  metadata:
    name: web
    labels:
      env: prod
  spec:
    ports:
      - port: 80
      selector:
        component: eShop

Newer cluster resources have provisions for matchLabels and matchExpressions

  apiVersion: v1
  kind: Deployment
  metadata:
    name: app
    labels:
      env: prod
  spec:
    replicas: 3
    selector:
      matchLabels:
        component: eShop
  apiVersion: v1
  kind: ReplicaSet
  metadata:
    name: basket
    labels:
      env: prod
  spec:
    replicas: 3
    selector:
      matchExpressions:
      - key: component
        operator: In
        values:
        - eShop

Annotations

Annotations are used in much the same way as labels but for a different purpose. The purpose is to provide additional information such as owner of the resource, telephone number, online documentation url etc.

  apiVersion: v1
  kind: ReplicaSet
  metadata:
    name: basket
    labels:
      env: prod
    annotations: {
        "help" : "https://help.com/replicaSet",
        "owner" : "Kam Lagan"
    }
  spec:
    replicas: 3
    selector:
      matchLabels:
        component: eShop

Template

A specification selector is a definition of how and what to monitor. The example details that the cluster should monitor any cluster resources with a label of nginx and ensure they are running three copies.

The template details what the resources created by the deployment should look like. The example details that any pods created by the deployment should be assigned a label of nginx.

resource targeting

Sources

datree google

⚠️ **GitHub.com Fallback** ⚠️