文章目录
- 1. k8s弹性伸缩
- 1.1 安装heapster监控
- 1.2 弹性伸缩使用和验证
- 2. 持久化存储
- 2.1 emptyDir
1. k8s弹性伸缩
k8s弹性伸缩,需要附加插件heapster
1.1 安装heapster监控
使用heapster(低版本)可以监控pod压力大不大
使用hpa调节pod数量,自动扩容或者缩容
1.先去连接api-server,然后拿到node节点的信息
2. 再去node节点监控取值,把值存储到数据库
3. 然后用grafana出图,可以用dashboard调用
开始安装heapster监控:
批量导入镜像:
[root@k8s-node-2 heapster]# for image in `ls *.tar.gz`; do docker load -i $image; done
[root@k8s-node-2 heapster]# docker tag docker.io/kubernetes/heapster_grafana:v2.6.0 10.0.0.11:5000/heapster_grafana:v2.6.0
[root@k8s-node-2 heapster]# docker tag docker.io/kubernetes/heapster_influxdb:v0.5 10.0.0.11:5000/heapster_influxdb:v0.5
[root@k8s-node-2 heapster]# docker tag docker.io/kubernetes/heapster:canary 10.0.0.11:5000/heapster:canary编写资源清单:
(1)"grafana-service.yaml"资源清单
[root@k8s-master heapster]# cat grafana-service.yaml
apiVersion: v1
kind: Service
metadata:labels:kubernetes.io/cluster-service: 'true'kubernetes.io/name: monitoring-grafananame: monitoring-grafananamespace: kube-system
spec:ports:- port: 80targetPort: 3000selector:name: influxGrafana(2)"heapster-controller.yaml"资源清单
[root@k8s-master heapster]# cat heapster-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:labels:k8s-app: heapstername: heapsterversion: v6name: heapsternamespace: kube-system
spec:replicas: 1selector:k8s-app: heapsterversion: v6template:metadata:labels:k8s-app: heapsterversion: v6spec:nodeName: 10.0.0.13containers:- name: heapsterimage: 10.0.0.11:5000/heapster:canaryimagePullPolicy: IfNotPresentcommand:- /heapster- --source=kubernetes:http://10.0.0.11:8080?inClusterConfig=false# monitoring-influxdb依赖dns配置,必须要安装k8s的dns- --sink=influxdb:http://monitoring-influxdb:8086(3)"heapster-service.yaml"资源清单
[root@k8s-master heapster]# cat heapster-service.yaml
apiVersion: v1
kind: Service
metadata:labels:kubernetes.io/cluster-service: 'true'kubernetes.io/name: Heapstername: heapsternamespace: kube-system
spec:ports:- port: 80targetPort: 8082selector:k8s-app: heapster(4)"influxdb-grafana-controller.yaml"清单
[root@k8s-master heapster]# cat influxdb-grafana-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:labels:name: influxGrafananame: influxdb-grafananamespace: kube-system
spec:replicas: 1selector:name: influxGrafanatemplate:metadata:labels:name: influxGrafanaspec:nodeName: 10.0.0.13containers:- name: influxdbimage: 10.0.0.11:5000/heapster_influxdb:v0.5volumeMounts:- mountPath: /dataname: influxdb-storage- name: grafanaimage: 10.0.0.11:5000/heapster_grafana:v2.6.0env:- name: INFLUXDB_SERVICE_URLvalue: http://monitoring-influxdb:8086- name: GF_AUTH_BASIC_ENABLEDvalue: "false"- name: GF_AUTH_ANONYMOUS_ENABLEDvalue: "true"- name: GF_AUTH_ANONYMOUS_ORG_ROLEvalue: Admin- name: GF_SERVER_ROOT_URLvalue: /api/v1/proxy/namespaces/kube-system/services/monitoring-grafana/volumeMounts:- mountPath: /varname: grafana-storagevolumes:- name: influxdb-storageemptyDir: {}- name: grafana-storageemptyDir: {}(5)"influxdb-service.yaml"资源清单
[root@k8s-master heapster]# cat influxdb-service.yaml
apiVersion: v1
kind: Service
metadata:labels: nullname: monitoring-influxdbnamespace: kube-system
spec:ports:- name: httpport: 8083targetPort: 8083- name: apiport: 8086targetPort: 8086selector:name: influxGrafana# 进行验证
[root@k8s-master heapster]# kubectl apply -f .
[root@k8s-master heapster]# kubectl get all -n kube-system
## 多了heapter和grafana服务
[root@k8s-master heapster]# kubectl cluster-info
1.2 弹性伸缩使用和验证
(1) 编写一个deployment资源和svc资源实现访问
[root@k8s-master deployment]# cat 01-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:name: nginx
spec:replicas: 2template:metadata:labels:app: dk-nginxspec:containers:- name: nginximage: 10.0.0.11:5000/nginx:1.13ports:- containerPort: 80resources:limits:cpu: 100mmemory: 50Mrequests:cpu: 100mmemory: 50M[root@k8s-master svc]# cat k8s_svc.yaml
apiVersion: v1
kind: Service # 简称svc
metadata:name: myweb
spec:type: NodePort # 默认类型为ClusterIPports:- port: 80 # clusterVIP端口nodePort: 30000 # 指定基于NodePort类型对外暴露的端口,若不指定,则会在"30000-32767"端口访问内随机挑选一个未监听的端口暴露targetPort: 80 # 指定后端Pod服务监听的端口selector:app: ycy-web # 指定匹配的Pod对应的标签
(2)生成一个hpa资源
# 1. 命令行的形式生成,在内存中有hpa的yaml,--max:指定最大的Pod数量, --min:指定最小的Pod数量, --cpu-percent:指定CPU的百分比
[root@k8s-master k8s_yaml]# kubectl autoscale deployment nginx --max=10 --min=1 --cpu-percent=10# 2. 导出刚刚设置的hpa的yaml[root@k8s-master k8s_yaml]# kubectl get hpa nginx -o yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:name: nginxnamespace: default
spec:maxReplicas: 10minReplicas: 1scaleTargetRef:apiVersion: extensions/v1beta1kind: Deploymentname: nginxtargetCPUUtilizationPercentage: 10# (3) 查看hpa资源
[root@k8s-master k8s_yaml]# kubectl get all
[root@k8s-master k8s_yaml]# kubectl get rs -o wide
[root@k8s-master k8s_yaml]# kubectl get hpa nginx -o yaml
[root@k8s-master k8s_yaml]# kubectl describe hpa nginx
[root@k8s-master k8s_yaml]# kubectl describe deployment nginx
(3) 进行压测,验证是否自动伸缩
# 1. 安装ab压测工具
[root@k8s-master k8s_yaml]# yum install httpd-tools -y
# 2. -n 发起请求数 -c 并发数
[root@k8s-master k8s_yaml]# ab -n 10000 -c 2000 http://10.0.0.12:30148/
常用命令:
kubectl get all -n kube-system
kubectl cluster-info
# 查看pod的资源使用情况
kubectl top -n default pod test-32rf5
# 删除对应的资源
kubectl delete rc --all
kubectl delete deploment --all
kubectl delete daemonset --all
2. 持久化存储
tomcat+mysql的案例中,Pod成功后可以添加自定义的数据,数据被存储在MySQL数据库实例中,批量删除容器,docker rm -f `docker ps -a -q,删除容器后,这些容器会被K8S重新创建,尽管业务恢复了,但我们的数据也丢失了。为了解决容器被删除后数据不丢失,则引入了存储类型,类似于docker中的数据卷
提示:以通过kubectl exec -it mysql-698607359-dvmzk bash
进入到容器并通过"env"查看MySQL的root用户密码
数据卷的分类:
数据卷:(1)kubernetes中的Volume提供了在容器中挂载外部存储的能力;(2)Pod需要设置卷来源(po.spec.volumes)和挂载点(po.spec.containers.volumeMounts)两个信息后才可以使用相应的volume;数据卷类型大致分类:本地数据卷:hostPath,emptyDir等。网络数据卷:NFS,Ceph,GlusterFS等。公有云:AWS,EBS等;K8S资源:configmap,secret等。
官方文档: https://kubernetes.io/docs/concepts/storage/volumes/
2.1 emptyDir
查看帮助文档 kubectl explain pod.spec.containers.volumeMounts
emptyDir 临时的空目录,随着pod的生命周期,删掉pod,空目录内的内容也会小时,想实现数据共享就不能使用emptyDir资源,deploment 设置了三个pod,那么每个pod都会生成一个empty,所以多个Pod不能使用该类型进行数据共享,但同一个Pod的多个容器可以基于该类型进行数据共享。
使用场景:存放日志,pod与pod之间empty挂载不同的数据
spec:nodeName: 10.0.0.13volumes:- name: mysql-dataemptyDir: {}containers:- name: wp-mysqlimage: 10.0.0.11:5000/mysql:5.7imagePullPolicy: IfNotPresentports:- containerPort: 3306volumeMounts:- name: mysql-datamountPath: /var/lib/mysql