controller(控制器)

1、什么是controller

(1)在集群上管理和运行容器的对象

2、Pod和controller关系

(1)Pod是通过Controller实现应用的运维,比如伸缩、滚动升级等

(2)Pod和Controller之间通过Pod上打label标签和controller上的selector建立关系

3、Deployment控制器的应用场景

(1)部署无状态的应用(web服务,微服务)

(2)管理Pod和ReplicaSet(副本创建)

(3)部署,滚动升级

4、yaml文件字段说明

[root@k8smaster ~]# kubectl create deployment web --image=nginx --dry-run -o yaml > web.yaml
W1214 15:55:56.144418   28060 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
[root@k8smaster ~]# cat web.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
#通过yaml文件进行部署
[root@k8smaster ~]# kubectl apply -f web.yaml 
deployment.apps/web created
#查看部署的pod
[root@k8smaster ~]# kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
web-5dcb957ccc-2xfmz   1/1     Running   0          14s
#对外发布(暴露端口)
[root@k8smaster ~]# kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web1 -o yaml > web1.yaml
[root@k8smaster ~]# cat web1.yaml 
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2021-12-14T08:11:03Z"
  labels:
    app: web
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:app: {}
      f:spec:
        f:externalTrafficPolicy: {}
        f:ports:
          .: {}
          k:{"port":80,"protocol":"TCP"}:
            .: {}
            f:port: {}
            f:protocol: {}
            f:targetPort: {}
        f:selector:
          .: {}
          f:app: {}
        f:sessionAffinity: {}
        f:type: {}
    manager: kubectl
    operation: Update
    time: "2021-12-14T08:11:03Z"
  name: web1
  namespace: default
  resourceVersion: "462761"
  selfLink: /api/v1/namespaces/default/services/web1
  uid: 69d9eeb0-2ec3-4599-ad2b-3088acd3646b
spec:
  clusterIP: 10.101.16.206
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30385
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}
  
[root@k8smaster ~]# kubectl apply -f web1.yaml 
#查看svc
[root@k8smaster ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        2d5h
web1         NodePort    10.101.16.206   <none>        80:30385/TCP   49s

5、Deployment控制器部署应用

6、升级回滚和弹性伸缩

#修改yaml文件,把replicas副本数修改为2,把nginx的版本设置为1.14
[root@k8smaster ~]# cat web.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx:1.14
        name: nginx
        resources: {}
status: {}
[root@k8smaster ~]# kubectl apply -f web.yaml 
deployment.apps/web created
[root@k8smaster ~]# kubectl get pod
NAME                  READY   STATUS              RESTARTS   AGE
web-65b7447c7-779jw   0/1     ContainerCreating   0          10s
web-65b7447c7-j9wv9   0/1     ContainerCreating   0          10s
#升级命令,升级过程中服务不中断
[root@k8smaster ~]# kubectl set image deployment web nginx=nginx:1.15
deployment.apps/web image updated
#查看是否升级成功
[root@k8smaster ~]# kubectl rollout status deployment web
deployment "web" successfully rolled out
#查看历史版本
[root@k8smaster ~]# kubectl rollout history deployment web
deployment.apps/web 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
#回滚到上一个版本
[root@k8smaster ~]# kubectl rollout undo deployment web
deployment.apps/web rolled back
#回滚到指定版本
[root@k8smaster ~]# kubectl rollout undo deployment web --to-revision=2
deployment.apps/web rolled back
#弹性伸缩
[root@k8smaster ~]# kubectl scale deployment web --replicas=10
deployment.apps/web scaled
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