Kubernetes资源申请及限制

任何的Kubernetes运行容器都应该设置资源的申请。限制的设定需要根据业务情况决定,防止容器无限制地消耗资源影响同一节点上的其他容器。资源申请及限制示例:

apiVersion: v1
kind: Service
metadata:
  name     : kafka
  namespace: infra
  labels:
    app: "kafka"
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
  - name: broker
    port: 9092
  clusterIP: None
  selector:
    app: "kafka"
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name     : kafka
  namespace: infra
  labels:
    app: "kafka"
spec:
  serviceName: kafka
  replicas: 3
  template:
    metadata:
      labels:
        app: "kafka"
    spec:
      nodeSelector:
        nodezone: b
      imagePullSecrets:
        - name: regsecret
      containers:
      - name : kafka-broker
        image: "loads/kafka:2.1.0"
        imagePullPolicy: "Always"
        livenessProbe:
          exec:
            command:
              - bin/kafka-topics.sh
              - --zookeeper
              - "zookeeper:2181"
              - --list
          initialDelaySeconds: 20
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
              - bin/kafka-topics.sh
              - --zookeeper
              - "zookeeper:2181"
              - --list
          initialDelaySeconds: 10
          timeoutSeconds: 5
        ports:
        - containerPort: 9092
          name: kafka
        resources:
          requests:
            cpu   : 5000m
            memory: 8000Mi
          requests:
            cpu   : 1000m
            memory: 2000Mi

不同的应用对于资源的要求会不一样,可以通过kubectl top指令查看运行的pod资源使用信息,并根据真实的资源使用情况逐步调整完善配置文件的资源申请信息。例如:

ijohn:~ john$ kubectl top pod -n log     
NAME                            CPU(cores)   MEMORY(bytes)   
log-agent-2rfkw                 148m         508Mi           
log-agent-9mr4z                 193m         672Mi           
log-agent-kzpd6                 230m         762Mi           
log-agent-vk7b4                 162m         681Mi           
log-archiver-66ccb7dd7b-4wpl7   3m           7Mi             
log-cleaner-5966f8cd55-vz8w7    0m           2Mi             
log-dumper-9b9d8dc5-b9vzv       398m         3773Mi          
log-kafka-0                     1518m        8829Mi          
log-kafka-1                     1341m        4480Mi          
log-kafka-2                     874m         7426Mi          
log-zookeeper-0                 6m           497Mi           
log-zookeeper-1                 6m           509Mi           
log-zookeeper-2                 7m           510Mi           
ijohn:~ john$

 

 

 

 

5 Replies to “Kubernetes资源申请及限制”

Leave a Reply

Your email address will not be published.