caitiao的博客

就现在,Just do it

0%

使用Deployment运行一个无状态应用

  1. 创建一个YAML配置文件 nginx-deployment.yaml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: nginx-deployment
    spec:
    selector:
    matchLabels:
    app: nginx
    replicas: 2 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
    template:
    metadata:
    labels:
    app: nginx
    spec:
    containers:
    - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80
  2. 提交Deployment配置到k8s集群
    1
    kubectl apply -f nginx-deployment.yaml
  3. 检查创建的应用是否成功
    1
    2
    3
    kubectl describe deployment nginx-deployment #显示Deployment信息
    kubectl get pods -l app=nginx #查看Pod列表
    kubectl decribe pod <pod-name> #查看Pod信息
  4. 升级nginx版本1.14.2->1.16.1,创建nginx-deployment-update.yaml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: nginx-deployment
    spec:
    selector:
    matchLabels:
    app: nginx
    replicas: 2
    template:
    metadata:
    labels:
    app: nginx
    spec:
    containers:
    - name: nginx
    image: nginx:1.16.1 # 将 nginx 版本从 1.14.2 更新为 1.16.1
    ports:
    - containerPort: 80
  5. 执行nginx更新
    1
    kubectl apply -f nginx-deployment-update.yaml
  6. 通过增加副本数来扩容应用,创建nginx-deployment-scale.yaml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: nginx-deployment
    spec:
    selector:
    matchLabels:
    app: nginx
    replicas: 4 # 将副本数从 2 更新为 4
    template:
    metadata:
    labels:
    app: nginx
    spec:
    containers:
    - name: nginx
    image: nginx:1.16.1
    ports:
    - containerPort: 80
  7. 执行扩容动作
    1
    kubectl apply -f nginx-deployment-scale.yaml
  8. 检查创建的应用是否扩容成功
    1
    kubectl get pods -l app=nginx #查看Pod列表,2->4