我有一个kubernetes设置,下面是
sn. type service namespace
1. statefulset rabbitmq rabbitmq
2. deployment pods default
3. hpa hpa default
度量被导出到'/api/custom.metrics.k8s。Io/v1beta1如下
{
"kind": "APIResourceList",
"apiVersion": "v1",
"groupVersion": "custom.metrics.k8s.io/v1beta1",
"resources": [
{
"name": "services/rabbitmq_queue_messages_ready",
"singularName": "",
"namespaced": true,
"kind": "MetricValueList",
"verbs": [
"get"
]
},
{
"name": "namespaces/rabbitmq_queue_messages_ready",
"singularName": "",
"namespaced": false,
"kind": "MetricValueList",
"verbs": [
"get"
]
},
{
"name": "pods/rabbitmq_queue_messages_ready",
"singularName": "",
"namespaced": true,
"kind": "MetricValueList",
"verbs": [
"get"
]
}
]
}
hpa配置
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: demo-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-pod2
minReplicas: 1
maxReplicas: 2
metrics:
- type: Object
object:
metric:
name: "rabbitmq_queue_messages_ready"
describedObject:
apiVersion: custom.metrics.k8s.io/v1/beta1
kind: Service
name: rabbitmq
target:
type: Value
value: 50
每当我尝试在default
命名空间中部署hpa时,我都会得到以下错误:
ScalingActive False FailedGetObjectMetric the HPA was unable to compute the replica count: unable to get metric rabbitmq_queue_messages_ready: Service on default rabbitmq/unable to fetch metrics from custom metrics API: the server could not find the metric rabbitmq_queue_messages_ready for services rabbitmq
但是当相同的hpa被设置为名称空间rabbitmq
时,使用hpa
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: demo-hpa
namespace: rabbitmq
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-pod2
minReplicas: 1
maxReplicas: 2
metrics:
- type: Object
object:
metric:
name: "rabbitmq_queue_messages_ready"
describedObject:
apiVersion: custom.metrics.k8s.io/v1/beta1
kind: Service
name: rabbitmq
target:
type: Value
value: 50
和部署。yaml是
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-pod2
labels:
app: test-pod2
namespace: rabbimtq
spec:
replicas: 1
selector:
matchLabels:
app: test-pod2
template:
metadata:
labels:
app: test-pod2
spec:
containers:
- name: test-pod2
image: golang:1.16
command: ["sh", "-c", "tail -f /etc/hosts"]
部署工作完美。
是否有一种方法,我可以从rabbitmq
命名空间导出服务rabbitmq的指标到任何其他命名空间然后我可以用它来缩放??
HPA是一个命名空间资源。这意味着它只能扩展与HPA本身在同一名称空间中的部署。这就是为什么只有当HPA和部署都在namespace: rabbitmq
中时才有效。您可以在您的集群中检查它,通过运行:
kubectl api-resources --namespaced=true | grep hpa
NAME SHORTNAMES APIGROUP NAMESPACED KIND
horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler
使其工作的最简单方法是简单地将HPA的Namespace
值设置为您想要扩展的部署名称空间。例如,如果您的部署设置如下:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: rabbimtq
HPA也必须在同一个命名空间中:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
namespace: rabbitmq
换句话说:将HPA的metadata.namespace
值设置为部署的metadata.namespace
值,因为HPA无法在其命名空间之外扩展部署。
命名空间就像单独的"在您的集群中。命名空间资源不能在它们所在的命名空间之外工作。他们无法看到不在他们的"框"中的资源。
这样做就不需要重新配置自定义度量。