kubernetes 水平伸縮及yaml格式編寫

Replication Controller:用來部署、升級Pod
Replica Set:下一代的Replication Controller
Deployment:能夠更加方便的管理Pod和Replica Set
$ mkdir rc-demo
$ vim rc-demo.yaml
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-demo
  labels:
    app: rc
spec:
  replicas: 3
#  selecter:
#    app: rc
  template:
    metadata:
      labels:
        app: rc
    spec:
      containers:
      - name: nginx-demo
        image: nginx
        ports:
        - containerPort: 80
$ kubectl edit rc rc-demo
在裏面能夠修改副本集數量  

滾動更新:
kubectl rolling-update rc-demo --image=nginx:1.7.9      ##(更新到1.7.9版本)

~~~~~RC不是實現一鍵回滾~~~~~

Deployment:

Deployment主要職責和RC同樣都是保證Pod的數量和健康

RC的所有功能:Deployment具有上面描述的RC的所有功能
事件和狀態查看:能夠查看Deployment的升級詳細進度和狀態
回滾:當升級Pod的時候若是出現問題,能夠使用回滾操做回滾到以前的任一版本
版本記錄:每一次對Deployment的操做,都可以保存下來,這也是保證能夠回滾到任一版本的基礎
暫停和啓動:對於每一次升級都可以隨時暫停和啓動



---
apiVersion: apps/v1beta1
kind: Deployment
metedata:
  name: nginx-deploy
  labels:
    app: nginx-demo
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
template就至關於pod 只是省略apiVersion kind name
不指定name,自動生成name,避免重複

$ vim deploy-demo.yaml
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:    
    app: nginx-demo
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80  

滾動升級設置:
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:    
    app: nginx-demo
spec:
  replicas: 3
  revisionHistoryLimit: 15
  minReadySeconds: 5
  stragety:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

查詢滾動更新狀態:
kubectl rollout status deploy nginx-deploy
kubectl rollout pause deploy nginx-deploy  ##暫停滾動更新
kubectl rollout resume deploy nginx-deploy ##恢復滾動更新
kubectl rollout history deploy nginx-deploy #查看回滾歷史版本
kubectl apply -f deploy-demo.yaml --record=true
rollout 歷史版本跟rs有關,rs刪除某一版本,rollout 歷史版本中也會刪除掉同一版本
kubectl rollout ando deploy nginx-deploy  ##回退上一版本最近的
 
kubectl rollout ando deploy nginx-deploy --to-revision=3 ##回退到指定版本


一.Pod水平自動伸縮(horizontal-pod-autoscaler)
1. kubectl autoscale命令來建立一個HPA資源對象
2. 可經過kube-controller-manager的標誌--horizontal-pod-autoscaler-sync-period進行設置(輪詢設置默認是30s)
3. HPA能夠從heapster或者用戶自定義的restclient端獲取每個的利用率
4.
    Heapster:僅支持CPU使用率
    自定義監控:咱們到後面的監控的課程中再給你們講解這部分的使用方法

安裝 heapster:須要influxdb和grafana支持
1.https://github.com/kubernetes-retired/heapster/blob/v1.5.4/deploy/kube-config/influxdb/influxdb.yaml
 https://github.com/kubernetes-retired/heapster/blob/v1.5.4/deploy/kube-config/influxdb/heapster.yaml
 https://github.com/kubernetes-retired/heapster/blob/v1.5.4/deploy/kube-config/influxdb/grafana.yaml

2.查看heapster日誌:
 kubectl logs -f heapster-5d4bf58946-hwqbz -n kube-system
### E0520 09:58:35.150249       1 reflector.go:190] k8s.io/heapster/metrics/util/util.go:30: Failed to list *v1.Node: nodes is forbidden: User "system:serviceaccount:kube-system:heapster" cannot list resource "nodes" in API group "" at the cluster scope

這個意思是heapster用戶沒有權限須要添加serviceaccount相對應的admin最高權限ClusterRole
集羣規則綁定:ClusterRoleBinding


在heapster.yaml中的serviceaccount下面添加

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: heapster-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: heapster
  namespace: kube-system
 
 
 
 
 
Hpa:
$ vim hap-demo.yaml
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: hpa-demo
  labels:
    app: hpa
spec:
  replicas: 1
  revisionHistoryLimit: 15
  minReadySeconds: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
       maxSurge: 1
       maxUnavailable: 1
  template:
    metadata:
      labels:
        app: hpa
    spec:
      containers:
      - name: nginx
        image: nginx:1.9.7
        resources:
          requests:
            cpu: 100m
#          limit:
#            cpu: 200m
#            memory:
        ports:
        - containerPort: 80  
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-demo
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: hpa-demo
  targetCPUUtilizationPercentage: 5
 
$ kubectl autoscale deploy hpa-demo --min=1 --max=10 --cpu-percent=5

$ kubectl get hpanode

相關文章
相關標籤/搜索