k8s之deployment

Deployment介绍

在k8s中,pod是最小的控制单元,但是k8s很少直接控制pod,一般都是通过pod控制器来完成。

pod控制器用于对pod的管理,确保pod资源符合预期的状态,当pod的资源出现故障时,会尝试进行重启或重建pod

在k8s中pod控制器的种类很多,本章节只介绍一种deployment

 

命令操作

命令格式:kubectl run deployment名称 [参数]

--image  指定pod的镜像

--port  指定端口

--replicas  指定创建pod数量

--namespace  指定namespace

查看创建的pod

#创建一个pod
[root@master ~]# kubectl run nginx --image=nginx:1.17.1 --port=80 --replicas=3 --namespace=dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
#同时查看pod和deployment两种资源
[root@master ~]# kubectl get deployment,pod -n dev
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           114s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-64777cd554-2mbxb   1/1     Running   0          114s
pod/nginx-64777cd554-h6spm   1/1     Running   0          114s
pod/nginx-64777cd554-zjdp8   1/1     Running   0          114s

查看创建的deployment的详细信息

[root@master ~]# kubectl describe deploy nginx -n dev
Name:                   nginx
Namespace:              dev
CreationTimestamp:      Fri, 02 Jul 2021 19:29:46 +0800
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx:1.17.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-64777cd554 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  4m42s  deployment-controller  Scaled up replica set nginx-64777cd554 to 3

删除deployment

[root@master ~]# kubectl delete deploy nginx -n dev
deployment.apps "nginx" deleted
[root@master ~]# kubectl get pod -n dev
No resources found in dev namespace.

 使用配置文件来操作

创建一个deploy-nginx.yaml,内容如下:

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

 

 操作配置文件

#创建
[root@master ~]# vim deploy-nginx.yaml
[root@master ~]# kubectl create -f deploy-nginx.yaml 
deployment.apps/nginx created
[root@master ~]# kubectl get deploy,pod -n dev
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           14s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-64777cd554-5k9m4   1/1     Running   0          13s
pod/nginx-64777cd554-gbn6t   1/1     Running   0          13s
pod/nginx-64777cd554-qwn2s   1/1     Running   0          13s

#删除
[root@master ~]# kubectl delete -f deploy-nginx.yaml 
deployment.apps "nginx" deleted
[root@master ~]# kubectl get deploy,pod -n dev
No resources found in dev namespace.
posted @ 2021-07-02 20:12  路过的柚子厨  阅读(1447)  评论(0编辑  收藏  举报