k8s selector_k8s实现扩缩容

部署nginx服务

root@k8s-master1:~# cd /opt/yml/nginx/root@k8s-master1:/opt/yml/nginx# cat nginx.ymlapiVersion: apps/v1kind: Deploymentmetadata:  name: nginx-deployment  namespace: kevin  labels:    app: nginxspec:  replicas: 1  selector:    matchLabels:      app: nginx  template:    metadata:      labels:        app: nginx    spec:      containers:      - name: nginx        image: harbor.kevin.com/base/nginx:latest        ports:        - containerPort: 80        resources:          limits:            cpu: 2            memory: 1Gi          requests:            cpu: 200m            memory: 256Mi        ---kind: ServiceapiVersion: v1metadata:  labels:    app: k8s-nginx-service-label  name: k8s-nginx-service  namespace: kevinspec:  type: NodePort  ports:  - name: http    port: 80    protocol: TCP    targetPort: 80    nodePort: 30004  selector:    app: nginxroot@k8s-master1:/opt/yml/nginx# kubectl apply -f nginx.ymldeployment.apps/nginx-deployment createdservice/k8s-nginx-service createdroot@k8s-master1:/opt/yml/nginx# kubectl get pod -n kevinNAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-667b5d8947-ldn5x   1/1     Running   0          20s

手动调整pod数量

kubectl scale 对运行在k8s 环境中的pod 数量进行扩容(增加)或缩容(减小)。

扩容

oot@k8s-master1:~# kubectl scale deployment/nginx-deployment --replicas=2 -n kevinhaodeployment.apps/nginx-deployment scaledroot@k8s-master1:~# kubectl get pod -n kevinNAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-667b5d8947-cfb87   1/1     Running   0          4snginx-deployment-667b5d8947-ldn5x   1/1     Running   0          2m57sroot@k8s-master1:~# kubectl get deployment -n kevin  NAME               READY   UP-TO-DATE   AVAILABLE   AGEnginx-deployment   2/2     2            2           3m54s

缩容

root@k8s-master1:~# kubectl scale -n kevinhao deployment nginx-deployment --replicas=1root@k8s-master1:~# kubectl get pod -n kevin                                      NAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-667b5d8947-ldn5x   1/1     Running   0          6m55sroot@k8s-master1:~# kubectl get deployment -n kevin                                NAME               READY   UP-TO-DATE   AVAILABLE   AGEnginx-deployment   1/1     1            1           6m57s

HPA自动调整pod数量

早期的版本只能基于Heapster组件实现对CPU利用率做为触发条件,但是在k8s 1.11版本开始使用Metrices Server完成数据采集,然后将采集到的数据通过API(Aggregated API,汇总API),然后再把数据提供给HPA控制器进行查询,以实现基于某个资源利用率对pod进行扩缩容的目的。

准备metrics-server

root@k8s-master1:~# cd /usr/local/src/root@k8s-master1:/usr/local/src# git clone https://github.com/kubernetes-sigs/metrics-server.git

准备image

root@k8s-master1:/usr/local/src# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6root@k8s-master1:/usr/local/src# docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6 harbor.kevin.com/base/metrics-server-amd64:v0.3.6root@k8s-master1:/usr/local/src# docker push harbor.kevin.com/base/metrics-server-amd64:v0.3.6

修改yaml文件并部署metrics-server

root@k8s-master1:/usr/local/src# cd metrics-server/root@k8s-master1:/usr/local/src/metrics-server# git checkout v0.3.6HEAD is now at d1f4f6f bump metrics-server version to v0.3.6root@k8s-master1:/usr/local/src/metrics-server# cd deploy/1.8+/root@k8s-master1:/usr/local/src/metrics-server/deploy/1.8+# vim metrics-server-deployment.yaml---apiVersion: v1kind: ServiceAccountmetadata:  name: metrics-server  namespace: kube-system---apiVersion: apps/v1kind: Deploymentmetadata:  name: metrics-server  namespace: kube-system  labels:    k8s-app: metrics-serverspec:  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: harbor.kevin.com/base/metrics-server-amd64:v0.3.6        imagePullPolicy: Always        volumeMounts:        - name: tmp-dir          mountPath: /tmproot@k8s-master1:/usr/local/src/metrics-server/deploy/1.8+# kubectl apply -f .clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader createdclusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator createdrolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader createdapiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io createdserviceaccount/metrics-server createddeployment.apps/metrics-server createdservice/metrics-server createdclusterrole.rbac.authorization.k8s.io/system:metrics-server createdclusterrolebinding.rbac.authorization.k8s.io/system:metrics-server createdroot@k8s-master1:/usr/local/src/metrics-server/deploy/1.8+# cd

