{"id":1462,"date":"2022-04-12T16:22:53","date_gmt":"2022-04-12T08:22:53","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=1462"},"modified":"2022-04-25T17:09:46","modified_gmt":"2022-04-25T09:09:46","slug":"11-k8s%e5%9f%ba%e4%ba%8emetrics-server%e7%9b%91%e6%8e%a7%e7%9a%84%e5%bc%b9%e6%80%a7%e4%bc%b8%e7%bc%a9","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=1462","title":{"rendered":"11.k8s\u57fa\u4e8emetrics-server\u76d1\u63a7\u7684\u5f39\u6027\u4f38\u7f29"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.metrics-server\u6982\u8ff0<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Metrics Server \u662f Kubernetes \u5185\u7f6e\u81ea\u52a8\u7f29\u653e\u7ba1\u9053\u7684\u5bb9\u5668\u8d44\u6e90\u6307\u6807\u7684\u53ef\u6269\u5c55\u3001\u9ad8\u6548\u6e90\u3002\r\nMetrics Server \u4ece Kubelets \u6536\u96c6\u8d44\u6e90\u6307\u6807\uff0c\u5e76\u901a\u8fc7 Metrics API \u5728 Kubernetes apiserver \u4e2d\u516c\u5f00\u5b83\u4eec\uff0c\u4f9b Horizontal Pod Autoscaler \u548c Vertical Pod Autoscaler \u4f7f\u7528\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.\u5b89\u88c5metrics-server\u76d1\u63a7<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5efa\u8bae\u9605\u8bfb\uff1a\r\nhttps:&#47;&#47;github.com\/kubernetes-sigs\/metrics-server\r\n\u56e0\u4e3a\u6211\u7684k8s\u96c6\u7fa4\u662f1.15\u7248\u672c\u7684\u6240\u4ee5\u5b89\u88c50.3\u7248\u672c\u7684\u76d1\u63a7\r\n\u7f16\u8f91yaml\u6587\u4ef6\r\n&#91;root@k8s-master metric]# cat aggregated-metrics-reader.yaml \r\nkind: ClusterRole\r\napiVersion: rbac.authorization.k8s.io\/v1\r\nmetadata:\r\n  name: system:aggregated-metrics-reader\r\n  labels:\r\n    rbac.authorization.k8s.io\/aggregate-to-view: \"true\"\r\n    rbac.authorization.k8s.io\/aggregate-to-edit: \"true\"\r\n    rbac.authorization.k8s.io\/aggregate-to-admin: \"true\"\r\nrules:\r\n- apiGroups: &#91;\"metrics.k8s.io\"]\r\n  resources: &#91;\"pods\"]\r\n  verbs: &#91;\"get\", \"list\", \"watch\"]\r\n&#91;root@k8s-master metric]# cat auth-reader.yaml \r\n---\r\napiVersion: rbac.authorization.k8s.io\/v1beta1\r\nkind: RoleBinding\r\nmetadata:\r\n  name: metrics-server-auth-reader\r\n  namespace: kube-system\r\nroleRef:\r\n  apiGroup: rbac.authorization.k8s.io\r\n  kind: Role\r\n  name: extension-apiserver-authentication-reader\r\nsubjects:\r\n- kind: ServiceAccount\r\n  name: metrics-server\r\n  namespace: kube-system\r\n&#91;root@k8s-master metric]# cat metrics-server-deployment.yaml \r\n---\r\napiVersion: v1\r\nkind: ServiceAccount\r\nmetadata:\r\n  name: metrics-server\r\n  namespace: kube-system\r\n---\r\napiVersion: extensions\/v1beta1\r\nkind: Deployment\r\nmetadata:\r\n  name: metrics-server\r\n  namespace: kube-system\r\n  labels:\r\n    k8s-app: metrics-server\r\nspec:\r\n  selector:\r\n    matchLabels:\r\n      k8s-app: metrics-server\r\n  template:\r\n    metadata:\r\n      name: metrics-server\r\n      labels:\r\n        k8s-app: metrics-server\r\n    spec:\r\n      serviceAccountName: metrics-server\r\n      volumes:\r\n      # mount in tmp so we can safely use from-scratch images and\/or read-only containers\r\n      - name: tmp-dir\r\n        emptyDir: {}\r\n      containers:\r\n      - name: metrics-server\r\n        image: registry.aliyuncs.com\/google_containers\/metrics-server-amd64:v0.3.3\r\n        imagePullPolicy: Always\r\n        volumeMounts:\r\n        - name: tmp-dir\r\n          mountPath: \/tmp\r\n        command:\r\n        - \/metrics-server\r\n        - --metric-resolution=30s\r\n        - --kubelet-preferred-address-types=InternalIP\r\n        - --kubelet-insecure-tls\r\n&#91;root@k8s-master metric]# cat resource-reader.yaml \r\n---\r\napiVersion: rbac.authorization.k8s.io\/v1\r\nkind: ClusterRole\r\nmetadata:\r\n  name: system:metrics-server\r\nrules:\r\n- apiGroups:\r\n  - \"\"\r\n  resources:\r\n  - pods\r\n  - nodes\r\n  - nodes\/stats\r\n  verbs:\r\n  - get\r\n  - list\r\n  - watch\r\n---\r\napiVersion: rbac.authorization.k8s.io\/v1\r\nkind: ClusterRoleBinding\r\nmetadata:\r\n  name: system:metrics-server\r\nroleRef:\r\n  apiGroup: rbac.authorization.k8s.io\r\n  kind: ClusterRole\r\n  name: system:metrics-server\r\nsubjects:\r\n- kind: ServiceAccount\r\n  name: metrics-server\r\n  namespace: kube-system\r\n&#91;root@k8s-master metric]# cat auth-delegator.yaml \r\n---\r\napiVersion: rbac.authorization.k8s.io\/v1beta1\r\nkind: ClusterRoleBinding\r\nmetadata:\r\n  name: metrics-server:system:auth-delegator\r\nroleRef:\r\n  apiGroup: rbac.authorization.k8s.io\r\n  kind: ClusterRole\r\n  name: system:auth-delegator\r\nsubjects:\r\n- kind: ServiceAccount\r\n  name: metrics-server\r\n  namespace: kube-system\r\n&#91;root@k8s-master metric]# cat metrics-apiservice.yaml \r\n---\r\napiVersion: apiregistration.k8s.io\/v1beta1\r\nkind: APIService\r\nmetadata:\r\n  name: v1beta1.metrics.k8s.io\r\nspec:\r\n  service:\r\n    name: metrics-server\r\n    namespace: kube-system\r\n  group: metrics.k8s.io\r\n  version: v1beta1\r\n  insecureSkipTLSVerify: true\r\n  groupPriorityMinimum: 100\r\n  versionPriority: 100\r\n&#91;root@k8s-master metric]# cat metrics-server-service.yaml \r\n---\r\napiVersion: v1\r\nkind: Service\r\nmetadata:\r\n  name: metrics-server\r\n  namespace: kube-system\r\n  labels:\r\n    kubernetes.io\/name: \"Metrics-server\"\r\n    kubernetes.io\/cluster-service: \"true\"\r\nspec:\r\n  selector:\r\n    k8s-app: metrics-server\r\n  ports:\r\n  - port: 443\r\n    protocol: TCP\r\n    targetPort: 443\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">3.\u9a8c\u8bc1\u5f39\u6027\u4f38\u7f29<\/h2>\n\n\n\n<p><strong><em>1.\u521b\u5efadeployment\u6587\u4ef6<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@k8s-master hpa]# cat deploy-wordpress2.yaml \r\napiVersion: extensions\/v1beta1\r\nkind: Deployment\r\nmetadata:\r\n  name: mysql\r\n  namespace: kube-system\r\nspec:\r\n  replicas: 1\r\n  template:\r\n    metadata:\r\n      labels:\r\n        app: mysql\r\n    spec:\r\n      containers:\r\n      - name: mysql\r\n        image: k8s-master:5000\/mysql:5.7\r\n        ports:\r\n        - containerPort: 3306\r\n        env:\r\n        - name: MYSQL_ROOT_PASSWORD\r\n          value: somewordpress\r\n        - name: MYSQL_DATABASE\r\n          value: wordpress\r\n        - name: MYSQL_USER\r\n          value: wordpress\r\n        - name: MYSQL_PASSWORD\r\n          value: wordpress\r\n---\r\n\r\napiVersion: v1\r\nkind: Service   #\u7b80\u79f0svc\r\nmetadata:\r\n  name: musql-svc\r\n  namespace: kube-system\r\nspec:\r\n  clusterIP: 10.254.86.101\r\n  type: ClusterIP\r\n  ports:\r\n    - port: 3306\r\n      targetPort: 3306    #pod port\r\n  selector:\r\n    app: mysql\r\n\r\n---\r\napiVersion: extensions\/v1beta1\r\nkind: Deployment\r\nmetadata:\r\n  name: wordpress\r\nspec:\r\n  replicas: 1\r\n  template:\r\n    metadata:\r\n      labels:\r\n        app: wordpress\r\n    spec:\r\n      containers:\r\n      - name: wordpress\r\n        image: k8s-master:5000\/wordpress:latest\r\n        ports:\r\n        - containerPort: 80\r\n        resources:\r\n          limits:\r\n            cpu: 100m\r\n          requests:\r\n            cpu: 100m\r\n        env:\r\n        - name: WORDPRESS_DB_HOST\r\n          value: musql-svc.kube-system.svc.cluster.local\r\n        - name: WORDPRESS_DB_USER\r\n          value: wordpress\r\n        - name: WORDPRESS_DB_PASSWORD\r\n          value: wordpress\r\n---\r\n\r\napiVersion: v1\r\nkind: Service   #\u7b80\u79f0svc\r\nmetadata:\r\n  name: wordpress-svc\r\nspec: \r\n  type: NodePort\r\n  ports:\r\n    - port: 80\r\n      nodePort: 31001\r\n      targetPort: 80   #pod port\r\n  selector:\r\n    app: wordpress\r\n&#91;root@k8s-master hpa]# kubectl apply -f deploy-wordpress2.yaml\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.\u521b\u5efa\u5f39\u6027\u4f38\u7f29\u89c4\u5219<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>###\u57fa\u4e8eyaml\u6587\u4ef6\u521b\u5efa\r\n&#91;root@k8s-master hpa]# cat hpa.yaml \r\napiVersion: autoscaling\/v1\r\nkind: HorizontalPodAutoscaler\r\nmetadata:\r\n  name: wordpress\r\n  namespace: default\r\nspec:\r\n  maxReplicas: 8 ###\u6700\u5927pod\u6570\r\n  minReplicas: 1 ###\u6700\u5c0fpod\u6570\r\n  scaleTargetRef: ###\u6269\u5bb9\u76f8\u5173\u9009\u62e9\u5668\r\n    apiVersion: extensions\/v1beta1 ###\u7248\u672c\u53f7\r\n    kind: Deployment  ###\u9700\u8981\u6269\u5bb9\u7684\u8d44\u6e90\u7c7b\u578b\r\n    name: wordpress ###\u8d44\u6e90\u7684\u540d\u5b57\r\n  targetCPUUtilizationPercentage: 5 ###cpu\u7684\u767e\u5206\u6bd4\uff0c\u8d85\u8fc7\u540e\u89e6\u53d1\u6269\u5bb9\r\n&#91;root@k8s-master hpa]# kubectl apply -f hpa.yaml\r\n##\u57fa\u4e8e\u547d\u4ee4\u884c\u521b\u5efa\r\nkubectl autoscale deployment wordpress --max=5 --min=2 --cpu-percent=5\r\n--max:\u6307\u5b9a\u6700\u5927\u7684Pod\u6570\u91cf\uff0c\u5982\u679c\u6307\u5b9a\u7684\u6570\u91cf\u8d8a\u5927\uff0c\u5219\u5f39\u6027\u4f38\u7f29\u7684\u8d44\u6e90\u521b\u5efa\u7684\u5c31\u8d8a\u591a\uff0c\u5bf9\u670d\u52a1\u5668\u8d44\u6e90\u4f1a\u8fdb\u884c\u6d88\u8017\u3002\r\n--minx:\u6307\u5b9a\u6700\u5c0f\u7684Pod\u6570\u91cf\u3002\r\n--cpu-percent:\u6307\u5b9aCPU\u7684\u767e\u5206\u6bd4\r<\/code><\/pre>\n\n\n\n<p><strong><em>3.\u4f7f\u7528ab\u5de5\u5177\u8fdb\u884c\u538b\u6d4b<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>##\u5b89\u88c5ab\u5de5\u5177\r\n&#91;root@k8s-master hpa]# yum -y install httpd-tools.x86_64\r\nab -n 1000000 -c 40  http:\/\/k8s.com:30001\r\n\u589e\u52a0\u5230\u4e868\u4e2a\r<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"420\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/04\/image-94.png\" alt=\"\" class=\"wp-image-1465\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/04\/image-94.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/04\/image-94-300x146.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/04\/image-94-768x373.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1462"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1462"}],"version-history":[{"count":2,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1462\/revisions"}],"predecessor-version":[{"id":1466,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1462\/revisions\/1466"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}