• kubernetes集群编排(8)


    目录

    k8s资源监控

    资源限制

    limitrange

    ResourceQuota

    metrics-server

    dashboard

    k9s

    hpa


    k8s资源监控

    资源限制

    上传镜像

    1. [root@k8s2 limit]# vim limit.yaml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. name: memory-demo
    6. spec:
    7. containers:
    8. - name: memory-demo
    9. image: stress
    10. args:
    11. - --vm
    12. - "1"
    13. - --vm-bytes
    14. - 200M
    15. resources:
    16. requests:
    17. memory: 50Mi
    18. limits:
    19. memory: 100Mi

    limitrange

    1. [root@k8s2 limit]# cat range.yaml
    2. apiVersion: v1
    3. kind: LimitRange
    4. metadata:
    5. name: limitrange-memory
    6. spec:
    7. limits:
    8. - default:
    9. cpu: 0.5
    10. memory: 512Mi
    11. defaultRequest:
    12. cpu: 0.1
    13. memory: 256Mi
    14. max:
    15. cpu: 1
    16. memory: 1Gi
    17. min:
    18. cpu: 0.1
    19. memory: 100Mi
    20. type: Container

    1. [root@k8s2 limit]# kubectl apply -f range.yaml
    2. [root@k8s2 limit]# kubectl describe limitrange

    创建的pod自动添加限制

    [root@k8s2 limit]# kubectl run demo --image nginx
    

    自定义限制的pod也需要在limitrange定义的区间内

    ResourceQuota

    1. [root@k8s2 limit]# vim range.yaml
    2. apiVersion: v1
    3. kind: LimitRange
    4. metadata:
    5. name: limitrange-memory
    6. spec:
    7. limits:
    8. - default:
    9. cpu: 0.5
    10. memory: 512Mi
    11. defaultRequest:
    12. cpu: 0.1
    13. memory: 256Mi
    14. max:
    15. cpu: 1
    16. memory: 1Gi
    17. min:
    18. cpu: 0.1
    19. memory: 100Mi
    20. type: Container
    21. ---
    22. apiVersion: v1
    23. kind: ResourceQuota
    24. metadata:
    25. name: mem-cpu-demo
    26. spec:
    27. hard:
    28. requests.cpu: "1"
    29. requests.memory: 1Gi
    30. limits.cpu: "2"
    31. limits.memory: 2Gi
    32. pods: "2"

    1. [root@k8s2 limit]# kubectl apply -f range.yaml
    2. [root@k8s2 limit]# kubectl describe resourcequotas

    配额是针对namespace施加的总限额,命名空间内的所有pod资源总和不能超过此配额

    创建的pod必须定义资源限制

    kubectl edit quota mem-cpu-demo   编辑指定名称的资源配额对象

    下载部署文件

    [root@k8s2 metrics]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
    

    修改部署文件

    上传镜像到harbor

    1. [root@k8s2 metrics]# kubectl apply -f components.yaml
    2. [root@k8s2 metrics]# kubectl -n kube-system get pod

    如有问题,可以查看日志

    [root@k8s2 metrics]# kubectl -n kube-system logs metrics-server-5d54764497-8vjm5

    1. [root@k8s2 metrics]# kubectl top node
    2. [root@k8s2 metrics]# kubectl top pod -A --sort-by cpu

    下载部署文件

    [root@k8s2 dashboard]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
    

    上传所需镜像到harbor

    修改svc为loadbalancer

    1. [root@k8s2 dashboard]# kubectl apply -f recommended.yaml
    2. [root@k8s2 dashboard]# kubectl -n kubernetes-dashboard edit svc kubernetes-dashboard

    集群需要部署metallb-system,如果没有可以使用NodePort方式

    访问https://192.168.81.101

    授权

    1. [root@k8s2 dashboard]# vim rbac.yaml
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. kind: ClusterRoleBinding
    4. metadata:
    5. name: admin-user
    6. roleRef:
    7. apiGroup: rbac.authorization.k8s.io
    8. kind: ClusterRole
    9. name: cluster-admin
    10. subjects:
    11. - kind: ServiceAccount
    12. name: kubernetes-dashboard
    13. namespace: kubernetes-dashboard

    获取token

    1. [root@k8s2 dashboard]# kubectl apply -f rbac.yaml
    2. [root@k8s2 dashboard]# kubectl -n kubernetes-dashboard create token kubernetes-dashboard

    使用token登录网页

     

    k9s

    1. [root@k8s2 dashboard]# tar zxf k9s.tar
    2. [root@k8s2 dashboard]# ./k9s

    hpa

    官网:HorizontalPodAutoscaler 演练 | Kubernetes

    上传镜像

    1. [root@k8s2 hpa]# vim hpa.yaml
    2. apiVersion: apps/v1
    3. kind: Deployment
    4. metadata:
    5. name: php-apache
    6. spec:
    7. selector:
    8. matchLabels:
    9. run: php-apache
    10. replicas: 1
    11. template:
    12. metadata:
    13. labels:
    14. run: php-apache
    15. spec:
    16. containers:
    17. - name: php-apache
    18. image: hpa-example
    19. ports:
    20. - containerPort: 80
    21. resources:
    22. limits:
    23. cpu: 500m
    24. requests:
    25. cpu: 200m
    26. ---
    27. apiVersion: v1
    28. kind: Service
    29. metadata:
    30. name: php-apache
    31. labels:
    32. run: php-apache
    33. spec:
    34. ports:
    35. - port: 80
    36. selector:
    37. run: php-apache

    1. [root@k8s2 hpa]# kubectl apply -f hpa.yaml
    2. [root@k8s2 hpa]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
    3. //当 CPU 利用率超过 50% 时,HPA 将根据当前 CPU 利用率来动态地扩展 Deployment 的副本数,使其保持在 110 之间
    4. [root@k8s2 hpa]# kubectl get hpa

    压测

    [root@k8s2 hpa]# kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
    

    pod负载上升

    触发hpa扩容pod

    结束压测后,默认等待5分钟冷却时间,pod会被自动回收

    多项量度指标

    [root@k8s2 hpa]# kubectl get hpa php-apache -o yaml > hpa-v2.yaml
    

    修改文件,增加内存指标

    1. - resource:
    2. name: memory
    3. target:
    4. averageValue: 50Mi
    5. type: AverageValue
    6. type: Resource

    1. [root@k8s2 hpa]# kubectl apply -f hpa-v2.yaml
    2. [root@k8s2 hpa]# kubectl get hpa

  • 相关阅读:
    java计算机毕业设计基于springboo+vue的学生活动组织管理系统
    时间复杂度O(40n*n)的C++算法:修改图中的边权
    【HCSD大咖直播】亲授大厂面试秘诀【云驻共创】
    第十八章:Swing自述
    MongoDB安装及集成
    Python如何解决点选验证码的登录识别(2)
    【PAT乙级】1015 德才论
    【Axure高保真原型】首字母筛选下拉列表
    servlet,service方法,post方法,get方法
    02 kubeadm部署k8s
  • 原文地址:https://blog.csdn.net/m0_64028800/article/details/134300230