Pod控制器
Posted l-dongf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pod控制器相关的知识,希望对你有一定的参考价值。
一、Pod控制器简介
- ReplicationController(目前已废弃)
- ReplicaSet(不建议直接使用):用户期望的Pod副本数,标签选择器以便选择自己管理控制的副本,Pod资源模板来完成Pod的新建,主要管理无状态的Pod资源副本
- Deployment:通过控制ReplicaSet之上来工作,支持滚动更新,回滚,基于声明式创建,在Pod运行中可动态修改Pod参数
- DaemonSet:以守护进程的方式来控制Pod,在集群中每个节点运行一个Pod副本
- Job:执行一次性的作业Pod
- Cronjob:执行周期性的作业Pod
- StatefulSet:管理有状态Pod,每个Pod都要有可能是有状态、数据的
二、ReplicaSet
- 获取帮助
# kubectl explain ReplicaSet
# kubectl explain ReplicaSet.spec
# kubectl explain ReplicaSet.spec.template
# kubectl explain ReplicaSet.spec.template.spec
- 简单定义
manifests]# vim rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
name: myapp-pod
labels:
app: myapp
release: canary
environment: qa
spec:
containers:
- name: myapp-container
image: dongfeimg/myapp:v1
ports:
- name: http
containerPort: 80
# kubectl get rs
- 手动扩容/缩容
# kubectl edit rs myapp
spec:
replicas: 5 #可动态修改Pod副本
- 手动升级image
# kubectl edit rs myapp
spec:
containers:
- image: dongfeimg/myapp:v2
# kubectl get rs -o wide
注意:只有重建的Pod才是新版本的image
三、Deployment
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
image: dongfeimg/myapp:v1
ports:
- name: http
containerPort: 80
# kubectl apply -f Deployment-damo.yaml
# kubectl get deploy
# kubectl get rs
# kubectl get pods
# kubectl describe deploy myapp-deploy
- 查看历史记录
# kubectl rollout history deployment myapp-deploy
- 监控查看pod状态
# kubectl get pods -l app=myapp -w
- 通过打补丁的方式扩容
# kubectl patch deployment myapp-deploy -p '{"spec":{"relicas":5}}'
- 滚动更新指定超出一个pod
# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
- 更新image版本(金丝雀发布)
# kubectl set image deployment myapp-deploy myapp=dongfeimg/myapp:v2 && kubectl rollout pause deployment myapp-deploy #丢出一只金丝雀
# kubectl rollout status deployment myapp-deploy
# kubectl get pods -l app=myapp -w
# kubectl rollout resume deployment myapp-deploy #所有的都开始更新
- 回滚
# kubectl rollout history deployment myapp-deploy #查看历史版本
# kubectl rollout undo deployment myapp-deploy --to-revision=1 #回滚到第一版
四、DaemonSet
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
image: dongfeimg/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info
filebeat + redis
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-alpine
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
image: dongfeimg/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info
- 更新image版本
# kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.6.6-alpine
以上是关于Pod控制器的主要内容,如果未能解决你的问题,请参考以下文章