K8s弹性伸缩需要附加组件heapster监控。 |
一、 安装Heapster监控
1. 工作原理
2. 部署服务
step 1 上传、导入镜像,并为镜像添加标签
heapster服务 提取码:tdi6 |
[root@k8s-registry ~]# for i in `ls docker_heapster*.tar.gz`; do docker load -i $i; done
......
[root@k8s-registry ~]# docker tag docker.io/kubernetes/heapster_grafana:v2.6.0 10.0.0.140:5000/heapster_grafana:v2.6.0
[root@k8s-registry ~]# docker tag docker.io/kubernetes/heapster_influxdb:v0.5 10.0.0.140:5000/heapster_influxdb:v0.5
[root@k8s-registry ~]# docker tag docker.io/kubernetes/heapster:canary 10.0.0.140:5000/heapster:canary
[root@k8s-registry ~]# docker push 10.0.0.140:5000/heapster:canary
......
[root@k8s-registry ~]# docker push 10.0.0.140:5000/heapster_influxdb:v0.5
......
[root@k8s-registry ~]# docker push 10.0.0.140:5000/heapster_grafana:v2.6.0
......
step 2 创建heapster配置文件并应用
# heapster服务Deployment资源
[root@k8s-master ~/k8s_yaml/heapster]# cat heapster-controller.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
k8s-app: heapster
name: heapster
version: v6
name: heapster
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: heapster
version: v6
spec:
containers:
- name: heapster
image: 10.0.0.140:5000/heapster:canary
imagePullPolicy: IfNotPresent
#Always/IfNotPresent/Never
command:
- /heapster
- --source=kubernetes:http://10.0.0.110:10000?inClusterConfig=false
- --sink=influxdn:http://monitoring-influxdb:8086
# influxdb服务和Grafana服务Deployment资源
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
name: influxGrafana
name: influx-grafana
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
name: influxGrafana
spec:
containers:
- name: influxdb
image: 10.0.0.140:5000/heapster_influxdb:v0.5
volumeMounts:
- mountPath: /data
name: influxdb-storage
- name: grafana
image: 10.0.0.140:5000/heapster_grafana:v2.6.0
env:
- name: INFLUXDB_SERVICE_URL
value: http://monitoring-influxdb:8086
# The following env variables are required to make Grafana accessible via
# the kubernetes api-server proxy. On production clusters, we recommend
# removing these env variables, setup auth for grafana, and expos the grafana
# service using a LoadBalancer or a public IP.
- name: GF_AUTH_BASIC_ENABLED
value: "false"
- name: GF_AUTH_ANONYMOUS_ENABLED
value: "true"
- name: GF_AUTH_ANONYMOUS_ORG_ROLE
value: Admin
- name: GF_SERVER_ROOT_URL
value: /api/v1/proxy/namespaces/kube-system/services/monitoring-grafana/
volumeMounts:
- mountPath: /var
name: grafana-storage
volumes:
- name: influxdb-storage
emptyDir: {}
- name: grafana-storage
emptyDir: {}
# heapster服务Service资源
apiVersion: v1
kind: Service
metadata:
labels:
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: Heapster
name: heapster
namespace: kube-system
spec:
ports:
- port: 80
targetPort: 8082
selector:
k8s-app: heapster
# influxdb服务Service资源
apiVersion: v1
kind: Service
metadata:
labels: null
name: monitoring-influxdb
namespace: kube-system
spec:
ports:
- name: http
port: 8083
targetPort: 8083
- name: api
port: 8086
targetPort: 8086
selector:
name: influxGrafana
# grafana服务Service资源
apiVersion: v1
kind: Service
metadata:
labels:
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: monitoring-grafana
name: monitoring-grafana
namespace: kube-system
spec:
# In a production setup, we recommend accessing Grafana through an external Loadbalancer
# or through a public IP.
# type: LoadBalancer
ports:
- port: 80
targetPort: 3000
selector:
name: influxGrafana
[root@k8s-master ~]# mkdir k8s_yaml/heapster && cd k8s_yaml/heapster
[root@k8s-master ~/k8s_yaml/heapster]# kubectl -s http://10.0.0.110:10000 create -f .
service "monitoring-grafana" created
deployment "heapster" created
service "heapster" created
deployment "influx-grafana" created
service "monitoring-influxdb" created
[root@k8s-master ~/k8s_yaml/heapster]# kubectl -s http://10.0.0.110:10000 get svc -o wide -n kube-system
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
heapster 10.100.131.98 <none> 80/TCP 6m k8s-app=heapster
kube-dns 10.100.1.1 <none> 53/UDP,53/TCP 237d k8s-app=kube-dns
kubernetes-dashboard 10.100.154.244 <none> 80/TCP 23h k8s-app=kubernetes-dashboard
monitoring-grafana 10.100.250.83 <none> 80/TCP 6m name=influxGrafana
monitoring-influxdb 10.100.7.71 <none> 8083/TCP,8086/TCP 6m name=influxGrafana
[root@k8s-master ~/k8s_yaml/heapster]# kubectl -s http://10.0.0.110:10000 get pod -n kube-system -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
heapster-3438138036-n319x 1/1 Running 0 31m 192.168.56.3 10.0.0.120 k8s-app=heapster,pod-template-hash=3438138036,version=v6
influx-grafana-2235217320-hbvqj 2/2 Running 0 31m 192.168.56.4 10.0.0.120 name=influxGrafana,pod-template-hash=2235217320
kube-dns-2134347918-7lz53 4/4 Running 0 17m 192.168.45.3 10.0.0.130 k8s-app=kube-dns,pod-template-hash=2134347918
kube-dns-2134347918-90f80 4/4 Running 0 17m 192.168.45.2 10.0.0.130 k8s-app=kube-dns,pod-template-hash=2134347918
kubernetes-dashboard-latest-2686154000-77357 1/1 Running 1 3h 192.168.56.2 10.0.0.120 k8s-app=kubernetes-dashboard,kubernetes.io/cluster-service=true,pod-template-hash=2686154000,version=latest
二、 部署弹性伸缩
1. 工作原理
hpa: Horizontal Pod Autoscaler
2. 创建hpa资源(Shell方式)
# 准备环境
[root@k8s-master ~/k8s_yaml/heapster]# cd ../Deployment/
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.1l
deployment "nginx" created
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.1
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.100.0.1 <none> 443/TCP 3h
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 expose deploy nginx --name=svc-nginx --type=NodePort --port=80 --target-port=80
service "svc-nginx" exposed
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get deployment -o wide --show-labels
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE LABELS
nginx 4 4 4 4 4m app=nginx
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get svc -o wide
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes 10.100.0.1 <none> 443/TCP 3h <none>
svc-nginx 10.100.254.138 <nodes> 80:28475/TCP 51s app=nginx
[root@k8s-master ~/k8s_yaml/Deployment]# curl -I 10.0.0.120:28475
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Tue, 13 Jul 2021 07:25:16 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes
shell方式
kubectl autoscale 资源类型 资源名称 --max=pod创建数量上限 --min=pod保留数量下限 --cpu-percent=cpu使用百分比 #创建hpa资源 kubectl get horizontalpodautoscaler #查看hpa资源 kubectl get hpa #查看hpa资源 |
在实际业务场景中,容器CPU使用百分比达到60%左右,建议进行弹性伸缩。 |
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 autoscale deployment nginx --max=10 --min=2 --cpu-percent=7
deployment "nginx" autoscaled
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get horizontalpodautoscaler
NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE
nginx Deployment/nginx 7% 0% 2 10 13s
3. 压测演示
ab -n 总访问次数 -c 单次请求数 http://域名:端口/资源 |
[root@k8s-master ~/k8s_yaml/Deployment]# ab -n 1000000 -c 1000 http://10.0.0.120:28475/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 10.0.0.120 (be patient)
......
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get horizontalpodautoscaler
NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE
nginx Deployment/nginx 7% 32% 2 10 14m
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get deployment nginx
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 10 10 10 10 1h
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get hpa nginx
NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE
nginx Deployment/nginx 7% 0% 2 10 18m
[root@k8s-master ~/k8s_yaml/Deployment]# kubectl -s http://10.0.0.110:10000 get deployment nginx
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 2 2 2 2 1h