关于k8s:k8s-pod控制器随记

49次阅读

共计 4808 个字符,预计需要花费 13 分钟才能阅读完成。

1.pod 控制器类别

ReplicaSet

deployment  次要 

Horizontal Pod Autoscaler (hpa) 高可用  实现削峰填谷 

DaemonSet 守护进厂类的工作

Job 执行一次工作,执行完之后就完结

Cronjob 周期性的执行工作

StatefulSet 治理有状态利用 

2. 具体控制器

------------------------------------------------pod 控制器 ------------------------------------------------------

replicaset

apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: myrs
    namespace: dev
    labels:
      controller: rs
spec:
    replicas: 3
    selector: 
       matchLabels: 
          app: nginx-pod
       matchExpressions:
       - {key: app,operator: In, Values: [nginx-pod]}
    template:
      metadata:
        labels: 
           app: nginx-pod
        spec:
           containers:
           - name: nginx
             image: nginx:1.17.1
             ports: 
             - containerPort: 80

--------------------------------------------             
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-rs
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec: 
        containers:
        - name: my-nginx
          image: nginx
--------------------------------------------    

[root@node1 ~]# kubectl get rs -n dev -o wide
NAME                 DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
my-rs                3         3         3       3m34s   my-nginx     nginx    app=nginx-pod
--------------------------------------------

[root@node1 ~]# kubectl get pod -n dev
NAME                       READY   STATUS    RESTARTS   AGE
my-rs-6wbdd                1/1     Running   0          5m
my-rs-cs6nr                1/1     Running   0          5m
my-rs-hzncc                1/1     Running   0          5m

--------------------------------------------
rs 扩缩容

kubectl edit rs my-rs -n dev  批改 replicas 即可

kubectl scale rs my-rs --replicas=2 -n dev
--------------------------------------------
镜像版本升级
kubectl edit rs my-rs -n dev  批改 image

kubectl set image rs my-rs my-nginx=nginx:1.18.0 -n dev

删除 rs

kubectl delete rs my-rs -n dev  #首先把正本数设置成 0, 而后再删除
kubectl delete rs my-rs -n dev --cascade=false #只删除控制器,不删除 pod

kubectl delete -f my-rs.yaml 


---------------
deployment

反对 replicaset 的所有性能
反对版本回滚和版本回退


apiVersion: apps/v1
kind: Deployment
metadata: 
  name: mynginx
  namespace: dev
spec:
  replicas: 3
  selector: 
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
       containers:
       - image: nginx
         imagePullPolicy: IfNotPresent
         name: nginx
         ports: 
         - containerPort: 80
           protocol: TCP

kubectl apply -f yaml-nginx-deploy.yaml

kubectl delete -f yaml-nginx-deploy.yaml

查问对应的 rs: 

[root@node1 ~]# kubectl get rs -n dev -o wide
NAME                 DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
mynginx-869c47cdd9   3         3         3       3m32s   nginx        nginx    pod-template-hash=869c47cdd9,run=nginx


扩缩容:kubectl scale deploy my-nginx --replicas=5 -n dev
    kubectl edit deploy my-nginx -n dev

    [root@node1 ~]# kubectl scale deploy mynginx --replicas=5 -n dev
    deployment.apps/mynginx scaled
    
    kubectl edit deploy mynginx -n dev  
    批改 replicas 

镜像降级:
    重建更新:滚动更新:(默认滚动更新) 新的起来了之后,旧的才会进行
    -----
    spec: 
        strategy: 
            type: Recreate 
    -----
    spec: 
        strategy: 
            type: RollingUpdate
            maxUnavailable: 25%
            maxSurge: 25%            
    
    kubectl set image deploy mynginx nginx=nginx -n dev    
    
    kubectl get pod -n dev -w 
    
    滚动更新 镜像降级,创立新的 rs,原来的 rs 会进行保留,成为上一个版本
    
    

版本回退
    kubectl rollout 
    status history pause resume  restart undo 
    
    查看状态
    [root@node1 ~]# kubectl rollout status deploy mynginx -n dev
    Waiting for deployment "mynginx" rollout to finish: 1 out of 2 new replicas have been updated...
    Waiting for deployment "mynginx" rollout to finish: 1 out of 2 new replicas have been updated...
    Waiting for deployment "mynginx" rollout to finish: 1 out of 2 new replicas have been updated...
    Waiting for deployment "mynginx" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "mynginx" rollout to finish: 1 old replicas are pending termination...
    deployment "mynginx" successfully rolled out
    
    查看历史
    kubectl rollout history deploy mynginx -n dev
    kubectl apply -f yaml-nginx-deploy.yaml --record
    
    查看历史
    [root@node1 ~]# kubectl rollout history deploy mynginx -n dev
    deployment.apps/mynginx 
    REVISION  CHANGE-CAUSE
    1         <none>
    2         <none>

    // 回退版本 to-revision 指定版本
    [root@node1 ~]# 
    [root@node1 ~]# 
    [root@node1 ~]# kubectl rollout undo deployment mynginx --to-revision=1 -n dev

金丝雀公布
    中间状态:灰度公布 
    
    kubectl set image deploy mynginx nginx=nginx:1.17.4 -n dev && kubectl rollout pause deploy mynginx -n dev
    
    
    
--------------------------------------------------hpa------------------------------------------
Horizontal Pod Autoscaler

手动调整改为主动调整,须要计算负载量

须要装置监控  Metrics-server

kubectl top node  查看资源应用信息

apiVersion:autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
    name: my-hpa
    namespace: dev
spec:
    minReplicas: 1
    maxReplicase: 10
    targetCPUUtilizationPercentage: 3
    scaleTargeRef:
       apiVersion: apps/v1
       kind: Deployment
       name: mynginx


--------------------------------------------------hpa------------------------------------------

网络局部 cni
--------------------------------------------------DaemonSet(ds)------------------------------------------
保障集群中每个节点运行一个 pod
每增加一个 node, 就会主动运行 pod

其 yaml 格局同 deploy


--------------------------------------------------DaemonSet------------------------------------------

--------------------------------------------------job------------------------------------------
批量解决一次性工作

job 对应的 pod 的重启策略为 Never, 或者 OnFailure

次要的配置项:manualSelector: true
completions: 6 #指定运行的 pod 总数
parallelism:3 #并发执行总数


--------------------------------------------------Cronjob------------------------------------------
job 工作创立之后立即执行

cronjob 以 job 为治理对象,相似 cron 表达式 
在指定的工夫点执行工作,反复运行

schedule:                         cron 表达式,最短时间为分钟
concurrencyPolicy:                并发执行工作,前一次工作没有实现,是否执行下一次工作,Allow 容许(默认)Forbid 禁止  Replace 替换,勾销以后的工作,执行新的工作
failedJobHistoryLimit:            为失败的工作保留的历史记录数目
successfulJobHistoryLimit:      为胜利的工作保留历史记录数目
startingDeadlineSeconds:        启动工作谬误的超时工夫
jobTemplate: 

正文完
 0