关于kubernetes:K8S-使用-Deployment-运行一个无状态应用Nginx

42次阅读

共计 2909 个字符,预计需要花费 8 分钟才能阅读完成。

一、指标

  1. 创立一个 nginx Deployment
  2. 应用 kubectl 列举对于 Deployment 的信息
  3. 更新 Deployment

二、筹备

你必须领有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。

如果你还没有集群,参考 用 kubeadm 在 Debian 或 Ubuntu 中创立 k8s 集群。

三、教程

1 创立并理解一个 nginx Deployment

你能够通过创立一个 Kubernetes Deployment 对象来运行一个利用,且你应用 yaml 格局的文件创建一个 Deployment 的配置文件。例如, 上面是一个运行 nginx:1.14.2 Docker 镜像的 Deployment 的配置文件:

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

应用 yaml 文件创建一个 Deployment:

kubectl apply -f deployment.yaml

显示 Deployment 相干信息:

kubectl describe deployment nginx-deployment

输入:

Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Thu, 27 May 2021 20:44:33 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.14.2
    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-deployment-66b6c48dd5 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  80s   deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 2

列出 Deployment 创立的 Pods:

kubectl get pods -l app=nginx

输入:

NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66b6c48dd5-8mbkg   1/1     Running   0          2m32s
nginx-deployment-66b6c48dd5-w46gw   1/1     Running   0          2m32s

显示某一个 Pod 信息:

kubectl describe pod <pod-name>

这里的 <pod-name> 是某一 Pod 的名称。

2 更新 Deployment

你能够通过更新一个新的 YAML 文件来更新 Deployment。上面的 YAML 文件指定该 Deployment 镜像更新为 nginx 1.16.1。

deployment-update.yaml

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 # Update the version of nginx from 1.14.2 to 1.16.1
        ports:
        - containerPort: 80

利用新的 YAML:

kubectl apply -f deployment-update.yaml

查看该 Deployment 以新的名称创立 Pods 同时删除旧的 Pods:

kubectl get pods -l app=nginx

3 通过减少正本数来扩缩利用

你能够通过利用新的 YAML 文件来减少 Deployment 中 Pods 的数量。上面的 YAML 文件将 replicas 设置为 4,指定该 Deployment 应有 4 个 Pods:

deployment-scale.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 4 # Update the replicas from 2 to 4
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

利用新的 YAML 文件:

kubectl apply -f deployment-scale.yaml

验证 Deployment 有 4 个 Pods:

kubectl get pods -l app=nginx

输入:

NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66b6c48dd5-h4x8h   1/1     Running   0          39s
nginx-deployment-66b6c48dd5-p7bgv   1/1     Running   0          38s
nginx-deployment-66b6c48dd5-xt5gh   1/1     Running   0          39s
nginx-deployment-66b6c48dd5-z8kj4   1/1     Running   0          38s

4 删除 Deployment

通过名称删除 Deployment:

kubectl delete deployment nginx-deployment

正文完
 0