一起来学k8s 22.二进制k8s集群安装Metrics-Server

二进制k8s集群安装Metrics-Server

环境准备

##/etc/hosts
192.168.48.101 master01
192.168.48.102 master02
192.168.48.103 master03
192.168.48.201 node01
192.168.48.202 node02
## keepalived的vip
192.168.48.66
IP Hostname CPU Memory
192.168.48.101 master01 2 4G
192.168.48.102 master02 2 4G
192.168.48.103 master03 2 4G
192.168.48.201 node01 2 4G
192.168.48.202 node02 2 4G
软件 版本
kubernetes 1.15.2
docker-ce 19.03
calico 3.8
etcd 3.3.13
CNI 0.8.1
coredns 1.4.0
metrics-server 0.3.3

下载地址

https://github.com/kubernetes-incubator/metrics-server

一起来学k8s 22.二进制k8s集群安装Metrics-Server_第1张图片
项目下载地址
链接: https://pan.baidu.com/s/1Jnzgs0OBS2K2j4_dflFc0Q 提取码: pfvf

[root@master01 ~]# unzip metrics-server-0.3.6.zip 

yaml文件在metrics-server-0.3.6/deploy/1.8+/

[root@master01 ~]# cd metrics-server-0.3.6/deploy/1.8+/
[root@master01 1.8+]# ll
total 28
-rw-r--r-- 1 root root 393 Oct 14 20:42 aggregated-metrics-reader.yaml
-rw-r--r-- 1 root root 308 Oct 14 20:42 auth-delegator.yaml
-rw-r--r-- 1 root root 329 Oct 14 20:42 auth-reader.yaml
-rw-r--r-- 1 root root 298 Oct 14 20:42 metrics-apiservice.yaml
-rw-r--r-- 1 root root 804 Oct 14 20:42 metrics-server-deployment.yaml
-rw-r--r-- 1 root root 291 Oct 14 20:42 metrics-server-service.yaml
-rw-r--r-- 1 root root 517 Oct 14 20:42 resource-reader.yaml

安装

修改yaml文件,添加参数

[root@master01 ~]# cd metrics-server-0.3.6/deploy/1.8+/
[root@master01 1.8+]# ll
total 28
-rw-r--r-- 1 root root 384 Jul 11 02:22 aggregated-metrics-reader.yaml
-rw-r--r-- 1 root root 308 Jul 11 02:22 auth-delegator.yaml
-rw-r--r-- 1 root root 329 Jul 11 02:22 auth-reader.yaml
-rw-r--r-- 1 root root 298 Jul 11 02:22 metrics-apiservice.yaml
-rw-r--r-- 1 root root 815 Jul 11 02:22 metrics-server-deployment.yaml
-rw-r--r-- 1 root root 291 Jul 11 02:22 metrics-server-service.yaml
-rw-r--r-- 1 root root 502 Jul 11 02:22 resource-reader.yaml

[root@master01 1.8+]# vim metrics-server-deployment.yaml 
...
- name: metrics-server
        image: k8s.gcr.io/metrics-server-amd64:v0.3.6
        imagePullPolicy: IfNotPresent
        args:
          - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
          - --kubelet-insecure-tls

一起来学k8s 22.二进制k8s集群安装Metrics-Server_第2张图片

metrics-server镜像

k8s.gcr.io/metrics-server-amd64:v0.3.6
[root@master01 ~]# docker pull gcr.azk8s.cn/google-containers/metrics-server-amd64:v0.3.6

[root@master01 ~]# docker tag gcr.azk8s.cn/google-containers/metrics-server-amd64:v0.3.6 k8s.gcr.io/metrics-server-amd64:v0.3.6

链接: https://pan.baidu.com/s/1wnKCpBcY2YNKKTPwSnVW0g 提取码: agnv

导入metrics-server镜像

docker load -i metrics-0.3.6.tar.gz

创建metrics-server

