Monday, December 14, 2020

Kubernetes - Lab - Label and selectors

 
Labels and selectors
go down for lab
------------------------
Kubernetes objects 
Imperative LAB tasks
Here, is a POD definition file.
# cat pod-definition-file.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end-service
sepc: 
    nodeName: 
    containers:
    - name: nginx-container
      image: nginx:1.19
      ports:
        - containerPort: 8080
-------------------------------------------------------
Group them based on need
label - 
class kind and color
we created different objects - pod, services, replica sets, deployment 
group and select
Labels
app: app1
function: frond-end
app: app2
function: web-services
app: app3
function: Images-proc
app: app5
function: DB
How do you specify lables in kubernetes
In pod definition file, under metadata, there is a section called labels
# cat pod-definition-file.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp1
    function: Front-end
    type: front-end-service
sepc: 
    containers:
    - name: nginx-container
      image: nginx:1.19
      ports:
        - containerPort: 8080
# kc apply -f pod.yaml
# kc get pods --selector app=app1
Lets create a replicaSet with three replicas
# cat pod-definition-file.yaml
apiVersion: v1
kind: Replicaset
metadata:
  name: myapp-pod
  labels:
    app: myApp1 # this is the label for replicaset
    function: Front-end
  annotations:
      buildversion: 1.34
sepc: 
  replicas: 3
  selector:
    matchLabels:
      app: myApp1
  template:
    metadata:
      labels:
        app: myApp1 # label defined at template section is for POD
        function: Front-end
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.19
Lab - Label and selectors
--------------------------------
1. We have deployed a number of PODs. They are labelled with 'tier', 'env' and 'bu'. How many PODs exist in the 'dev' environment?
Use selectors to filter the output
-> Run the command 'kubectl get pods --selector env=dev'
# kc get pods
# kc get pors --show-labels
# kc get pods -l env=dev --no-headers | wc -l 
or
# kc get pods --selector env=dev 
2. How many PODs are in the 'finance' business unit ('bu')?
-> Run the command 'kubectl get pods --selector bu=finance'
# kc get pods --selector bu=finance 
or
# kc get pods -l bu=finance --no-headers | wc -l
3. How many objects are in the 'prod' environment including PODs, ReplicaSets and any other objects?
-> Run the command 'kubectl get all --selector env=prod'
# kc get pods --selector env=prod
or
kc get pods -l env=prod --no-headers
we need all object, not only pod, so run all
# kc get all -l env=prod --no-headers | wc -l
count all output, here 7
5. Identify the POD which is part of the prod environment, the finance BU and of frontend tier?
-> Run the command 'kubectl get all --selector env=prod,bu=finance,tier=frontend'
# kc get pods --selector env=prod,bu=finance,tier=frontend
# kc get all --selector env=prod,bu=finance,tier=frontend
or
# kc get pods -l env=prod,bu=finance,tier=frontned
6. A ReplicaSet definition file is given 'replicaset-definition-1.yaml'. Try to create the replicaset. There is an issue with the file. Try to fix it.
    ReplicaSet: replicaset-1
    Replicas: 2 
-> Set the labels on the pod definition template to frontend
here, lets go ahead and create replicaset
# kc create -f replica-definition-1.yaml
error with selector does not match template 'labels'
edit the file,
# cat replica-definition-1.yaml
apiVersion: v1
kind: ReplicaSet
metadata:
  name: replicaset-1
 
sepc: 
    replicas: 2
    selector:
      matchLabels:
        tier: frontend
    template:
      metadata:
        labels:
          tier: frontend
      spec:
        containers:
        - name: nginx
          image: nginx
x
x

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...