When a Kubernetes cluster managed by Enterprise Distributed Application Service (EDAS) runs many applications and versions, identifying specific pods becomes difficult. Labels -- key-value pairs such as app=frontend or env=production -- let you tag pods so you can filter them with kubectl, target them with scheduling rules, and organize them for group management.
Add labels to pods during deployment
Add custom labels to application pods when you create or deploy an application. After deployment, use kubectl to filter pods by those labels.
Procedure
Log on to the EDAS console.
In the left-side navigation pane, click Application Management > Applications. In the top navigation bar, select a region. In the upper part of the page, select a namespace. Select Container Service or Serverless Kubernetes Cluster from the Cluster Type drop-down list. Then, find the application that you want to deploy and click the application name.
In the upper-right corner of the Application Overview page, choose .
On the Select Deployment Mode page, select a deployment mode and click Start Deployment in the upper-right corner of the deployment mode section.
NoteThis example uses the Release to All at Once mode.
On the Release to All at Once page, modify the deployment parameter settings, click Label Configuration, and then configure name-value pairs. Click OK.
NoteSpecify the application name as the value in the name-value pair.
Verify the labels
After deployment, run the following kubectl command to filter pods by label:
kubectl get pod -l demo-app-name=sc-server-demoExpected output:
NAME READY STATUS RESTARTS AGE
sc-server-demo-group-1-1-5795d5487c-**** 2/2 Running 0 2m58sThe output confirms that the pod labeled demo-app-name=sc-server-demo is running.
Schedule pods to the same node using pod affinity
Pod affinity rules tell Kubernetes to schedule pods onto the same node (or topology domain) as pods that match a specific label. This is useful when two tightly coupled applications need low-latency communication -- for example, a client service and the server it depends on.
The following diagram shows the target state: after you configure pod affinity, Kubernetes places both applications on the same node.
| Node | Pods |
|---|---|
cn-hangzhou.192.168.AAA.BBB | sc-server-demo (server), sc-client-demo (client) |
Prerequisites
Before you begin, make sure that you have:
An application with the label
demo-app-name=sc-server-demoalready deployed. If you have not added this label yet, complete the steps in Add labels to pods during deployment
Procedure
In this example, you configure pod affinity for a second application so that its pods land on the same node as the previously labeled application.
Log on to the EDAS console.
In the left-side navigation pane, click Application Management > Applications. In the top navigation bar, select a region. In the upper part of the page, select a namespace. Select Container Service or Serverless Kubernetes Cluster from the Cluster Type drop-down list. Then, find the application that you want to deploy and click the application name.
In the upper-right corner of the Application Overview page, choose .
On the Select Deployment Mode page, select a deployment mode and click Start Deployment in the upper-right corner of the deployment mode section.
NoteThis example uses the Release to All at Once mode.
On the Release to All at Once page, modify the application environment and deployment package, and click Scheduling Rules.
In the Select a mode field, select Custom Configuration. On the Pod Affinity tab, create a scheduling rule with the following values:
Field Value Description Topology Key kubernetes.io/hostnameScopes the rule to individual nodes. Application Tag Key demo-app-nameThe label key to match against. Operator InThe pod is scheduled on a node where a pod carries a label with a value in the specified set. Application Tag Value sc-server-demoThe label value to match. This rule instructs Kubernetes to schedule the current application's pods onto the same node (
kubernetes.io/hostname) as pods that carry the labeldemo-app-name=sc-server-demo.Click Label Configuration and configure the
demo-app-name=sc-client-demoname-value pair. Then, click OK.
Verify pod co-location
After deployment, run the following kubectl commands to check which node each application's pods are running on:
kubectl get pod -l demo-app-name=sc-client-demo -o wide
kubectl get pod -l demo-app-name=sc-server-demo -o wideExpected output:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
sc-client-demo-group-1-1-5c5cbb9889-**** 2/2 Running 0 24s 10.85.***.*4 cn-hangzhou.192.168.AAA.BBB <none> <none>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
sc-server-demo-group-1-1-5795d5487c-**** 2/2 Running 0 35m 10.85.***.*3 cn-hangzhou.192.168.AAA.BBB <none> <none>Both pods are running on the same node (cn-hangzhou.192.168.AAA.BBB), confirming that the pod affinity rule is working as expected.
What to do next
When you deploy applications, you can create scheduling rules on the Pod Affinity tab and Pod Anti-affinity tab. For details, see Configure scheduling rules.