K8s-Pod控制器详解

我只是一个虾纸丫 提交于 2020-08-17 11:54:35

1.ReplicationController 和ReplicaSet(推荐使用)

Rc:用来确保容器应用的副本数始终保持在用户定义的副本数,即如果容器异常退出,会自动创建新的Pod来代替,而如果异常多出来的容器也会自动回收。

Rs:新版版K8s推荐使用Rs代替Rc,Rs与Rc没有本质的不同,Rs支持集合式的selector

虽然Rs可以独立使用,但是一般建议使用Deployment来自动管理Rs。

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: frontend
  # these labels can be applied automatically
  # from the labels in the pod template if not set
  # labels:
    # app: guestbook
    # tier: frontend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 3
  # selector can be applied automatically
  # from the labels in the pod template if not set,
  # but we are specifying the selector here to
  # demonstrate its usage.
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:
        - containerPort: 80

2.Deployment为Pod和ReplicaSet提供了一个声明式方法,用来替代以前Rc来方便管理应用。典型的应用场景包括:

  • 定义Deployment来创建Pod和Rs
  • 滚动升级和回滚应用
  • 扩容和缩容
  • 暂停和继续Deployment

简单定义:

扩(缩)容命令:

kubectl scale deployment nginx-deployment --replicas 10

若集群支持HPA的话,还可以为Deployment设置自动扩展:

kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80

更新镜像:

kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1

回滚:

kubectl rollout undo deployment/nginx-deployment

...........

 

StatefulSet

StatefulSet作为Controller为Pod提供唯一的标识,可以保证部署和scale的顺序,为了解决有状态服务的问题,其应用场景包括:

  • 稳定的持久化存储,即Pod重新调度后还是能访问到相同的持久化数据,基于PVC实现
  • 稳定的网络标识,即Pod重新调度后PodName和HostName不变,基于Headless Service(即没有Cluster IP的service)来实现
  • 有序部署,有序扩展,有序收缩,有序删除

 

DaemonSet

DaemonSet确保全部(或者一些)Node上运行一个Pod副本,当有Node加入集群时,也会为他们新增一个Pod,当有Node从集群移除时,这些Pod也会删除。删除DaemonSet将会删除它创建的所有Pod。

使用DeamonSet的一些典型用法

  • 运行集群存储 daemon
  • 在每个 Node 上运行日志收集 daemon
  • 在每个 Node 上运行监控 daemon

 

Job

负责批处理任务,即仅执行一次的任务,保证批处理任务的一个或多个Pod成功结束

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!