1.生产中,很少会跑一个自主式pod,一般由控制器去创建pod,其配置文件中内嵌了pod的创建方式.
pod控制器:ReplicaSet、Deployment、DaemonSet、Job、Cronjob、StatefulSet
ReplicaSet:代用户创建指定数量的pod副本数量,确保pod副本数量符合预期状态,并且支持滚动式自动扩容和缩容功能.
ReplicaSet主要由三个组件组成:
a.用户期望的pod副本数量;
b.标签选择器,判断哪个pod归自己管理;
c.pod资源模板,当现存的pod数量不足,会根据pod资源模板进行新建.
帮助用户管理无状态的pod资源,精确反应用户定义的目标数量,但RelicaSet不是直接使用的控制器,而是使用Deployment;
Deployment:工作在ReplicaSet之上,用于管理无状态应用,目前来说最好的控制器.支持滚动更新和回滚功能,还提供声明式配置;
DaemonSet:用于确保集群中的每一个节点只运行特定的pod副本,通常用于实现系统级后台任务,比如ELK中负责收集日志filebeat,特性:服务是无状态的,服务必须是守护进程;
Job:只要完成就立即退出,不需要重启或重建;
Cronjob:周期性任务控制,不需要持续后台运行;
StatefulSet:管理有状态应用.
2.ReplicaSet(rs)示例
cat rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels:
run: myapp
release: canary
template:
metadata:
name: whatever
labels:
run: myapp
release: canary
env: test
spec:
containers:
- name: nginx-web
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
template:资源模板中定义的name其实不生效,pod运行起来之后,真正的名字是控制器名+随机字符串
kubectl create -f rs-demo.yaml
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
rs-myapp-2hxc9 1/1 Running 0 74s 10.244.2.7 k8s-node2
rs-myapp-d6845 1/1 Running 0 74s 10.244.1.9 k8s-node1
curl 10.244.2.7
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
# 编辑replicatset的配置文件,这个文件不是我们手工创建的,而是apiserver维护的,修改副本数
kubectl edit rs myapp
# 也可以升级版本,将v1改成v2,但只有pod重建后,比如增加或删除Pod,才会更新成v2版本
3.Deployment控制器
图解:通过Deployment控制器来动态更新pod版本,Deployment下有众多replicatset,但只有一个是激活的,更改配置文件中的镜像版本,就会一个一个的删除replicatset v1版本中的Pod,自动新创建的pod就会变成v2版本,当pod全部变成v2版本后,replicatset v1不会被删除,这样一旦发现v2版本有问题,还可以回退到v1版本,通常deployment默认保留10个版本的replicatset.
kubectl explain deploy # 文档是落后于k8s版本的
deploy示例:
cat deploy-demo.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
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
# apply:声明式更新和创建,可以应用多次,create只能用一次
kubectl apply -f deploy-demo.yaml
kubectl get deploy # 会看见一个名为myapp-deploy的deploy生成
kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp-deploy-574965d786 2 2 2 93s
kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-deploy-574965d786-5x42g 1/1 Running 0 70s
myapp-deploy-574965d786-dqzpd 1/1 Running 0 70s # 默认滚动策略是RollingUpdate,查看滚动更新的历史
kubectl rollout history deployment myapp-deploy # 如果要修改副本数,则编辑deploy-demo.yaml修改副本数,或者:
kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
# 给更新策略打补丁:
kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
# maxSurge:pod的数量最多可超出期望值多少个;maxUnavailable:最多不可用的pod有多少个. # 金丝雀发布,先发布一个,此时多了一个pod,现在有6个
# 用set image命令将镜像myapp升级为v3版本,并且将myapp-deploy控制器标记为暂停,
# 被暂停的资源不会被控制器使用,可以使"kubectl rollout resume"命令恢复已暂停资源
kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3
&& kubectl rollout pause deployment myapp-deploy
kubectl get pods -l app=myapp -w
# resume:继续,重新开始,可以看到继续更新,删一个更新一个
kubectl rollout status deployment myapp-deploy
kubectl rollout resume deployment myapp-deploy
# 查看副本集的详细信息
kubectl get rs -o wide
# 版本回滚
kubectl rollout history deployment myapp-deploy
kubectl rollout undo deployment myapp-deploy --to-revision=1
4.DaemonSet示例
# node1、node2下载filebeat镜像
docker pull ikubernetes/filebeat:5.6.5-alpine
cat ds-demo.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-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: ikubernetes/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info # REDIS_HOST这个键值对是filebeat-ds向filebeat配置文件传的环境变量
kubectl apply -f ds-demo.yaml
# 暴露redis端口
kubectl expose deployment redis --port=6379
# 进入redis
kubectl exec -it redis-664bbc646b-sg6wk -- /bin/sh
/data # redis-cli -h redis.default.svc.cluster.local
# 进入filebeat
kubectl exec -it filebeat-ds-bszfz -- /bin/sh
nslookup redis.default.svc.cluster.local
# daemon-set也支持滚动更新
kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.5.7-alpine
kubectl explain pods.spec # 有一个字段hostNetwork,可以让容器直接共享宿主机的网络
注:不同pod之间通信,filebeat向redis发送日志靠的是service
参考博客:http://blog.itpub.net/28916011/viewspace-2214692/