当前位置: 首页 > 工具软件 > deployment > 使用案例 >

K8s系列之:Deployment应用实战和Deployment相关命令整理

都阳辉
2023-12-01

一、Pod和Controller关系

Pod和Controller关系:

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

二、Deployment应用场景

Deployment应用场景:

  • 部署无状态应用
  • 管理Pod和ReplicaSet
  • 部署、滚动升级等功能
  • 应用场景:web服务、微服务

三、创建deployment的yaml文件

kubectl create deployment web --image=nginx --dry-run -o yaml > web.yaml
W0323 16:06:22.869058   58281 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
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文件创建deployment

kubectl apply -f web.yaml

五、deployment扩容

kubectl scale deployment web --replicas=3

也可以在创建deployment时指明副本数量

 kubectl create deployment web --image=nginx --replicas=3 --dry-run -o yaml > web.yaml
W0323 17:24:22.044115  103293 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.

查看创建的deployment的yaml

 cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

六、创建服务的yaml文件

kubectl expose deployment web --port=80 --target-port=80 --type=NodePort --dry-run -o yaml > service.yaml
cat service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort
status:
  loadBalancer: {}

七、根据yaml文件创建服务

kubectl apply -f service.yaml

查看pod、svc

 kubectl get pods,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-85b98978db-2fmpx   1/1     Running   0          5h18m
pod/web-76b56fd968-c7nnr     1/1     Running   0          53m
pod/web-76b56fd968-r8gdd     1/1     Running   0          53m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        6h54m
service/nginx        NodePort    10.99.10.254     <none>        80:30407/TCP   5h35m
service/web          NodePort    10.103.247.181   <none>        80:31870/TCP   9s

八、升级Pod

kubectl set image deployment web nginx=nginx:1.15

九、回滚到上一个版本

kubectl rollout undo deployment web

十、查看升级状态

kubectl rollout status deployment web

十一、查看升级历史

kubectl rollout history deployment web

十二、回滚到指定版本

kubectl rollout undo deployment web --to-revision=2

十三、弹性伸缩

kubectl scale deployment web --replicas=3
 类似资料: