文章目录
- 1. k8s弹性伸缩
- 1.1 安装heapster监控
- 1.2 弹性伸缩使用和验证
- 2. 持久化存储
- 2.1 emptyDir
- 2.2 HostPath
- 2.3 NFS
- 2.4 PV和PVC
1. k8s弹性伸缩
k8s弹性伸缩,需要附加插件heapster
1.1 安装heapster监控
使用heapster(低版本)可以监控pod压力大不大
使用hpa调节pod数量,自动扩容或者缩容
批量导入镜像:
[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-grafana
name: monitoring-grafana
namespace: kube-system
spec:
ports:
- port: 80
targetPort: 3000
selector:
name: influxGrafana
(2)"heapster-controller.yaml"资源清单
[root@k8s-master heapster]# cat heapster-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
labels:
k8s-app: heapster
name: heapster
version: v6
name: heapster
namespace: kube-system
spec:
replicas: 1
selector:
k8s-app: heapster
version: v6
template:
metadata:
labels:
k8s-app: heapster
version: v6
spec:
nodeName: 10.0.0.13
containers:
- name: heapster
image: 10.0.0.11:5000/heapster:canary
imagePullPolicy: IfNotPresent
command:
- /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: Heapster
name: heapster
namespace: kube-system
spec:
ports:
- port: 80
targetPort: 8082
selector:
k8s-app: heapster
(4)"influxdb-grafana-controller.yaml"清单
[root@k8s-master heapster]# cat influxdb-grafana-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
labels:
name: influxGrafana
name: influxdb-grafana
namespace: kube-system
spec:
replicas: 1
selector:
name: influxGrafana
template:
metadata:
labels:
name: influxGrafana
spec:
nodeName: 10.0.0.13
containers:
- name: influxdb
image: 10.0.0.11:5000/heapster_influxdb:v0.5
volumeMounts:
- mountPath: /data
name: influxdb-storage
- name: grafana
image: 10.0.0.11:5000/heapster_grafana:v2.6.0
env:
- name: INFLUXDB_SERVICE_URL
value: http://monitoring-influxdb:8086
- 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: {}
(5)"influxdb-service.yaml"资源清单
[root@k8s-master heapster]# cat influxdb-service.yaml
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
# 进行验证
[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: 2
template:
metadata:
labels:
app: dk-nginx
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
resources:
limits:
cpu: 100m
memory: 50M
requests:
cpu: 100m
memory: 50M
[root@k8s-master svc]# cat k8s_svc.yaml
apiVersion: v1
kind: Service # 简称svc
metadata:
name: myweb
spec:
type: NodePort # 默认类型为ClusterIP
ports:
- 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: nginx
namespace: default
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: nginx
targetCPUUtilizationPercentage: 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/