CKA 备考 - 3 - 升级一个应用

通过分析实操题来学习、巩固cka中的考点。本次的重点是用升级应用的镜像版本。

题目

CKA 备考 - 1 - 建立一个Deloyment并显示状态 中发布的app的 nginx 镜像版本从 1.16 升级到 1.17, 然后查看复制集

解题思路

核心概念

本题主要涉及到应用版本的升级,在kubernetes中可以通过几种方法对已经发布的应用进行升级。

  1. 重新发布对应的yaml文件
  2. 编辑已经发布的应用的设置
  3. 执行滚动升级

注意观察和比较升级前、后的Pod实例名,感受升级的过程对应用可用性的影响。

相关命令

对于已经发布的应用,如果原来没有建立yaml文件(通过执行 kubectl apply发布)或是不想编辑源yaml文件,可以直接使用 kubectl edit 命令进行编辑。让我们先看看该命令的细节说明, 执行:

1
kubectl edit --help

可以看到如下的系统信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Edit a resource from the default editor.

The edit command allows you to directly edit any API resource you can retrieve via the command-line tools. It will open
the editor defined by your KUBE_EDITOR, or EDITOR environment variables, or fall back to 'vi' for Linux or 'notepad' for
Windows. You can edit multiple objects, although changes are applied one at a time. The command accepts file names as
well as command-line arguments, although the files you point to must be previously saved versions of resources.

Editing is done with the API version used to fetch the resource. To edit using a specific API version, fully-qualify
the resource, version, and group.

The default format is YAML. To edit in JSON, specify "-o json".

The flag --windows-line-endings can be used to force Windows line endings, otherwise the default for your operating
system will be used.

In the event an error occurs while updating, a temporary file will be created on disk that contains your unapplied
changes. The most common error when updating a resource is another editor changing the resource on the server. When this
occurs, you will have to apply your changes to the newer version of the resource, or update your temporary saved copy to
include the latest resource version.

Examples:
# Edit the service named 'docker-registry'
kubectl edit svc/docker-registry

# Use an alternative editor
KUBE_EDITOR="nano" kubectl edit svc/docker-registry

# Edit the job 'myjob' in JSON using the v1 API format
kubectl edit job.v1.batch/myjob -o json

# Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation
kubectl edit deployment/mydeployment -o yaml --save-config

Options:
--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
--field-manager='kubectl-edit': Name of the manager used to track field ownership.
-f, --filename=[]: Filename, directory, or URL to files to use to edit the resource
-k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f or -R.
-o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
--output-patch=false: Output the patch if the resource is edited.
-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
--save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
--show-managed-fields=false: If true, keep the managedFields when printing objects in JSON or YAML format.
--template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
--validate=true: If true, use a schema to validate the input before sending it
--windows-line-endings=false: Defaults to the line ending native to your platform.

Usage:
kubectl edit (RESOURCE/NAME | -f FILENAME) [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

可以看到该命令其实是调用系统中默认的编辑器(Linux中默认为vi)对资源的配置信息进行编辑。

解题步骤

在解本题之前,我们先查看以下现在my-dep的列表,执行 kubectl get pod, 得到:

1
2
3
4
5
6
NAME                      READY   STATUS    RESTARTS      AGE
my-dep-655995c9d4-2rr72 1/1 Running 1 (26m ago) 124m
my-dep-655995c9d4-4gknh 1/1 Running 1 (26m ago) 124m
my-dep-655995c9d4-6x66r 1/1 Running 1 (26m ago) 70m
my-dep-655995c9d4-mtqwb 1/1 Running 1 (26m ago) 70m
my-dep-655995c9d4-pz849 1/1 Running 1 (26m ago) 124m

因为我们要升级的是 my-dep 中的Pod, 现在让我们来编辑Deployment: my-dep, 所以自己编辑my-dep, 执行

1
kubectl edit deployment/my-dep

打开文件后,修改 containers/image中的nginx:1.16 为 nginx:1.17, 然后保存

退出 edit 以后,执行 kubectl get pod 命令,可以看到类似如下的信息

1
2
3
4
5
6
7
8
NAME                      READY   STATUS              RESTARTS      AGE
my-dep-655995c9d4-2rr72 1/1 Running 1 (43m ago) 141m
my-dep-655995c9d4-4gknh 1/1 Running 1 (43m ago) 141m
my-dep-655995c9d4-6x66r 1/1 Running 1 (43m ago) 87m
my-dep-655995c9d4-pz849 1/1 Running 1 (43m ago) 141m
my-dep-86bdfb944c-g8bs4 0/1 ContainerCreating 0 18s
my-dep-86bdfb944c-jm5f9 0/1 ContainerCreating 0 18s
my-dep-86bdfb944c-sg695 0/1 ContainerCreating 0 18s

可以看到,kubernetes集群开始用新的进行建立Pod, 同时保持着旧Pod的运行。

等下载完成后,会生成全新的Pod, 如:

1
2
3
4
5
6
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-86bdfb944c-f286n 1/1 Running 0 56s
my-dep-86bdfb944c-g8bs4 1/1 Running 0 92s
my-dep-86bdfb944c-jm5f9 1/1 Running 0 92s
my-dep-86bdfb944c-rr9t7 1/1 Running 0 54s
my-dep-86bdfb944c-sg695 1/1 Running 0 92s

查看 rollout 历史,可以看到

1
2
3
REVISION  CHANGE-CAUSE
1 <none>
2 <none>

可以看到发行的版本升级了。

查看复制集

1
kubectl get rs

系统显示

1
2
3
NAME                DESIRED   CURRENT   READY   AGE
my-dep-655995c9d4 0 0 0 4h57m
my-dep-86bdfb944c 5 5 5 156m

本文标题:CKA 备考 - 3 - 升级一个应用

文章作者:Morning Star

发布时间:2022年12月14日 - 07:12

最后更新:2022年12月15日 - 07:12

原始链接:https://www.mls-tech.info/microservice/k8s/kubernetes-cka-preparation-03-upgrade-app/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。