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 hpa