验证metrics-server pod

  • 验证metrics-server 是否采集到node数据

root@k8s-master1:~# kubectl top nodesNAME          CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%  172.16.1.30   172m         4%     1091Mi          33%      172.16.1.31   213m         5%     911Mi           28%      172.16.1.32   173m         4%     941Mi           29%      172.16.1.33   141m         3%     722Mi           22%      172.16.1.34   137m         3%     705Mi           21%      172.16.1.35   159m         4%     765Mi           23%
  • 验证metrics-server 是否采集到pod数据

root@k8s-master1:~# kubectl top pod -n kevinNAME                                     CPU(cores)   MEMORY(bytes)  kevin-nginx-deployment-7f6659c8f-pcgbf   0m           4M

修改controller-manager启动参数

root@k8s-master1:~# vim /etc/systemd/system/kube-controller-manager.service......# 添加如下--horizontal-pod-autoscaler-use-rest-clients=true \--horizontal-pod-autoscaler-sync-period=10s \......root@k8s-master1:~# systemctl daemon-reloadroot@k8s-master1:~# systemctl restart kube-controller-manager

yaml文件中定义扩缩容配置

  • 添加hpa.yml

root@k8s-master1:~# cd /opt/yml/nginx/root@k8s-master1:/opt/yml/nginx# vim hpa.ymlspec:  scaleTargetRef:    apiVersion: apps/v1    kind: Deployment    name: nginx-deployment  minReplicas: 2  maxReplicas: 5 spec:  scaleTargetRef:    apiVersion: apps/v1    kind: Deployment    name: nginx-deployment  minReplicas: 2  maxReplicas: 5  targetCPUUtilizationPercentage: 5root@k8s-master1:/opt/yml/nginx# kubectl apply -f .horizontalpodautoscaler.autoscaling/kevin-podautoscaler createddeployment.apps/nginx-deployment unchangedservice/k8s-nginx-service unchanged
  • 验证HPA

root@k8s-master1:/opt/yml/nginx# kubectl get pod -n kevinNAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-7949787b68-72wvx   1/1     Running   0          2m20snginx-deployment-7949787b68-qlbkv   1/1     Running   0          19mroot@k8s-master1:/opt/yml/nginx# kubectl get hpa -n kevinNAME                  REFERENCE                     TARGETS   MINPODS   MAXPODS   REPLICAS   AGEkevin-podautoscaler   Deployment/nginx-deployment   0%/5%     2         5         2          29m
  • 测试扩缩容

提高CPU使用率

root@k8s-master1:~# kubectl get pod -n kevinNAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-679468446d-b26cs   1/1     Running   0          2m38snginx-deployment-679468446d-t9dlk   1/1     Running   0          2m45sroot@nginx-deployment-679468446d-b26cs:/# grep "physical id" /proc/cpuinfo |wc -l4root@nginx-deployment-679468446d-b26cs:/# for i in `seq 1 4`;do dd if=/dev/zero of=/dev/null;done

再开一个终端查看Pod负载

root@k8s-master1:/opt/yml/nginx# kubectl top pod -n kevinNAME                                CPU(cores)   MEMORY(bytes)  nginx-deployment-679468446d-b26cs   735m         4Mi            nginx-deployment-679468446d-t9dlk   0m           3Mi            root@k8s-master1:/opt/yml/nginx# kubectl get hpa -n kevin                                                  NAME                  REFERENCE                     TARGETS   MINPODS   MAXPODS   REPLICAS   AGEkevin-podautoscaler   Deployment/nginx-deployment   183%/5%   2         5         5          32m

查看Pod

root@k8s-master1:~# kubectl get pod -n kevinNAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-679468446d-b26cs   1/1     Running   0          6m25snginx-deployment-679468446d-c2ns6   1/1     Running   0          103snginx-deployment-679468446d-cn9lq   1/1     Running   0          113snginx-deployment-679468446d-r9vbp   1/1     Running   0          113snginx-deployment-679468446d-t9dlk   1/1     Running   0          6m32s

已经扩容到5个Pod

停止提高CPU使用率一段时间后

root@k8s-master1:~# kubectl get pod -n kevinNAME                                READY   STATUS    RESTARTS   AGEnginx-deployment-679468446d-cn9lq   1/1     Running   1          8m37snginx-deployment-679468446d-t9dlk   1/1     Running   1          8m44s

23ff7d7f3e508c467c0f40000f2216f6.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值