k8s HPA
安装metrics-server支持HPA
https://github.com/kubernetes-sigs/metrics-server
Configuration
Depending on your cluster setup, you may also need to change flags passed to the Metrics Server container. Most useful flags:
--kubelet-preferred-address-types
- The priority of node address types used when determining an address for connecting to a particular node (default [Hostname,InternalDNS,InternalIP,ExternalDNS,ExternalIP])--kubelet-insecure-tls
- Do not verify the CA of serving certificates presented by Kubelets. For testing purposes only.--requestheader-client-ca-file
- Specify a root certificate bundle for verifying client certificates on incoming requests.
下载components.yaml
https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
修改components.yaml(- --kubelet-insecure-tls 不验证证书 image使用aliyun镜像)
kubectl apply -f ./components.yaml
获取cpu memory指标
Kubectl top nodes
示例
是一个简单的tomcatdemo,对数据进行查询 http://192.168.164.11:30001/demo/
myweb
apiVersion: apps/v1
kind: Deployment
metadata:
name: myweb
labels:
app: myweb
spec:
selector:
matchLabels:
app: myweb
replicas: 2
strategy:
type: Recreate
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: kubeguide/tomcat-app:v1
ports:
- containerPort: 8080
resources:
requests:
cpu: 20m
limits:
cpu: 200m
env:
- name: MYSQL_SERVICE_HOST
value: 10.1.128.91
- name: MYSQL_SERVICE_PORT
value: "3306"
myweb-service
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30001
selector:
app: myweb
mysql
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.5
ports:
- name: container3306
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: '123456'
mysql-service
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: myweb-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myweb
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
或者使用命令
kubectl autoscale deployment myweb --cpu-percent=50 --min=2 --max=10
压测