Pod控制器:
自主定义的pod资源删除后不会被重启,被Pod控制器管理的pod资源被删除后会重启。
pod控制器的种类:
ReplicationController: (最早使用,现在已经被废弃,太重。)
* ReplicaSet: 代用户创建管理用户所期望的pod资源,支持扩缩容,
1.用户期望副本数
2.标签选择器
3.pod资源模板
* Deployment: (工作在ReplicaSet上。) 支持滚动更新,回滚,支持申明性资源(k8s推荐) 管理无状态pod最推荐的控制器。
* DaemonSet: 守护进程类,系统级别的,done了无法被重构,可以保证每个节点上只运行一个相关类型的Pod,并且可以设置为只在期望的节点上运行一个pod
Job: 运行一次,确保pod中是正常运行
Cronjob: 周期性运行,不需要持续后台运行
StatefulSet: 可以管理有状态的Pod应用
TPR: Third Party Resources 1.2+,1.7
CDR: Custom Defined Resources 1.8+
Operator:
Helm: 类似linux yum
========
ReplicaSet eg:
[root@k8s-master manifests]# cat ReplicaSet.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: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
[root@k8s-master manifests]# kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp 2 2 2 32h
nginx-deploy-55d8d67cf 1 1 1 46h
[root@k8s-master manifests]#
[root@k8s-master manifests]#
扩缩容
[root@k8s-master manifests]# kubectl edit rs myapp #实时修改
replicaset.extensions/myapp edited
========
Deployment eg:
[root@k8s-master manifests]# cat deployment.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
==========
kubectl apply -f deploy-demo.yaml (apply申明式,可以执行多次,可以直接编辑yaml文件来修改期望的状态。比creat好)
滚动更新:
直接修改yaml文件 ,然后再次执行 kubectl apply -f deploy-demo.yaml
kubectl rollout history deployment myapp-deploy 查看滚动历史记录
kubectl patch deployment myapp-deploy -p '{"spec": {"replicas":5}}' 打补丁的方式修改更新策略
金司雀发布
kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl
回滚到发布的额第一个版本:
kubectl rollout undo deployment myapp-deploy --to-revision=1
查看当前控制器版本:
kubectl rollout history deployment myapp-deploy
=====
DaemonSet eg:
[root@k8s-master manifests]# cat daemonset.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: myapp-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
===========
=======================================================