iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 16
1
Kubernetes

15 分鐘學習系列 - 第一次學 Kubernetes 就上手系列 第 16

Day 16 - Kubernetes Label 與 Selector

上一篇筆記介紹 Namespace, 這一篇筆記將介紹 label, 並且可以使用 selector 篩選應用程式

  • Label 是一個 key/value 的標籤, 通常是用具有意義且可以辨別特定物件的屬性與分類.

Label 的格式如下:
apiVersion: v1
kind: Pod
metadata:
name: homepage-dev
namespace: web
labels:
env: development
dev-lead: karthik
team: web
application_type: ui
release-version: "12.0"

我們再次從範例部署檔案 sample-infrastructure.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: web
---
apiVersion: v1
kind: Namespace
metadata:
  name: auth
---
apiVersion: v1
kind: Namespace
metadata:
  name: cart
---
apiVersion: v1
kind: Namespace
metadata:
  name: social
---
apiVersion: v1
kind: Namespace
metadata:
  name: catalog
---
apiVersion: v1
kind: Namespace
metadata:
  name: quote
---
apiVersion: v1
kind: Namespace
metadata:
  name: purchasing
---
apiVersion: v1
kind: Namespace
metadata:
  name: infra
---
apiVersion: v1
kind: Pod
metadata:
  name: homepage-dev
  namespace: web
  labels:
    env: development
    dev-lead: karthik
    team: web
    application_type: ui
    release-version: "12.0"
spec:
  containers:
  - name: helloworld
    image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: homepage-staging
  namespace: web
  labels:
    env: staging
    team: web
    dev-lead: karthik
    application_type: ui
    release-version: "12.0"
spec:
  containers:
  - name: helloworld
    image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: homepage-prod
  namespace: web
  labels:
    env: production
    team: web
    dev-lead: karthik
    application_type: ui
    release-version: "12.0"
spec:
  containers:
  - name: helloworld
    image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: login-dev
  namespace: auth
  labels:
    env: development
    team: auth
    dev-lead: jim
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: login
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: login-staging
  namespace: auth
  labels:
    env: staging
    team: auth
    dev-lead: jim
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: login
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: login-prod
  namespace: auth
  labels:
    env: production
    team: auth
    dev-lead: jim
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: login
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: cart-dev
  namespace: cart
  labels:
    env: development
    team: ecommerce
    dev-lead: carisa
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: cart
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: cart-staging
  namespace: cart
  labels:
    env: staging
    team: ecommerce
    dev-lead: carisa
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: cart
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: cart-prod
  namespace: cart
  labels:
    env: production
    team: ecommerce
    dev-lead: carisa
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: cart
    image: karthequian/ruby:latest
---

apiVersion: v1
kind: Pod
metadata:
  name: social-dev
  namespace: social
  labels:
    env: development
    team: marketing
    dev-lead: carisa
    application_type: api
    release-version: "2.0"
spec:
  containers:
  - name: social
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: social-staging
  namespace: social
  labels:
    env: staging
    team: marketing
    dev-lead: marketing
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: social
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: social-prod
  namespace: social
  labels:
    env: production
    team: marketing
    dev-lead: marketing
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: social
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: catalog-dev
  namespace: catalog
  labels:
    env: development
    team: ecommerce
    dev-lead: daniel
    application_type: api
    release-version: "4.0"
spec:
  containers:
  - name: catalog
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: catalog-staging
  namespace: catalog
  labels:
    env: staging
    team: ecommerce
    dev-lead: daniel
    application_type: api
    release-version: "4.0"
spec:
  containers:
  - name: catalog
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: catalog-prod
  namespace: catalog
  labels:
    env: production
    team: ecommerce
    dev-lead: daniel
    application_type: api
    release-version: "4.0"
spec:
  containers:
  - name: catalog
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: quote-dev
  namespace: quote
  labels:
    env: development
    team: ecommerce
    dev-lead: amy
    application_type: api
    release-version: "2.0"
spec:
  containers:
  - name: quote
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: quote-staging
  namespace: quote
  labels:
    env: staging
    team: ecommerce
    dev-lead: amy
    application_type: api
    release-version: "2.0"
spec:
  containers:
  - name: quote
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: quote-prod
  namespace: quote
  labels:
    env: production
    team: ecommerce
    dev-lead: amy
    application_type: api
    release-version: "1.0"
