Kubernetes Classroom Series – 16/Oct/2019, Labels, Selectors and Annotations

Challenges With Multiple Pods

Preview

Challenge with Continuous Deployments

Preveiw

To Solve above Challenges

  • Controllers
  • Services
  • Labels

Labels

Preview

  • Labels are key-value pairs.
  • They can be applied to any k8s resources.
  • Labels can be used retreive by using key-value pairs
Give me the pods which have label app=tomcat
Give me the pods which have label app=tomcat and env=production
  • Labels are used in k8s by various resources
  • In this example today we would see the usage by Replication Controller

Sample Commands to know the Label and RC connection

  • Create a pod1.yaml file for Pod using
---
apiVersion: v1
kind: Pod
metadata:
  name: mytomcat
  labels:
    env: dev
    app: tomcat
    ver: "8"
spec:
  containers:
    - image: tomcat:8
      name: tomcat
      ports:
        - containerPort: 8080
          protocol: TCP
  • Execute kubectl apply -f pod1.yml
  • Create a pod2.yaml file for Pod using
---
apiVersion: v1
kind: Pod
metadata:
  name: mytomcattest
  labels:
    env: test
    app: tomcat
    ver: "8"
spec:
  containers:
    - image: tomcat:8
      name: tomcat
      ports:
        - containerPort: 8080
          protocol: TCP
  • Execute kubectl apply -f pod2.yml
  • Execute following commands
kubectl get pods
# observe labels
kubectl get pod mytomcat -oyaml
kubectl get pod mytomcattest -oyaml
  • Now lets create rc.yml with two replicas of tomcat
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: tomcat-rc
spec:
  replicas: 2
  selector:
    app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
        - image: tomcat:8
          name: tomcat
          ports:
            - containerPort: 8080
              protocol: TCP
  • Execute the following commands
kubectl apply -f rc.yml
kubectl get pods

  • No new pods are created by RC as there are two pods already existing with matching labels.
  • To test this, lets make a small change
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: tomcat-rc
spec:
  replicas: 2
  selector:
    app: tomcat
    env: dev
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
        - image: tomcat:8
          name: tomcat
          ports:
            - containerPort: 8080
              protocol: TCP

  • Observe the selector section, this matches only with one existing pod. Now if you apply you should see 3 pods in total in your k8s cluster
kubectl apply -f rc.yml
kubectl get pods

Annotations

  • Annoatations are also key values pairs.
  • Annotations are for external usage. Sample is
---
apiVersion: v1
kind: Pod
metadata:
  name: mytomcattest
  labels:
    env: test
    app: tomcat
    ver: "8"
  annotations:
    commit-id: 0c345f
    author: khaja
    organization: qualitythought
spec:
  containers:
    - image: tomcat:8
      name: tomcat
      ports:
        - containerPort: 8080
          protocol: TCP

Controller Daemonset.

  • Create a yaml file for daemon set.
  • Daemon set create a pod on every node

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner