Controller控制器

文章目录

一、什么是Controller?

Kubernetes 通常不会直接创建 Pod,而是通过 Controller 来管理 Pod 的。Controller 中定义了 Pod 的部署特性,比如有几个副本,在什么样的 Node 上运行等。为了满足不同的业务场景,Kubernetes 提供了多种 Controller,包括 Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job 等。

二、Pod和Controller关系

Pod是通过Controller实现应用的运维,比如伸缩、滚动升级等等。
Pod和Controller之间通过label标签建立关系

三、Deployment控制器应用场景

  • 部署无状态应用
  • 管理Pod和ReplicaSet
  • 部署、滚动升级等功能

主要有web服务、微服务

四、yaml文件字段说明

[root@node1 home]# kubectl create deployment nginx --image=nginx -o yaml --dry-run > nginx.yaml
[root@node1 home]# cat nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
# 使用yaml部署应用,默认部署端口是31883
[root@node1 home]# kubectl apply -f nginx.yaml 
# 对外发布(暴露端口)指定对外端口号
[root@node1 home]# kubectl expose deployment nginx --port=80 --type=NodePort --target-port=80 --name=nginx -o yaml > nginx.yaml
[root@node1 home]# kubectl apply -f nginx.yaml 

五、Deployment控制器部署应用

六、升级回滚

应用升级

#部署nginx1.14版本
[root@node1 ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
nginx-5658bdf5d4-mrpdk   1/1     Running   0          12h   10.244.0.4   node2   <none>           <none>
[root@node1 ~]# 

#mode2节点上查看nginx版本
[root@node2 ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
nginx                     latest              4cdc5dd7eaad        4 days ago          133MB
nginx                     <none>              4f380adfc10f        2 weeks ago         133MB
quay.io/coreos/flannel    v0.14.0             8522d622299c        7 weeks ago         67.9MB
nginx                     1.14                295c7be07902        2 years ago         109MB
lizhenliang/pause-amd64   3.0                 99e59f495ffa        5 years ago         747kB

#开始升级nginx
[root@node1 ~]# kubectl set image deployment nginx nginx=nginx:1.15
deployment.apps/nginx image updated
[root@node1 ~]# 

[root@node3 ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
nginx                     latest              4cdc5dd7eaad        4 days ago          133MB
quay.io/coreos/flannel    v0.14.0             8522d622299c        7 weeks ago         67.9MB
nginx                     1.15                53f3fd8007f7        2 years ago         109MB
lizhenliang/pause-amd64   3.0                 99e59f495ffa        5 years ago         747kB
[root@node3 ~]# 

# 查看升级状态
[root@node1 ~]# kubectl rollout status deployment nginx
deployment "nginx" successfully rolled out
[root@node1 ~]# 

#查看升级历史
[root@node1 ~]# kubectl rollout history deployment nginx
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

应用回滚

# 回滚到上一个版本
[root@node1 ~]# kubectl rollout undo deployment nginx
deployment.apps/nginx rolled back
[root@node1 ~]# 

# 回滚到上一个版本
[root@node1 ~]# kubectl rollout undo deployment nginx
deployment.apps/nginx rolled back
[root@node1 ~]# kubectl rollout history deployment nginx
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

#回滚到指定版本
[root@node1 ~]# kubectl rollout undo deployment nginx --to-revision=2
deployment.apps/nginx rolled back
[root@node1 ~]# 

七、弹性伸缩

扩容

[root@node1 ~]# kubectl scale deployment nginx --replicas=3
[root@node1 ~]# kubectl get  pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
nginx-764b95f4c5-4mr54   1/1     Running   0          27s     10.244.0.6    node2   <none>           <none>
nginx-764b95f4c5-clskx   1/1     Running   0          27s     10.244.1.14   node3   <none>           <none>
nginx-764b95f4c5-xsd82   1/1     Running   0          4m23s   10.244.1.13   node3   <none>           <none>
[root@node1 ~]# 

缩容

[root@node1 ~]# kubectl scale deployment nginx --replicas=1
deployment.apps/nginx scaled
[root@node1 ~]# kubectl get  pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
nginx-764b95f4c5-4mr54   1/1     Running   0          4m58s   10.244.0.6   node2   <none>           <none>
[root@node1 ~]# 
上一篇:kubernetes核心概念 7月18日学习笔记


下一篇:6 第六章 Pod控制器详解