spec:
  containers:
  - name: quote
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: ordering-dev
  namespace: purchasing
  labels:
    env: development
    team: purchasing
    dev-lead: chen
    application_type: backend
    release-version: "2.0"
spec:
  containers:
  - name: ordering
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: ordering-staging
  namespace: purchasing
  labels:
    env: staging
    team: purchasing
    dev-lead: chen
    application_type: backend
    release-version: "2.0"
spec:
  containers:
  - name: ordering
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: ordering-prod
  namespace: purchasing
  labels:
    env: production
    team: purchasing
    dev-lead: chen
    application_type: backend
    release-version: "2.0"
spec:
  containers:
  - name: ordering
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: logger
  namespace: infra
  labels:
    env: production
    team: infrastructure
    dev-lead: tracey
    application_type: backend
    release-version: "2.0"
spec:
  containers:
  - name: logger
    image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
  name: monitor
  namespace: infra
  labels:
    env: production
    team: infrastructure
    dev-lead: tracey
    application_type: backend
    release-version: "2.0"
spec:
  containers:
  - name: monitor
    image: karthequian/ruby:latest
---
  • Selector 具有以下的幾種格式
    1. Equality-based (=,==,!=)
    2. Set-based (in, notin, exists)

    使用命令 kubectl get pods -l env=staging --all-namespaces 列出所有 env=staging 標籤
PS C:\k8s\namespaces> kubectl get pods -l env=staging --all-namespaces
NAMESPACE    NAME               READY     STATUS      RESTARTS   AGE
auth         login-staging      1/1       Running     8          1h
cart         cart-staging       0/1       Completed   7          1h
catalog      catalog-staging    1/1       Running     7          1h
purchasing   ordering-staging   1/1       Running     7          1h
quote        quote-staging      1/1       Running     7          1h
social       social-staging     1/1       Running     7          1h
web          homepage-staging   1/1       Running     0          1h

使用命令 kubectl get pods -l 'env in (development,production)' --all-namespaces -o wide 列出所有 env 是 development 或 production 標籤

PS C:\k8s\namespaces> kubectl get pods -l 'env in (development,production)' --all-namespaces -o wide
NAMESPACE    NAME            READY     STATUS    RESTARTS   AGE       IP          NODE
auth         login-dev       1/1       Running   60         2d        10.1.1.14   docker-for-desktop
auth         login-prod      1/1       Running   61         2d        10.1.1.9    docker-for-desktop
cart         cart-dev        1/1       Running   61         2d        10.1.1.18   docker-for-desktop
cart         cart-prod       1/1       Running   61         2d        10.1.1.26   docker-for-desktop
catalog      catalog-dev     1/1       Running   60         2d        10.1.1.31   docker-for-desktop
catalog      catalog-prod    1/1       Running   60         2d        10.1.1.23   docker-for-desktop
infra        logger          1/1       Running   60         2d        10.1.1.29   docker-for-desktop
infra        monitor         1/1       Running   60         2d        10.1.1.35   docker-for-desktop
purchasing   ordering-dev    1/1       Running   60         2d        10.1.1.33   docker-for-desktop
purchasing   ordering-prod   1/1       Running   60         2d        10.1.1.34   docker-for-desktop
quote        quote-dev       1/1       Running   60         2d        10.1.1.19   docker-for-desktop
quote        quote-prod      1/1       Running   60         2d        10.1.1.20   docker-for-desktop
social       social-dev      1/1       Running   61         2d        10.1.1.16   docker-for-desktop
social       social-prod     1/1       Running   60         2d        10.1.1.22   docker-for-desktop
web          homepage-dev    1/1       Running   3          2d        10.1.1.30   docker-for-desktop
web          homepage-prod   1/1       Running   3          2d        10.1.1.27   docker-for-desktop

有了 label 與 selector, 便可以快速篩選出物件囉.


參考資料:


上一篇
Day 15 - 淺談 Kubernetes Namespaces
下一篇
Day 17 - 如何重新設定 Docker 連線到 Azure Kubernetes Service
系列文
15 分鐘學習系列 - 第一次學 Kubernetes 就上手30

尚未有邦友留言

立即登入留言