问题描述
sn. type service namespace
1. statefulset rabbitmq rabbitmq
2. deployment pods default
3. hpa hpa default
指标在`/apis/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","namespaced": false,{
"name": "pods/rabbitmq_queue_messages_ready","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 与 set do namespace rabbitmq
with 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
和 deployment.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 是一种 namespaced 资源。这意味着它只能扩展与 HPA 本身在同一命名空间中的部署。这就是为什么它仅在 HPA 和 Deployment 都在 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
值设置为 Deployment 的 metadata.namespace
值,因为 HPA 无法在其命名空间之外扩展 Deployment。
命名空间就像集群中的独立“盒子”。命名空间资源不能在它们所在的命名空间之外工作。他们看不到不在他们“盒子”里的资源。
通过这样做,无需重新配置自定义指标。