[root@master01 ~]# cd metrics-server-0.3.6/deploy/1.8+/
[root@master01 1.8+]# kubectl apply -f .
[root@master01 1.8+]# kubectl get pod --all-namespaces 
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-7bd78b474d-r2tdd   1/1     Running   1          20h
kube-system   calico-node-cfckb                          1/1     Running   1          20h
kube-system   calico-node-kzqg6                          1/1     Running   1          20h
kube-system   calico-node-msmv2                          1/1     Running   1          20h
kube-system   calico-node-p2kvc                          1/1     Running   1          20h
kube-system   calico-node-q8tr4                          1/1     Running   1          20h
kube-system   coredns-5c6c9cf6c8-ppmf4                   1/1     Running   1          20h
kube-system   coredns-5c6c9cf6c8-x2sj2                   1/1     Running   1          20h
kube-system   metrics-server-55cb6d6b57-m9clg            1/1     Running   0          9s

[root@master01 ~]# kubectl top pods -n kube-system 
NAME                                       CPU(cores)   MEMORY(bytes)   
calico-kube-controllers-7bd78b474d-r2tdd   3m           14Mi            
calico-node-cfckb                          18m          70Mi            
calico-node-kzqg6                          20m          74Mi            
calico-node-msmv2                          18m          71Mi            
calico-node-p2kvc                          19m          71Mi            
calico-node-q8tr4                          19m          70Mi            
coredns-5c6c9cf6c8-ppmf4                   4m           10Mi            
coredns-5c6c9cf6c8-x2sj2                   3m           10Mi            
metrics-server-55cb6d6b57-m9clg            1m           14Mi            
[root@master01 ~]# kubectl top nodes
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master01   194m         9%     884Mi           23%       
master02   177m         8%     780Mi           20%       
master03   174m         8%     782Mi           20%       
node01     76m          3%     514Mi           13%       
node02     82m          4%     431Mi           11%       

HPA 水平伸缩

版本

通过kubectl api-versions可以看到,目前有3个版本:

autoscaling/v1            #只支持通过cpu为参考依据,来改变pod副本数
autoscaling/v2beta1       #支持通过cpu、内存、连接数以及用户自定义的资源指标数据为参考依据。
autoscaling/v2beta2       #同上,小的变动

部署HPA

SVC、Deployment资源清单

apiVersion: v1
kind: Service
metadata:
  name: svc-hpa
  namespace: default
spec:
  selector:
    app: myapp
  type: NodePort
  ports:
  - name: http
    port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp-demo
      namespace: default
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
        resources:
          requests:
            cpu: 50m
            memory: 50Mi
          limits:
            cpu: 50m
            memory: 50Mi

HPA资源清单

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: myapp-hpa-v2
  namespace: default
spec:
  minReplicas: 1         ##至少1个副本
  maxReplicas: 8         ##最多8个副本
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 50  ##注意此时是根据使用率,也可以根据使用量:targetAverageValue
  - type: Resource
    resource:
      name: memory
      targetAverageUtilization: 50  ##注意此时是根据使用率,也可以根据使用量:targetAverageValue

测试

[root@master01 ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
myapp-5b79fd9968-w77mb   1/1     Running   0          50s
[root@master01 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1               443/TCP        20h
svc-hpa      NodePort    10.102.225.82           80:30122/TCP   58s

使用ab工具模拟压力测试

yum install -y httpd-tools  
ab -r -c 1000 -n 5000000 http://master01:30122/hostname.html
[root@master01 ~]# kubectl get pod -w
NAME                     READY   STATUS    RESTARTS   AGE
myapp-5b79fd9968-w77mb   1/1     Running   0          10m
myapp-5b79fd9968-wn45p   0/1     Pending   0          0s
myapp-5b79fd9968-wn45p   0/1     Pending   0          0s
myapp-5b79fd9968-wn45p   0/1     ContainerCreating   0          0s
myapp-5b79fd9968-wn45p   0/1     ContainerCreating   0          3s
myapp-5b79fd9968-wn45p   1/1     Running             0          9s
myapp-5b79fd9968-7hh5t   0/1     Pending   0          0s
myapp-5b79fd9968-7hh5t   0/1     Pending   0          0s
myapp-5b79fd9968-7hh5t   0/1     ContainerCreating   0          0s
myapp-5b79fd9968-7hh5t   0/1     ContainerCreating   0          3s
myapp-5b79fd9968-7hh5t   1/1     Running             0          4s

你可能感兴趣的:(kubernetes)