HPA in EKS based on number of active connections

0

I want to use Websockets from FastAPI for my pod to be deployed in EKS.

Now my idea is to keep the number of active websocket connections rather constant. I thought of using the Prometheus-Adapter for it.

However, I am rather inexperienced in setting things up. Do you have suggestions or perhaps even an example?

asked a year ago474 views
1 Answer
0

Yes, you can use the Prometheus-Adapter to autoscale your EKS pods based on the number of active websocket connections.

Here is an example of how you can set it up:

  1. First, you need to deploy the Prometheus-Adapter in your EKS cluster. You can use the following command to deploy it:
kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/prometheus-adapter/master/deploy/manifests/prometheus-adapter.yaml
  1. Next, you need to create a Custom Metrics API server. You can use the following YAML configuration to create a Custom Metrics API server:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: custom-metrics
  namespace: kube-system

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: custom-metrics
rules:
- apiGroups: ["custom.metrics.k8s.io"]
  resources: ["*"]
  verbs: ["*"]

---

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: custom-metrics
subjects:
- kind: ServiceAccount
  name: custom-metrics
  namespace: kube-system
roleRef:
  kind: ClusterRole
  name: custom-metrics
  apiGroup: rbac.authorization.k8s.io

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: custom-metrics
  namespace: kube-system
  labels:
    k8s-app: custom-metrics
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: custom-metrics
  template:
    metadata:
      labels:
        k8s-app: custom-metrics
    spec:
      serviceAccountName: custom-metrics
      containers:
      - image: k8s.gcr.io/custom-metrics-stackdriver-adapter:v0.10.0
        name: custom-metrics-adapter
        args:
        - --source=stackdriver
        - --stackdriver.metrics-prefix=custom.googleapis.com/
        - --config=/etc/config/config.yaml
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
      - name: config-volume
        configMap:
          name: custom-metrics-config
  1. Create a configuration file for the Prometheus-Adapter. Here is an example configuration file that retrieves the number of active websocket connections:
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-adapter-config
  namespace: kube-system
data:
  metrics:
    - seriesQuery: 'websocket_connections{job="your-app"}'
      resources:
        overrides:
          resource: 'pod'
      name:
        matches: "websocket_connections"
        as: "websocket_connections"
      metricsQuery: 'max_over_time({{{.Series}}}[5m])'
  1. Finally, you can create a Horizontal Pod Autoscaler (HPA) using the following command:
kubectl autoscale deployment your-app --cpu-percent=50 --min=1 --max=10 --name=your-app-hpa

This HPA will scale up or down based on the number of active websocket connections.

You can also use the **kubectl get hpa **command to check the status of the HPA and see how many replicas are currently running.

I hope this helps! Let me know if you have any questions.

hash
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions