美文网首页
五、Pod控制器

五、Pod控制器

作者: jan29 | 来源:发表于2021-11-25 17:34 被阅读0次

    1、ReplicaSet

    用于弹性伸缩副本,是最基础的pod控制器

    1.1简单实例

    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: base-rs-httpd
      namespace: default
      labels:
        app: web
        release: cannary
    spec:
      replicas: 3
      selector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - web
        - key: release
          operator: In
          values:
          - cannary
      template:
        metadata:
          labels:
            app: web
            release: cannary
        spec:
          containers:
          - name: httpd
            image: httpd:2.4
            ports:
            - name: httpd
              containerPort: 80
            imagePullPolicy: IfNotPresent
            livenessProbe:
              tcpSocket:
                port: 80
            readinessProbe:
              httpGet:
                path: /
    

    通过标签选择器选择后端的pod
    标签选择器有两种配置方式matchExpressionsmatchLabels
    语法格式查看kubectl explain rs.spec.selector

    1.2弹性伸缩

    spec:
      replicas: 3
    

    只需要修改其replicas字段即可

    1.3版本更新

        spec:
          containers:
          - name: httpd
            image: httpd:latest
    

    只需修改其image字段即可
    但是rs有一个问题,不能自动更新,所以需要用到deployment

    2、Deployment

    建立于ReplicaSet之上,比rs功能更加强大。可以自动更新,并且可以设置更新策略。

    2.1自动更新

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: base-deploy-web
    spec:
      replicas: 7
      selector:
        matchLabels:
          app: web-http
          release: stable
      template:
        metadata:
          labels:
            app: web-http
            release: stable
        spec:
          containers:
          - name: nginx
            image: nginx:1.18
            imagePullPolicy: IfNotPresent
            ports:
            - name: nginx
              containerPort: 80
          restartPolicy: Always
    

    2.2基于策略自动更新

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: base-deploy-web
    spec:
      replicas: 7
    #  paused: True
      strategy:
        rollingUpdate:
          maxSurge: 10%
          maxUnavailable: 0
      selector:
        matchLabels:
          app: web-http
          release: stable
      template:
        metadata:
          labels:
            app: web-http
            release: stable
        spec:
          containers:
          - name: nginx
            image: nginx:1.18
            imagePullPolicy: IfNotPresent
            ports:
            - name: nginx
              containerPort: 80
          restartPolicy: Always
    

    maxSurge: 代表最大用于更新时可以超过原pod的多少
    maxUnavailable: 代表最大用于更新时不可提供服务的原pod为多少
    可以使用数字和百分比

    2.3金丝雀发布

    [root@master inventory]# kubectl set image deployment base-deploy-web nginx=nginx:lastet && kubectl rollout pause deployment base-deploy-web
    

    更新一个容器后立即暂停,当后期业务没有问题,再全部更新

    3、DaemonSet

    用于一个节点部署一个pod,一般用于做集群守护日志收集监控告警

    3.1日志收集

    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: fluentd-elasticsearch
      namespace: kube-system
      labels:
        k8s-app: fluentd-elasticsearch
      annntations:
    spec:
      selector:
        matchLabels:
          name: fluentd-elasticsearch
      template:
        metadata:
          labels:
            name: fluentd-elasticsearch
        spec:
          containers:
          - name: fluentd-elasticsearch
            image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
            imagePullPolicy: IfNotPresent
          restartPolicy: Always
    

    配置字段跟deployment基本相同,只是少了replicas,因为其不用于一个节点跑多个Pod

    相关文章

      网友评论

          本文标题:五、Pod控制器

          本文链接:https://www.haomeiwen.com/subject/ctqztrtx.html