k8s控制器

控制器

1,ReplicaSet
ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行。 然而,Deployment 是一个更高级的概念,它管理 ReplicaSet,并向 Pod 提供声明式的更新以及许多其他有用的功能。 因此,我们建议使用 Deployment 而不是直接使用 ReplicaSet,除非您需要自定义更新业务流程或根本不需要更新。

这实际上意味着,您可能永远不需要操作 ReplicaSet 对象:而是使用 Deployment,并在 spec 部分定义您的应用。

vim  rs.yaml #编辑文件
apiVersion: apps/v1
kind: ReplicaSet    #rs控制器
metadata:
  name: rs.example  #元数据名称
spec:
  replicas: 3     #副本数
  selector:
    matchLabels:   #匹配标签
      app: nginx
  template:        #模板
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1

查看创建成功

[kubeadm@server1 ~]$ kubectl apply -f rs.yaml 
replicaset.apps/rs.example created
[kubeadm@server1 ~]$ kubectl get pod
NAME               READY   STATUS    RESTARTS   AGE
rs.example-rgmwp   1/1     Running   0          16s
rs.example-rx5qk   1/1     Running   0          16s
rs.example-zzh2p   1/1     Running   0          16s
[kubeadm@server1 ~]$ kubectl get pod -o wide
NAME               READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
rs.example-rgmwp   1/1     Running   0          20s   10.244.2.7    server3   <none>           <none>
rs.example-rx5qk   1/1     Running   0          20s   10.244.1.10   server2   <none>           <none>
rs.example-zzh2p   1/1     Running   0          20s   10.244.0.34   server1   <none>           <none>

rs根据标签来维护pod,所以删除不了,他会自动重新启动,

2,Deployment
支持用例创建 Deployment,更新 Deployment,回滚 Deployment,缩放 Deployment,暂停、恢复 Deployment
编辑文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - containerPort: 80
[kubeadm@server1 ~]$ kubectl apply -f deployment.yaml 
deployment.apps/nginx-deployment created
[kubeadm@server1 ~]$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7d6f48b859-2tgst   1/1     Running   0          8s
nginx-deployment-7d6f48b859-49wqx   1/1     Running   0          8s
nginx-deployment-7d6f48b859-72l2g   1/1     Running   0          8s
[kubeadm@server1 ~]$ vim deployment.yaml 
[kubeadm@server1 ~]$ kubectl get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
nginx-deployment-7d6f48b859-2tgst   1/1     Running   0          80s   10.244.2.8    server3   <none>           <none>
nginx-deployment-7d6f48b859-49wqx   1/1     Running   0          80s   10.244.1.13   server2   <none>           <none>
nginx-deployment-7d6f48b859-72l2g   1/1     Running   0          80s   10.244.0.39   server1   <none>           <none>

nginx-deployment是创建的控制器,
7d6f48b859是调用的rs控制器,
2tgst,49wqx,72l2g,是三个pod号,因为是三个副本

更新,回滚,

更新版本,设为6个副本

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 6
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        ports:
        - containerPort: 80
[kubeadm@server1 ~]$ kubectl apply -f deployment.yaml 
deployment.apps/nginx-deployment configured
[kubeadm@server1 ~]$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6f7794c6c4-2zfpw   1/1     Running   0          114s
nginx-deployment-6f7794c6c4-4ccjk   1/1     Running   0          112s
nginx-deployment-6f7794c6c4-bgw7k   1/1     Running   0          114s
nginx-deployment-6f7794c6c4-gl44m   1/1     Running   0          112s
nginx-deployment-6f7794c6c4-hhzvx   1/1     Running   0          114s
nginx-deployment-6f7794c6c4-v66nz   1/1     Running   0          113s

更新策略:会重新建立rs和新的pod,同时也会保留之前的rs用于回滚,
用文件更新的话只会保留记录看不到你做了什么操作,用命令行来指令的话可以看见,

[kubeadm@server1 ~]$ kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-6f7794c6c4   6         6         6       9m44s
nginx-deployment-7d6f48b859   0         0         0       16m
[kubeadm@server1 ~]$ kubectl rollout history deployment nginx-deployment 
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

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

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-example
spec:
  selector:
    matchLabels:
      name: daemonset-example
  template:
    metadata:
      labels:
        name: daemonset-example
    spec:
      containers:
      - name: daemonset-example
        image: nginx
kubeadm@server1 ~]$ kubectl get pod -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
daemonset-example-62647   1/1     Running   0          13s   10.244.0.45   server1   <none>           <none>
daemonset-example-rrbhk   1/1     Running   0          13s   10.244.2.44   server3   <none>           <none>
daemonset-example-tvz24   1/1     Running   0          13s   10.244.1.39   server2   <none>           <none>

4,job
创建一个或多个Pod,并确保指定数量的Pod成功终止。吊舱成功完成后,工作将跟踪成功完成的情况。当达到指定的成功完成次数时,任务(即Job)就完成了。删除作业将清除其创建的Pod。执行od成功则退出,非零退出默认则会重启六次。

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4
[kubeadm@server1 ~]$ kubectl logs pi-52hx6 
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901

5,CronJob
Cron Job 创建基于时间调度的 Jobs。
一个 CronJob 对象就像 crontab (cron table) 文件中的一行。它用 Cron 格式进行编写,并周期性地在给定的调度时间执行 Job。

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

[kubeadm@server1 ~]$ kubectl get pod
NAME                     READY   STATUS      RESTARTS   AGE
hello-1582350780-xdddt   0/1     Completed   0          2m11s
hello-1582350840-gmfr8   0/1     Completed   0          71s
hello-1582350900-5mfln   0/1     Completed   0          11s
[kubeadm@server1 ~]$ kubectl logs hello-1582350
hello-1582350780-xdddt  hello-1582350840-gmfr8  hello-1582350900-5mfln  
[kubeadm@server1 ~]$ kubectl logs hello-1582350900-5mfln
Sat Feb 22 05:56:03 UTC 2020
Hello from the Kubernetes cluster

你可能感兴趣的:(k8s控制器)