一、Pod控制器的类别
-
ReplicationController:早期唯一的控制器,已废弃
-
ReplicaSet:控制Pod满足用户期望副本;标签选择器选择由自己管理的Pod副本;Pod资源模板完成Pod资源的新建。主要用于管理无状态的Pod,不建议直接使用此控制器
-
Deployment:基于ReplicaSet控制器;建议使用此控制器,支持滚动更新和回滚,管理无状态应用
-
DaemonSet:守护进程类的控制器,例如每台node都启动一个Pod一直持续下去
-
Job:执行一次性的任务,完成后就退出,如果没有完成则重新构建一个Pod运行
-
Cronjob:周期性运行的任务
-
StatefulSet:管理有状态的应用,每个Pod都是单独管理
二、ReplicaSet控制器
- kubectl explain rs:获取帮助手册
# cat rs-damo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata: #ReplicaSet的metadata
name: myapp-rs
namespace: default
spec: #ReplicaSet的spec
replicas: 2
selector:
matchLabels: #标签选择器,用于控制器选择控制符合条件的pod
app: myapp
release: canary
template:
metadata:
name: myapp-pod #此处pod名会被控制器的名字代替
labels: #此处为Pod的标签,必须符合控制器的标签选择器定义的条件
app: myapp
release: canary
spec:
containers:
- name: myapp-container
image: dongfeimg/myapp:v1
ports:
- name: http
containerPort: 80
# kubectl get rs
# kubectl get pods
- 扩容/缩容
# kubectl edit rs myapp-rs
spec:
replicas: 10 #将此值改
- 升级/回滚
# kubectl edit rs myapp-rs
spec:
containers:
- image: dongfeimg/myapp:v2 #修改此值不能立即更新,原有pod删除后重建会升级到v2版本
三、Deployment控制器
- kubectl explain deploy
# cat deploy-damo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp-container
image: dongfeimg/myapp:v1
ports:
- name: http
containerPort: 80
# kubectl apply -f deploy-damo.yaml #声明式创建
# kubectl get deploy #deploy控制rs
# kubectl get rs #rs控制pod
# kubectl get pods
- 使用修改清单文件的方式扩容、缩容、更新和回滚等操作
# vim deploy-damo.yaml
spec:
replicas: 3 #将副本扩容至3个
# kubectl apply -f deploy-damo.yaml
# vim deploy-damo.yaml
spec:
containers:
- name: myapp-container
image: dongfeimg/myapp:v2 #将版本更新为v2
# kubectl apply -f deploy-damo.yaml
# kubectl rollout history deployment myapp-deploy #查看升级的历史信息
- 使用kubectl patch的方式扩容、缩容、更新和回滚等操作
# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}' #传入json格式的数组来实现扩容
# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}' #"maxSurge":1 最大在更新pod数,"maxUnavailable":0 允许最大不可用的pod
# kubectl describe deployment myapp-deploy |grep RollingUpdateStrategy #验证
- kubectl set image和kubectl rollout pause
# kubectl set image deployment myapp-deploy myapp-container=dongfeimg/myapp:v3 #将容器版本更新至v3
&& kubectl rollout pause deployment myapp-deploy #在更新第一个的时候暂停,金丝雀发布
# kubectl rollout resume deployment myapp-deploy #继续此前的更新
- kubectl rollout undo:回滚
# kubectl rollout history deployment myapp-deploy
# kubectl rollout undo deployment myapp-deploy --to-revision=1 #回滚至v1
四、DaemonSet控制器
- 在集群中符合选择器的node上一个pod,例如在每个节点上运行一个filebeat来收集日志
- kubectl explain ds
# cat ds-damo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: redis
role: logstor
template:
metadata:
labels:
app: redis
role: logstor
spec:
containers:
- name: redis
image: redis:4.0
ports:
- name: redis
containerPort: 6379
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
template:
metadata:
labels:
app: filebeat
release: stable
spec:
containers:
- name: filebeat-container
image: dongfeimg/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local #redis-server的主机名(svc)
- name: REDIS_LOG_LEVEL
value: info
# kubectl expose deployment redis --port=6379