kubernetes - Deployment

一:deployment 资源

1.简介

Deployment 为 Pod 提供声明式更新

在 Deployment 对象中描述所需的状态,然后 Deployment 控制器将实 际状态以受控的速率更改为所需的状态

您可以定义部署以创建新的副本集,或删除现有部署并在新部署中采用 其所有资源

一句话:Deployment 主要功能是保证有足够的 Pod 正常对外提供服务

2.创建 deployment.yaml

# 定义资源类型
kind: Deployment
# 定义API版本号
apiVersion: app1/v1
# 定义基础信息
metadata:
  name: test-deployment
# 定义容器信息
spec:
  # 建立副本数
  replicas: 2
  # 定义Pod的模板
  template:
    metadata:
      labels:
        app: test-deployment
        env: test
    spec:
      containers:
        - name: nginx
          images: nginx:1.19.2
  # 定义选择器
  selector:
    # 精确匹配
    matchLabels:
      app: test-deployment
      env: test

3.部署

选项 描述
--record 允许将当前命令记录在正在创建或更新的资源的注释中
--record=true
-f filename 文件名
kubectl apply -f --record=true [资源路径]	# 存在就更新,不存在就创建
kubectl apply -f deployment.yaml


kubectl create -f [资源清单路径]	# 部署过1次,yaml就不可再创建了,一般不用

查看部署状态

排查问题,最主要看Events

kubectl get pods -w
kubectl get deployment.apps

查看部署详情(错误)

kubectl describe [资源类型] [资源名称]
kubectl describe deployments.apps nginx-deployment

查看运行错误

kubectl logs [pod名称]

删除之后,会再次自动启动

kubectl delete pod name xxx

4.更新

方式1 - 通过命令行修改

kubectl set image [资源类型(控制器资源)] [资源名称] [容器名称]=[新镜像]
kubectl set image deployment test-deployment django=redis

方式2 - 修改yaml文件

vim test.yaml
# 定义资源类型
kind: Deployment
# 定义API版本号
apiVersion: apps/v1
# 定义基础信息
metadata:
  name: test-deployment
# 定义容器信息
spec:
  # 定义副本个数
  replicas: 2
  # 定义Pod的模板
  template:
    metadata:
      labels:
        app: test-deployment
        env: test
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.2
        - name: mysql
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: '123456'
  # 定义选择器
  selector:
    # 精确匹配
    matchLabels:
      app: test-deployment
        env: test

方式3 - 修改资源

kubectl edit [资源类型] [资源名称]

方式4 - 去面板中修改

方式5 - 打补丁

kubectl patch [资源类型] [资源名称] -p [配置项]

kubectl patch deployment.apps test-deployment -p '{"spec": {"replicas": 5}}'

方式6 - 修改仓库的镜像

1.修改仓库的镜像

2.修改进行拉取策略:imagePullPolicy: Always
# Always:不管本地是否有镜像,都会取远程拉取镜像

3.删除那个Pod,让它自动再启一个

5.回滚

当新版本不稳定时,可以对其进行回滚操作,默认情况下,所有 Deployment 的 rollout 历史都保留在系统中, 可以随时回滚

查看部署历史

kubectl rollout history [资源类型] [资源名称]

kubectl rollout history deployment test-back

如果当前的版本和历史版本一致,那么那个历史版本就会消失,只显示当前版本

edit 的缺点:必须得知道上个版本具体是什么

kubectl rollout history deployment test-back

kubectl rollout undo deployment test-back

① 回滚方式1 - edit

kube edit [资源类型] [资源名称]
# 然后修改内容

② 回滚方式2 - undo 回滚到上个版本

kubectl rollout undo [资源类型] [资源名称]

③ 回滚方式3 - 回滚到指定版本

kubectl rollout undo [资源类型] [资源名称] --to-revision=[指定版本号]

6.扩容 与 缩容

① 方式1 - edit

kube edit [资源类型] [资源名称]
# 修改副本数 replicas: 数量

② 方式2 - 修改配置清单 yaml

vim deployment.yaml

③ 方式3 - 打补丁 patch

kubectl patch [资源类型] [资源名称] -p [配置项]

kubectl patch deployment.apps test-deployment -p '{"spec": {"replicas": 10}}'

④ 方式4 - scale

kubectl scale [资源类型] [资源名称] --replicas=[副本数]

kubectl scale deployment.apps test-deployment --replicas=5

⑤ 方式5 - HPA

缺点:默认情况下只能定义成1个副本
docker pull registry.cn-hangzhou.aliyuncs.com/k8sos/metrics-server:v0.4.1

kubectl get pods -n kube-system -l k9s-app=metrics-server

kubectl top pods

7.

查看集群IP

kubectl get pods -o wide

删除其中1个Pod,会自动生成一个新的,但是IP会改变

所以,需要实现负载均衡、动态获取IP、服务发现

二:Service

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-service
spec:
  selector:
    matchLabels:
      app: test-svc
  template:
    metadata:
      labels:
        app: test-svc
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.2

---
kind: Service
apiVersion: v1
metadata:
  name: nginx-svc
spec:
ClusterIP: None
  selector:
    app: test-svc
  ports:
    - port: 80
      targetPort: 80
      name: http
    - port: 443
      targetPort: 443
      name: https

kubectl describe service nginx-svc

image-20201210120118608

image-20201210120149390

image-20201210120153388

image-20201210120231015

实现负载均衡了

image-20201210120414395

image-20201210120456621

探针

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-service
spec:
  selector:
    matchLabels:
      app: test-svc
  template:
    metadata:
      labels:
        app: test-svc
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.2
          # 探针
          readinessProbe:
            tcpSocket:
              port: 80

---
kind: Service
apiVersion: v1
metadata:
  name: nginx-svc
spec:
  selector:
    app: test-svc
  ports:
    - port: 80
      targetPort: 80
      name: http
    - port: 443
      targetPort: 443
      name: https

存活性探测

1.exec

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-service
spec:
  selector:
    matchLabels:
      app: test-svc
  template:
    metadata:
      labels:
        app: test-svc
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.2
          # 存活性探测:exec
          livenessProbe:
            exec:
              command:
                - cat
                - /root/test/manage.py
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-svc
spec:
  selector:
    app: test-svc
  ports:
    - port: 80
      targetPort: 80
      name: http
    - port: 443
      targetPort: 443
      name: https

2.端口

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-service
spec:
  selector:
    matchLabels:
      app: test-svc
  template:
    metadata:
      labels:
        app: test-svc
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.2
          # 存活性探测:端口
          livenessProbe:
            tcpSocket:
              port: 8080
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-svc
spec:
  selector:
    app: test-svc
  ports:
    - port: 80
      targetPort: 80
      name: http
    - port: 443
      targetPort: 443
      name: https

3.httpGet

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-service
spec:
  selector:
    matchLabels:
      app: test-svc
  template:
    metadata:
      labels:
        app: test-svc
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.2
          # 存活性探测:httpGet
          livenessProbe:
            httpGet:
              port: 8080
              path: index.html
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-svc
spec:
  selector:
    app: test-svc
  ports:
    - port: 80
      targetPort: 80
      name: http
    - port: 443
      targetPort: 443
      name: https

就绪性探测

posted @ 2020-12-18 21:19  轻描丨淡写  阅读(375)  评论(0编辑  收藏  举报