#
# Balancer scaling 2 deployments using priority policy and hpa.
#
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-1
  labels:
    app: nginx-1
    srv: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-1
      srv: nginx
  template:
    metadata:
      labels:
        app: nginx-1
        srv: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-2
  labels:
    app: nginx-2
    srv: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-2
      srv: nginx
  template:
    metadata:
      labels:
        app: nginx-2
        srv: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
---
apiVersion: balancer.x-k8s.io/v1alpha1
kind: Balancer
metadata:
  name: nginx
spec:
  replicas: 5
  selector:
    matchLabels:
      srv: nginx
  policy:
    policyName: priority
    priorities:
      targetOrder: [nginx-1,nginx-2]
    fallback:
      startupTimeoutSeconds: 180
  targets:
    - name: nginx-1
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: nginx-1
      minReplicas: 1
      maxReplicas: 7
    - name: nginx-2
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: nginx-2
      minReplicas: 1
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    srv: nginx
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
spec:
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - resource:
      name: cpu
      target:
        averageUtilization: 50
        type: Utilization
    type: Resource
  scaleTargetRef:
    apiVersion: balancer.x-k8s.io/v1alpha1
    kind: Balancer
    name: nginx
