Pod控制器
▶ Pod控制器介绍
Pod是k8s的最小管理单元,在k8s中,按照pod的创建方式可以将其分为两类:
• 自主式pod:
k8s直接创建的pod,这种pod删除后就没有了,也不会重建
• 控制器创建的pod:
k8s通过控制器创建的pod,这种pod删除了之后还会自动重建
▶ Pod控制器分类
• ReplicaSet:保证副本数量一直维持在期望值,并支持pod数量扩缩容,镜像版本升级(一次升级)
• Deployment:通过ReplicaSet来控制Pod,并支持滚动升级、回退版本
• Horizontal Pod Autoscaler(HPA):可以根据集群负载自动水平调整Pod的数量,实现削峰填谷
• StatefulSet:管理有状态应用。
有状态应用:应用自身需要保存数据。Mysql数据库
无状态应用:应用本身不存储数据,数据全部在外部存储。UI前端
▶ ReplicaSet(RS)
RS的主要作用就是保证一定数量的pod正常运行,它会持续监听这些Pod的运行状态,一旦pod发生故障,就会重启或重建。同时它还支持对pod数量的扩缩容和镜像版本的升降级。
• yaml文件:
apiVersion: apps/v1
kind: ReplicaSet
…………
spec:
replicas: 3 #副本数
selector:
matchLabels: #匹配标签
app: nginx-pod
template: #容器模板
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
#### • 命令操作
创建rs:
kubectl create -f pc-replicaset.yaml
查看rs:
kubectl -n demo get rs pc-replicaset -o wide
修改rs:
kubectl -n demo edit rs pc-replicaset
kubectl -n demo scale rs pc-replicaset --replicas=2
删除rs:
kubectl -n demo delete rs pc-replicaset
镜像升级:
kubectl -n demo set image rs pc-replicaset nginx=nginx:1.17.1
### ▶ Deployment(Deploy)
Deployment管理ReplicaSet,ReplicaSet管理Pod。Deployment比RS功能更加强大。
• Deploy主要功能
1.支持ReplicaSet的所有功能
2.支持发布的停止、继续
3.支持滚动升级和回滚版本
• 创建deployment
▪ yaml文件
apiVersion: apps/v1
kind: Deployment
………………
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
………………
### ▪ 命令操作
1.创建deploy:
kubectl create -f deployment.yaml
kubectl -n demo create deploy nginx --image=nginx:1.17.1 --replicas:3
2.查看deploy:
kubectl -n demo get deploy
kubectl -n demo get rs
3.查看pod:
kubectl -n demo get pods
#### • 扩缩容
变更副本数量:
kubectl -n demo scale deploy mydeploy --replicas=5
kubectl -n demo edit deploy mydeploy
#### • 镜像更新(滚动升级)
### ▪ 重建更新:
yaml文件:
spec:
strategy:
type:Recreate
### ▪ 滚动更新:
yaml文件:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25% #可以增加多少pod
maxUnavailable: 25% #不可用pod的最大数量
### ▪ 命令更新镜像:
kubectl -n demo set image deploy mydeploy nginx=nginx:1.17.2
### ▪ 观察升级过程:
kubectl -n demo get pods -w
#### • 版本回退
### ▪ kubectl rollout:版本升级相关功能,支持下面的选项:
status:显示当前升级状态
history:显示升级历史记录
pause:暂停版本升级过程
resume:继续已经暂停的版本升级过程
restart:重启版本升级过程
undo:回滚到上一版本(可用--to-revision回滚到指定版本)
### ▪ 具体操作命令:
1.查看当前升级版本的状态:
kubectl -n demo rollout status deploy mydeploy
2.查看升级历史记录:
kubectl -n demo rollout history deploy mydeploy
显示内容:
REVISION CHANGE-CAUSE
1 详细版本说明
2 详细版本说明
3.回滚上一个版本:
kubectl -n demo rollout undo deploy mydeploy
4.指定回滚版本:
kubectl -n demo rollout --to-revision=1
#回滚到了1版本
▪ 金丝雀发布:
1.更新版本:
kubectl -n demo set image deploy mydeploy nginx=nginx:1.17.2
2.暂停更新:
kubectl -n demo rollout pause deploy mydeploy
3.观察更新状态:
kubectl -n demo rollout status deploy mydeploy
4.确保更新的pod没问题了,继续更新:
kubectl -n demo rollout resume deploy mydeploy
5.查看最后的更新情况:
kubectl -n demo get rs -o wide
4.确保更新的pod没问题了,继续更新
第二种方式(灰度发布):
新建一个deployment,name一样,版本更新,效果为三个旧版本一个新版本
前者如果部分新版本有问题,必须resume全部滚动更新完成,才可以undo回退版本,这种方式只需要增减deploy就可以了
▶ HPA
水平Pod自动弹性伸缩,可以获取每个Pod利用率,然后和HPA中定义的指标进行对比,实现Pod数量的自动调整。
• 安装metrics-server(检测指标)
▪ cs-server0.6.2版本部署yaml文件如下:
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
*
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
name: system:aggregated-metrics-reader
rules:
apiGroups:
metrics.k8s.io
resources:
pods
nodes
verbs:
get
list
watch
*
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
rules:
apiGroups:
""
resources:
nodes/metrics
verbs:
get
apiGroups:
""
resources:
pods
nodes
verbs:
get
list
watch
*
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
kind: ServiceAccount
name: metrics-server
namespace: kube-system
*
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
kind: ServiceAccount
name: metrics-server
namespace: kube-system
*
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
kind: ServiceAccount
name: metrics-server
namespace: kube-system
*
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
ports:
name: https
port: 443
protocol: TCP
targetPort: https
selector:
k8s-app: metrics-server
*
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
- --kubelet-use-node-status-port
- --metric-resolution=15s
- --kubelet-use-node-status-port
- --kubelet-insecure-tls
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.2
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 4443
name: https
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /readyz
port: https
scheme: HTTPS
initialDelaySeconds: 20
periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 200Mi
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-dir
nodeSelector:
kubernetes.io/os: linux
priorityClassName: system-cluster-critical
hostNetwork: true
serviceAccountName: metrics-server
volumes:
- emptyDir: {}
name: tmp-dir
*
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
k8s-app: metrics-server
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: metrics-server
namespace: kube-system
version: v1beta1
versionPriority: 100
### ▪ 安装:kubectl apply -f xx.yaml
### ▪ 查看pod运行状况:
kubectl -n kube-system get pods
### ▪ top查看资源使用情况
kubectl top node
#### • 部署HPA
### ▪ yaml文件
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: pc-hpa
namespace: demo
spec:
minReplicas: 1 #最少1个
maxReplicas: 10 #最多10个
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 3
---#配置一个deployment
---#配置一个service
### ▪ 创建/查看
kubectl create -f xx.yaml
kubectl -n dmeo get hpa
#### • KPA VPA(非默认自带)
### ▶ DaemonSet(DS)
DS保证在集群中的每一台节点上都运行一个副本。适用于日志收集、节点监控等场景。
• yaml文件
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: xxx
namespace: xxx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:.1.17.1
#### • 创建:kubectl create -f xxx.yaml
#### • 查看:kubectl get -n demo get ds -o wide
### ▶ Job(一次性执行任务)
### ▶ CronJob(重复性执行任务)
### ▶ deployment和statefulset的区别
特性| Deployment| StatefulSet
---|---|---
主要用途| 部署无状态应用| 部署有状态应用
Pod标识| 随机唯一标识(随机后缀)| 稳定、可预测的标识(顺序编号)
网络标识| 共享Service,通过负载均衡访问| 每个Pod有独立的DNS名称:<pod-name>.<service-name>.<namespace>.svc.cluster.local
存储| 共享存储卷(所有Pod相同)| 独立持久化存储卷(每个Pod不同)
创建顺序| 并行创建| 按顺序创建(0, 1, 2...)
删除顺序| 并行删除| 逆序删除(..., 2, 1, 0)