/ kubernetes

k8s基于hpa实现pod弹性扩容

要使用hpa,第一步是安装heapster(下面的10.135.19.77换成自己k8s可以访问到的ip)

wget https://github.com/kubernetes/heapster/archive/master.zip
unzip master.zip
cd heapster-master/

sed -i "s/gcr.io\/google_containers\/heapster-grafana-amd64:v4.4.3/index.tenxcloud.com\/jimmy\/heapster-grafana-amd64:v4.0.2/g" deploy/kube-config/influxdb/grafana.yaml

sed -i "s/gcr.io\/google_containers\/heapster-amd64:v1.4.0/index.tenxcloud.com\/jimmy\/heapster-amd64:v1.3.0-beta.1/g" deploy/kube-config/influxdb/heapster.yaml

sed -i "s/https:\/\/kubernetes.default/http:\/\/10.135.19.77:8080\?inClusterConfig=false\&useServiceAccount=false/g" deploy/kube-config/influxdb/heapster.yaml

sed -i "s/monitoring-influxdb.kube-system.svc/10.135.19.77/g" deploy/kube-config/influxdb/heapster.yaml

sed -i "s/gcr.io\/google_containers\/heapster-influxdb-amd64:v1.3.3/index.tenxcloud.com\/jimmy\/heapster-influxdb-amd64:v1.1.1/g" deploy/kube-config/influxdb/influxdb.yaml


/bin/bash deploy/kube.sh start

编写测试例子

  • 编写文件a.yaml
apiVersion: extensions/v1beta1  
kind: Deployment  
metadata:  
  name: my-app
spec:  
  replicas: 2
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: registry.alauda.cn/yubang/paas_base_test
        ports:
        - containerPort: 80
        command: ["/bin/bash", "/var/start.sh"] 
        resources:  
          limits:  
            cpu: 0.01  
            memory: 64Mi
  • 编写文件b.yaml
apiVersion: v1  
kind: Service  
metadata:  
  name: my-app-svc
  labels:
    app: my-app
spec:  
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30964
  type: NodePort
  selector:
    app: my-app
  • 编写文件c.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: my-app
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 5

启动服务

kubectl apply -f a.yaml --validate  
kubectl apply -f b.yaml --validate  
kubectl apply -f c.yaml --validate

查看是否正常运行

kubectl get horizontalpodautoscaler