Edit This Page

给容器分配非透明整型资源

本页展示了如何给容器分配非透明整型资源。

FEATURE STATE: Kubernetes v1.13 feature-state-alpha.txt
This feature is currently in a alpha state, meaning:

  • The version names contain alpha (e.g. v1alpha1).
  • Might be buggy. Enabling the feature may expose bugs. Disabled by default.
  • Support for feature may be dropped at any time without notice.
  • The API may change in incompatible ways in a later software release without notice.
  • Recommended for use only in short-lived testing clusters, due to increased risk of bugs and lack of long-term support.

准备开始

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube, or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

在做这个练习之前,请在给节点配置非透明整型资源文档中进行练习, 该文档介绍了在一个节点上配置dongle资源。

给Pod分配非透明整型资源

为了请求一个非透明整型资源,需要在容器配置文件中包含resources:requests字段。 非透明整型资源类型前缀是pod.alpha.kubernetes.io/opaque-int-resource-

下面是含有一个容器的Pod的配置文件:

oir-pod.yaml docs/tasks/configure-pod-container
apiVersion: v1
kind: Pod
metadata:
  name: oir-demo
spec:
  containers:
  - name: oir-demo-ctr
    image: nginx
    resources:
      requests:
        pod.alpha.kubernetes.io/opaque-int-resource-dongle: 3

在配置文件中,可以看到容器请求了3个dongles资源。

创建Pod:

kubectl create -f https://k8s.io/docs/tasks/configure-pod-container/oir-pod.yaml

验证Pod是否正在运行:

kubectl get pod oir-demo

查询Pod的状态:

kubectl describe pod oir-demo

输出显示了dongle请求:

Requests:
  pod.alpha.kubernetes.io/opaque-int-resource-dongle: 3

尝试创建第二个Pod

下面是含有一个容器的Pod的配置文件。该容器请求了两个dongles资源。

oir-pod-2.yaml docs/tasks/configure-pod-container
apiVersion: v1
kind: Pod
metadata:
  name: oir-demo-2
spec:
  containers:
  - name: oir-demo-2-ctr
    image: nginx
    resources:
      requests:
        pod.alpha.kubernetes.io/opaque-int-resource-dongle: 2

Kubernetes无法再满足两个dongles的请求,因为第一个Pod已经使用了四个可用dongles中的三个。

尝试创建Pod:

kubectl create -f https://k8s.io/docs/tasks/configure-pod-container/oir-pod-2.yaml

查询Pod的状态

kubectl describe pod oir-demo-2

输出显示该Pod无法被调度,因为没有节点有两个可用的dongles资源:

Conditions:
  Type    Status
  PodScheduled  False
...
Events:
  ...
  ... Warning   FailedScheduling  pod (oir-demo-2) failed to fit in any node
fit failure summary on nodes : Insufficient pod.alpha.kubernetes.io/opaque-int-resource-dongle (1)

查看Pod的状态:

kubectl get pod oir-demo-2

输出显示Pod已创建,但是没有被调度并运行在节点上。 它的状态为Pending:

NAME         READY     STATUS    RESTARTS   AGE
oir-demo-2   0/1       Pending   0          6m

删除

删除本练习中创建的Pod:

kubectl delete pod oir-demo

接下来

对于应用开发者

对于集群管理员

反馈