在k8s中如果需要采集各个几点的资源使用情况,需要安装 metrics-server服务。
wget https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz
修改其中的配置
cd metrics-server-0.3.6/deploy/1.8+/
vim metrics-server-deployment.yaml
直接将下面内容进行替换即可。
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: metrics-server
namespace: kube-system
labels:
k8s-app: metrics-server
spec:
selector:
matchLabels:
k8s-app: metrics-server
template:
metadata:
name: metrics-server
labels:
k8s-app: metrics-server
spec:
serviceAccountName: metrics-server
volumes:
# mount in tmp so we can safely use from-scratch images and/or read-only containers
- name: tmp-dir
emptyDir: {}
containers:
- name: metrics-server
# 修改image 和 imagePullPolicy
image: mirrorgooglecontainers/metrics-server-amd64:v0.3.6
imagePullPolicy: IfNotPresent
# 新增command配置
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalDNS,InternalIP,ExternalDNS,ExternalIP,Hostname
volumeMounts:
- name: tmp-dir
mountPath: /tmp
# 新增resources配置
resources:
limits:
cpu: 300m
memory: 200Mi
requests:
cpu: 200m
memory: 100Mi
最后将当前目录的所有yaml进行安装。
kubectl apply -f metrics-server-0.3.6/deploy/1.8+/ .
我的执行结果如下
[root@k8smaster 1.8+]# kubectl apply -f .
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader configured
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator configured
Warning: rbac.authorization.k8s.io/v1beta1 RoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 RoleBinding
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader configured
Warning: apiregistration.k8s.io/v1beta1 APIService is deprecated in v1.19+, unavailable in v1.22+; use apiregistration.k8s.io/v1 APIService
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io configured
serviceaccount/metrics-server configured
deployment.apps/metrics-server configured
service/metrics-server configured
clusterrole.rbac.authorization.k8s.io/system:metrics-server configured
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server configured
如果出现这种情况,请稍等一会。
[root@k8smaster 1.8+]# kubectl top nodes
error: metrics not available yet
出现如下信息,说明安装成功,k8s已经可以查看到节点资源使用信息。
[root@k8smaster ~]# kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8smaster 214m 10% 1074Mi 62%
k8snode 77m 3% 846Mi 49%
本次演练cpu使用率超过某个值时,自动扩容,因此,笔者通过springboot启动服务,通过如下代码模拟cpu计算。
@GetMapping("hpa")
public String hpa(HttpServletResponse out){
Double num= 0.01d;
for(int i=0;i<1000000;i++){
System.out.println (num);
num += Math.sqrt (i);
}
return "suc";
}
准备完成之后将springboot工程打包。
FROM image_jdk1.8
MAINTAINER sandishui
RUN mkdir /usr/local/webapp
WORKDIR /usr/local/webapp
ADD alipay.jar /usr/local/webapp
ENTRYPOINT ["nohup","java","-jar","alipay.jar","&"]
docker build -t java-server .
[root@k8smaster java_server]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java-server latest 56fe85337d8a 20 hours ago 636MB
准备yaml文件,直接复制即可。如果镜像不同,注意镜像名称、拉取方式、service的type。8082是我springboot服务端口号。
apiVersion: apps/v1
kind: Deployment
metadata:
name: java-server
spec:
selector:
matchLabels:
run: java-server
replicas: 1
template:
metadata:
labels:
run: java-server
spec:
containers:
- name: java-server
image: java-server
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8082
resources:
requests:
memory: 50Mi
cpu: 50m
---
apiVersion: v1
kind: Service
metadata:
name: java-server
labels:
run: java-server
spec:
ports:
- port: 8082
selector:
run: java-server
type: NodePort
生成yaml之后,通过yaml创建。
kubectl apply -f java_server.yaml
查看是否启动成功。我这边已经正常启动,通过service的31752可以正常的访问springboot服务。
[root@k8smaster k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
java-server-7554ff68d-ss9kc 1/1 Running 0 16h
nginx-6799fc88d8-gv8r6 1/1 Running 2 26d
[root@k8smaster k8s]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
java-server NodePort 10.109.116.150 <none> 8082:31752/TCP 16h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28d
nginx NodePort 10.109.58.243 <none> 80:32024/TCP 25d
使 Pod 的副本数量维持在 1 到 10 之间。 大致来说,HPA 将(通过 Deployment)增加或者减少 Pod 副本的数量以保持所有 Pod 的平均 CPU 利用率在 50% 左右(由于每个 Pod 请求 200 毫核的 CPU,这意味着平均 CPU 用量为 100 毫核)。自己测试,可以自行修改参数。
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
创建成功之后查看hpa状态。当前cpu使用为4%。
[root@k8smaster k8s]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
java-server Deployment/java-server 4%/50% 1 5 1 17h
通过jmeter一直请求springboot服务进行测试。发现hpa状态。
[root@k8smaster k8s]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
java-server Deployment/java-server 302%/50% 1 5 5 17h
此时pod数量。从之前的一个变为多个。
[root@k8smaster k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
java-server-7554ff68d-7jnpr 1/1 Running 0 45s
java-server-7554ff68d-7wx57 1/1 Running 0 45s
java-server-7554ff68d-jxmhm 1/1 Running 0 45s
java-server-7554ff68d-ss9kc 1/1 Running 0 16h
java-server-7554ff68d-xht6r 1/1 Running 0 30s
nginx-6799fc88d8-gv8r6 1/1 Running 2 26d
然后关闭jmeter之后,pod数量一会就会降下来。
注:pod的伸缩的时间比较久,它不是一个瞬发的过程,cpu使用率升高或者降低都需要等一会才能看到效果。在cpu扩容服务器时大概在1分钟就可以看到效果。cpu使用率降低,需要等几分钟才能看到pods数降低。
java-server-7554ff68d-xht6r 1/1 Running 0 30s
nginx-6799fc88d8-gv8r6 1/1 Running 2 26d
然后关闭jmeter之后,pod数量一会就会降下来。
注:pod的伸缩的时间比较久,它不是一个瞬发的过程,cpu使用率升高或者降低都需要等一会才能看到效果。在cpu扩容服务器时大概在1分钟就可以看到效果。cpu使用率降低,需要等几分钟才能看到pods数降低。