concepts/overview/working-with-objects/common-labels/
您可以使用比kubectl和儀表板更多的工具來視覺化和管理kubernetes物件。一組通用的標籤允許工具互操作,以所有工具都能理解的通用方式描述物件。
除了支援工具之外,推薦的標籤還以可查詢的方式描述應用程式。
後設資料是圍繞應用程式的概念組織的。kubernetes不是一個平臺即服務(platform as-as-a-service,paas),也沒有或沒有強制執行應用程式的正式概念。相反,應用程式是非正式的,用後設資料來描述。應用程式所包含內容的定義是鬆散的。
Note: These are recommended labels. They make it easier to manage applications but aren’t required for any core tooling.
共享標籤和批註共享一個公共字首:“app.kubernetes.io”。沒有字首的標籤是使用者專用的。共享字首確保共享標籤不會干擾自定義使用者標籤。
Labels
為了充分利用這些標籤,應該在每個資源物件上應用它們。
Key | Description | Example | Type |
---|---|---|---|
app.kubernetes.io/name |
The name of the application | mysql |
string |
app.kubernetes.io/instance |
A unique name identifying the instance of an application | wordpress-abcxzy |
string |
app.kubernetes.io/version |
The current version of the application (e.g., a semantic version, revision hash, etc.) | 5.7.21 |
string |
app.kubernetes.io/component |
The component within the architecture | database |
string |
app.kubernetes.io/part-of |
The name of a higher level application this one is part of | wordpress |
string |
app.kubernetes.io/managed-by |
The tool being used to manage the operation of an application | helm |
string |
To illustrate these labels in action, consider the following StatefulSet object:
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: wordpress-abcxzy
app.kubernetes.io/version: "5.7.21"
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
app.kubernetes.io/managed-by: helm
Applications And Instances Of Applications
An application can be installed one or more times into a Kubernetes cluster and, in some cases, the same namespace. For example, wordpress can be installed more than once where different websites are different installations of wordpress.應用程式可以安裝到kubernetes叢集中一次或多次,在某些情況下,也可以安裝到同一名稱空間中。例如,WordPress可以在不同網站安裝WordPress時多次安裝。
The name of an application and the instance name are recorded separately. For example, WordPress has a app.kubernetes.io/name
of wordpress
while it has an instance name, represented as app.kubernetes.io/instance
with a value of wordpress-abcxzy
. This enables the application and instance of the application to be identifiable. Every instance of an application must have a unique name.應用程式名和例項名分別記錄。例如,wordpress的“app.kubernetes.io/name”為“wordpress”,而例項名為“app.kubernetes.io/instance”,值為“wordpress abcxzy”。這使得應用程式和應用程式例項可以識別。應用程式的每個例項都必須具有唯一的名稱。
Examples
To illustrate different ways to use these labels the following examples have varying complexity.為了說明使用這些標籤的不同方法,下面的示例具有不同的複雜性。
A Simple Stateless Service
考慮使用 Deployment
and Service
objects. 下面的兩個片段代表瞭如何以最簡單的形式使用標籤。
The Deployment
用於監視執行應用程式本身的pod.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/name: myservice
app.kubernetes.io/instance: myservice-abcxzy
...
The Service
is used to expose the application.
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: myservice
app.kubernetes.io/instance: myservice-abcxzy
...
Web Application With A Database
考慮一個稍微複雜一點的應用程式:使用資料庫(mysql)的web應用程式(wordpress),使用helm安裝。下面的程式碼片段演示了用於部署此應用程式的物件的開始。
The start to the following Deployment
is used for WordPress:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/name: wordpress
app.kubernetes.io/instance: wordpress-abcxzy
app.kubernetes.io/version: "4.9.4"
app.kubernetes.io/managed-by: helm
app.kubernetes.io/component: server
app.kubernetes.io/part-of: wordpress
...
The Service
is used to expose WordPress:
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: wordpress
app.kubernetes.io/instance: wordpress-abcxzy
app.kubernetes.io/version: "4.9.4"
app.kubernetes.io/managed-by: helm
app.kubernetes.io/component: server
app.kubernetes.io/part-of: wordpress
...
MySQL is exposed as a StatefulSet
以及它所屬的更大的應用程式的後設資料:
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-abcxzy
app.kubernetes.io/version: "5.7.21"
app.kubernetes.io/managed-by: helm
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
...
The Service
is used to expose MySQL as part of WordPress:
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-abcxzy
app.kubernetes.io/version: "5.7.21"
app.kubernetes.io/managed-by: helm
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
...
With the MySQL StatefulSet
and Service
you’ll notice information about both MySQL and Wordpress, the broader application, are included.使用mysql的statefulset和service,您會注意到mysql和wordpress這兩個更廣泛的應用程式的相關資訊。
本作品採用《CC 協議》,轉載必須註明作者和本文連結