k8s docker 使用指北
K8S 及docker使用指北
文章目录 1
一、Docker常用命令总结 5
1、Docker容器常用操作 6
(1)镜像的查看与删除 6
(2)容器的启动与进入 6
(3)通过容器提交镜像 7
(4)镜像的保存与加载 7
(5)容器与主机之间进行数据拷贝 7
(6)镜像重命名 8
(7)使用GPU运行容器 8
(8)容器的保存与导入 8
(9)镜像的拉取与上传 9
2、注意事项 9
二、k8s常用命令总结 10
1、kubectl用法概述 10
2、kubectl输出格式 13
3、kubectl常用操作 14
(1)创建资源对象 14
(2)查看资源对象 15
(3)描述资源对象 15
(4)删除资源对象 15
(5)执行容器命令 16
(6)查看容器日志 16
(7)查看集群信息 16
三、nvidia-smi常用命令总结 16
1、显示所有GPU的当前信息状态 16
2、实时查看GPU使用情况 18
四、k8s陈述式管理 18
- 陈述式资源管理方法 18
- k8s相关信息查看 18
2.1 查看版本信息 18
2.2 查看资源对象简写 18
2.3 查看集群信息 21
2.4 配置kubectl自动补全 21
2.5 查看日志 21
2.6 基本信息查看 21
2.6.1 查看master节点状态 21
2.6.2 查看命名空间 22
2.7 命名空间操作 22
2.7.1 查看default命名空间的所有资源 22
2.7.2 创建命名空间app 22
2.7.3 删除命名空间app 23
2.8 deployment/pod操作 23
2.8.1 在命名空间kube-public创建副本控制器(deployment)来启动Pod(nginx-test) 23
2.8.2 描述某个资源的详细信息 23
2.8.3 查看命名空间kube-public中pod信息 24
2.8.4 登录容器 24
2.8.5 删除(重启)pod资源 24
2.8.6 若无法删除,总是处于terminate状态,则要强行删除pod 25
2.8.7 扩缩容 25
2.8.8 删除副本控制器 26
2.9 增加/删除label 26
2.9.1 增加label 26
2.9.2 删除label 26 - K8S模拟项目 26
3.1 项目的生命周期 26
3.2 创建kubectl run命令 26
3.3 发布kubectl expose命令 27
3.3.1 Service的作用 27
3.3.2 Service的类型 28
3.3.3 查看Pod网络状态详细信息和Service暴露的端口 28
3.3.4 查看关联后端的节点 29
3.3.5 查看service的描述信息 29
3.3.6 查看负载均衡端口 29
3.3.7 访问查看 30
3.3.8 查看访问日志 31
3.4 更新kubectl set 32
3.4.1 获取修改模板 32
3.4.2 查看当前nginx的版本号 32
3.4.3 将nginx版本更新为1.15 32
3.4.4 监听pod状态 32
3.4.5 查看pod的ip变化 33
3.4.6 重新查看nginx版本号 33
3.5 回滚kubectl rollout 34
3.5.1 查看历史版本 34
3.5.2 执行回滚到上一个版本 34
3.5.3 执行回滚到指定版本 35
3.5.4 检查回滚状态 35
3.6 删除kubectl delete 36
3.6.1 删除副本控制器 36
3.6.2 删除service 36 - 金丝雀发布/灰度发布(Canary Release) 36
4.1 金丝雀发布简介 36
4.2 更新deployment的版本,并配置暂停deployment 36
4.2.1 创建pods 36
4.2.2 发布服务 37
4.2.3 查看nginx版本 37
4.2.4 定义版本CHANGE-CAUSE 37
4.2.5 更新nginx版本为1.15并配置暂停 38
4.2.6 观察更新状态 38
4.2.7 监控更新的过程 38
4.2.8 查看nginx版本 39
4.2.9 查看并更新历史版本change-cause 40
4.2.10 resume继续更新 41
4.2.11 查看最后的更新情况 41
五、声明式管理 41 - 声明式管理方法 41
- 查看资源配置清单 42
- 解释资源配置清单 44
- 修改资源配置清单并应用 45
4.1 离线修改 45
4.2 在线修改 46 - 删除资源配置清单 46
5.1 陈述式删除 46
5.2 声明式删除 46
一、Docker常用命令总结
1、Docker容器常用操作
(1)镜像的查看与删除
• 查看镜像:docker images 或 docke image ls
• 删除容器:docker rm 容器ID //注意删除容器前先停止容器
• 删除镜像:docker rmi 镜像名 //注意删除镜像前先删除容器
(2)容器的启动与进入
• 运行容器:docker run -it 镜像名 /bin/bash
• 退出容器:exit 或者 Ctrl+P+Q
• 查看容器:docker ps -a
• 查看运行的容器:docker ps
• 容器停止:docker stop 容器ID
• 启动容器:docker start 容器ID
• 重启容器:docker restart 容器ID
• 重启容器后进入交互式:docker start -i 5c6ce895b979
• 进入容器:docker attach 容器ID docker exec -it 容器ID /bin/bash
• 举例:将nginx镜像运行为一个容器:docker run -d -p 81:80 -it nginx /bin/bash //其中-d是保持后台运行,-p 是端口映射,将内部的端口80向外部的端口81映射,/bin/bash就表示启动容器后启动bash。
(3)通过容器提交镜像
• 提交镜像:docker commit 容器ID 镜像名[:tag] 举例:docker commit f7507396068a vehicle_detect:v1
(4)镜像的保存与加载
• 保存镜像:docker save m2>m2.tar //保存m2镜像到m2.tar文件
docker save m2 -o /home/m2.tar
• 镜像加载:docker load<m2.tar
docker load -i m2.tar
(5)容器与主机之间进行数据拷贝
• 主机目录拷贝到容器目录:docker cp /data/test 96f7f14e99ab:/data/ # 将主机/data/test目录拷贝到容器96f7f14e99ab的/data目录下
• 主机目录拷贝到容器目录,目录重命名:docker cp /data/test 96f7f14e99ab:/data # 将主机/data/test目录拷贝到容器96f7f14e99ab中,目录重命名为data
• 容器目录拷贝到主机目录:docker cp 96f7f14e99ab:/data /tmp/ # 将容器96f7f14e99ab的/data目录拷贝到主机的/tmp目录中
(6)镜像重命名
• 重命名镜像:docker tag ubuntu:15.10 asialee/ubuntu:v3 #将镜像ubuntu:15.10标记为 asialee/ubuntu:v3 镜像
(7)使用GPU运行容器
• 容器使用所有GPU:docker run -it --gpus all vehicle_detect:v1 /bin/bash
• 容器使用两个GPU:docker run -it --gpus 2 vehicle_detect:v1 /bin/bash
• 容器指定GPU运行:docker run -it --gpus ' "device=1,2" ' vehicle_detect:v1 /bin/bash
• docker run -it --gpus ' "device=UUID-ABCDEF,1" ' vehicle_detect:v1 /bin/bash
(8)容器的保存与导入
• 将容器保存为tar文件:docker export -o m2.tar 容器ID
• 举例:将id为a404c6c174a2的容器按日期保存为m2-20201016.tar文件
docker export -o m2-date +%Y%m%d
.tar a404c6c174a2
• 从归档文件中创建镜像:docker import m2.tar m3:v1
• 举例:从镜像归档文件m2.tar创建镜像,命名为asialee/m3:v1
docker import m2.tar asialee/m3:v1
区别:
• - docker save 保存的是镜像(image),docker export 保存的是容器(container);
• - docker load 用来载入镜像包,docker import 用来载入容器包,但两者都会恢复为镜像;
• - docker load 不能对载入的镜像重命名,而 docker import 可以为镜像指定新名称。
(9)镜像的拉取与上传
• 从镜像仓库中拉取或者更新指定镜像:docker pull
• 举例:从Docker Hub下载ubuntu最新版镜像:docker pull ubuntu
• 从Docker Hub下载REPOSITORY为ubuntu的所有镜像:docker pull -a ubuntu
• 将本地的镜像上传到镜像仓库:docker push #要先登陆到镜像仓库
• 举例:上传本地镜像m2:v1到镜像仓库中:docker push m2:v1
• 登录镜像仓库:docker login local.harbor.com 用户名/密码
• docker login -u asialee -p root12345 local.harbor.com
• 镜像上传:docker push local.harbor.com/library/ai-detect
(10)查看docker容器内的IP
• 进入容器后,查看docker容器内的IP:cat /etc/hosts
2、注意事项
镜像转文件的命令是: docker save image > /root/image.tar
文件转镜像的命令是: docker load < /root/image.tar 或者 docker load -i /root/image.tar
错误 1 是: requested load from stdin, but stdin is empty
结论 : docker load 的时候, < 误写成 >, 会出现这个错误,并且原 tar 包会被修改,修改后就不可用了
错误 2 是: open /var/lib/docker/tmp/docker-import-054903443/repositories: no such file or directory
结论 : 如果你使用过错误1 中的命令,那么即使之后的命令都是正确的,也会遇到上述错误,避免的办法是不要输错命令。倘若已经输错了,那么唯一可用的方法是删除当前的 tar 包,并且重新获取 tar 包。
二、k8s常用命令总结
kubectl是Kubernetes的命令行工具,可以让用户通过命令行的方式对Kubernetes集群进行操作,通过它与Kubernetes进行交互。通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。
1、kubectl用法概述
(1)kubectl命令行的语法如下
$ kubectl [command] [TYPE] [NAME] [flags]
• command:子命令,用于操作Kubernetes集群资源对象的命令,例如create、delete、describe、get、apply等。
• TYPE:资源对象的类型,区分大小写,能以单数形式、复数形式或者简写形式表示。例如以下3种TYPE是等价的 (1) kubectl get pod pod1 ,(2)kubectl get pods pod1,(3)kubectl get po pod1。
• NAME:资源对象的名称,区分大小写。如果不指定名称,则系统将返回属于TYPE的全部对象的列表,例如$ kubectl get pods将返回所有Pod的列表。
• flags:kubectl子命令的可选参数,例如使用“-s”指定apiserver的URL地址而不用默认值。
kubectl可操作的资源对象类型如下所示:
资源对象的名称 缩写
cluster
componentstatuses cs
configmaps cm
daemonsets ds
deployments deploy
endpoints ep
events ev
horizontalpodautoscalers hpa
ingresses ing
Jobs
limitranges limits
nodes no
namespace ns
networkpolicies
statefulsets
persistentvolumeclaims pvc
persistentvolumes pvc
pods po
podsecuritypolicies psp
podtemplate
replicasets rs
replicationcontrollers rc
resourcequotas quota
cronjob
secrets
serviceaccounts
services svc
storageclasses sc
thirdpartyresources
(2)同时对多个资源对象进行操作
在一个命令行中也可以同时对多个资源对象进行操作,以多个TYPE和NAME的组合表示,示例如下。
• 获取多个Pod的信息:kubectl get pods pod1 pod2
• 同时应用多个yaml文件,以多个-f file参数表示:kubectl create -f pod1.yaml -f rc1.yaml -f service1.yaml
2、kubectl输出格式
kubectl 命令可以用多种格式对结果进行显示,输出的格式通过-o参数指定:
$ kubectl [command] [TYPE] [NAME] -o=<output_format>
根据不同子命令的输出结果,可选的输出格式如下:
常用的输出格式示例如下:
• 显示Pod的更多信息:kubectl get pod
• 以yaml格式显示Pod的详细信息:kubectl get pod
• 以JSON格式显示Pod的详细信息:kubectl get pod
• 以自定义列名显示Pod的信息:kubectl get pod
3、kubectl常用操作
(1)创建资源对象
• 根据yaml配置文件创建资源对象:kubectl create -f ***.yaml
(2)查看资源对象
• 查看资源版本标签:kubectl api-versions
• 查看集群状态:kubectl get cs
• 查看集群节点信息:kubectl get nodes
• 查看所有Pod列表:kubectl get pods
• 查看deployment:kubectl get deploy
• 查看端口映射:kubectl get svc
• 查看指定命名空间的服务:kubectl get svc -n kubectl-system
• 显示运行中的Pod、Service、Deployment以及ReplicaSet的关键信息:kubectl get all
• 查看所有的命名空间:kubectl get all --all-namespace
(3)描述资源对象
• 显示Node的详细信息:kubectl describe nodes
• 显示Pod的详细信息:kubectl describe pods
• 显示由RC管理的Pod的信息:kubectl describe pods
(4)删除资源对象
• 基于pod.yaml定义的名称删除Pod:kubectl delete -f pod.yaml
• 删除所有Pod:kubectl delete pods --all
• 于删除名称为my_pod的Pod:kubectl delete pod my_pod
(5)执行容器命令
• 执行Pod的date命令:kubectl exec
• 指定Pod中某个容器执行date命令:kubectl exec
• 通过bash进入容器:kubectl exec -it
• kubectl exec -it container-name /bin/bash
(6)查看容器日志
• 查看容器输出到stdout的日志:kubectl logs
• 跟踪查看容器的日志,相当于tail -f命令的结果:kubectl logs -f
(7)查看集群信息
• 查看集群状态信息:kubectl cluster-info
三、nvidia-smi常用命令总结
nvidia-smi提供监控GPU使用情况和更改GPU状态的功能,是一个跨平台工具,它支持所有标准的NVIDIA驱动程序。
1、显示所有GPU的当前信息状态
命令:nvidia-smi
• Fan:风扇转速,从0到100%之间变动,N/A表示没有风扇
• Temp: GPU温度,单位摄氏度(GPU温度过高会导致GPU频率下降)
• Perf:性能状态,从P0到P12,P0表示最大性能,P12表示状态最小性能
• Pwr: GPU功耗,上方的Persistence-M:是持续模式的状态,持续模式虽然耗能大,但是在新的GPU应用启动时,花费的时间更少,这里显示的是off的状态。
• Bus-Id:GPU总线,domain🚌device.function
• Disp.A:Display Active,表示GPU的显示是否初始化
• Memory-Usage:显存使用率
• Volatile GPU-Util:GPU使用率
• ECC:是否开启错误检查和纠正技术,0/DISABLED, 1/ENABLED
• Compute M.:计算模式,0/DEFAULT,1/EXCLUSIVE_PROCESS,2/PROHIBITED
2、实时查看GPU使用情况
• 实时刷新GPU使用状态并高亮显示:watch -n 0.1 -d nvidia-smi
• 设置每 0.2s 刷新一次:watch -n 0.2 nvidia-smi
四、k8s陈述式管理
- 陈述式资源管理方法
• kubernetes 集群管理集群资源的唯一入口是通过相应的方法调用 apiserver 的接口
• kubectl 是官方的 CLI 命令行工具,用于与 apiserver 进行通信,将用户在命令行输入的命令,组织并转化为 apiserver 能识别的信息,进而实现管理 k8s 各种资源的一种有效途径
• kubectl 的命令大全
kubectl --help
k8s官方中文文档:http://docs.kubernetes.org.cn/683.html
• 对资源的增、删、查操作比较容易,但对改的操作就不容易了 - k8s相关信息查看
2.1 查看版本信息
kubectl version
[root@master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.1", GitCommit:"4485c6f18cee9a5d3c3b4e523bd27972b1b53892", GitTreeState:"clean", BuildDate:"2019-07-18T09:18:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.1", GitCommit:"4485c6f18cee9a5d3c3b4e523bd27972b1b53892", GitTreeState:"clean", BuildDate:"2019-07-18T09:09:21Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
kubectl get nodes
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 20h v1.15.1
node01 Ready20h v1.15.1
node02 Ready20h v1.15.1
2.2 查看资源对象简写
kubectl api-resources
[root@master ~]# kubectl api-resources
NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
persistentvolumeclaims pvc true PersistentVolumeClaim
persistentvolumes pv false PersistentVolume
pods po true Pod
podtemplates true PodTemplate
replicationcontrollers rc true ReplicationController
resourcequotas quota true ResourceQuota
secrets true Secret
serviceaccounts sa true ServiceAccount
services svc true Service
mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition
apiservices apiregistration.k8s.io false APIService
controllerrevisions apps true ControllerRevision
daemonsets ds apps true DaemonSet
deployments deploy apps true Deployment
replicasets rs apps true ReplicaSet
statefulsets sts apps true StatefulSet
tokenreviews authentication.k8s.io false TokenReview
localsubjectaccessreviews authorization.k8s.io true LocalSubjectAccessReview
selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview
selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview
subjectaccessreviews authorization.k8s.io false SubjectAccessReview
horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler
cronjobs cj batch true CronJob
jobs batch true Job
certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest
leases coordination.k8s.io true Lease
events ev events.k8s.io true Event
daemonsets ds extensions true DaemonSet
deployments deploy extensions true Deployment
ingresses ing extensions true Ingress
networkpolicies netpol extensions true NetworkPolicy
podsecuritypolicies psp extensions false PodSecurityPolicy
replicasets rs extensions true ReplicaSet
ingresses ing networking.k8s.io true Ingress
networkpolicies netpol networking.k8s.io true NetworkPolicy
runtimeclasses node.k8s.io false RuntimeClass
poddisruptionbudgets pdb policy true PodDisruptionBudget
podsecuritypolicies psp policy false PodSecurityPolicy
clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding
clusterroles rbac.authorization.k8s.io false ClusterRole
rolebindings rbac.authorization.k8s.io true RoleBinding
roles rbac.authorization.k8s.io true Role
priorityclasses pc scheduling.k8s.io false PriorityClass
csidrivers storage.k8s.io false CSIDriver
csinodes storage.k8s.io false CSINode
storageclasses sc storage.k8s.io false StorageClass
volumeattachments storage.k8s.io false VolumeAttachment
2.3 查看集群信息
kubectl cluster-info
[root@master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.122.10:6443
KubeDNS is running at https://192.168.122.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
2.4 配置kubectl自动补全
source <(kubectl completion bash)
[root@master ~]# source <(kubectl completion bash)
可通过TAB键实现命令补全,建议将其写入etc/profile
2.5 查看日志
journalctl -u kubelet -f
[root@master ~]# journalctl -u kubelet -f
-- Logs begin at 一 2021-11-01 18:58:09 CST. --
......
2.6 基本信息查看
kubectl get [-o wide|json|yaml] [-n namespace]
获取资源的相关信息,-n指定命名空间,-o指定输出格式
resource可以是具体资源名称,如"pod nhinx-xxx";也可以是资源类型,如“pod,node,svc,deploy”多种资源使用逗号间隔;或者all(仅展示几种核心资源,并不完整)
–all-namespaces或-A:表示显示所有命名空间
–show-labels:显示所有标签
-l app:仅显示标签为app的资源
-l app=nginx:仅显示包含app标签,且值为nginx的资源
2.6.1 查看master节点状态
kubectl get componentstatuses
kubectl get cs
[root@master ~]# kubectl get componentstatuses
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
[root@master ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
2.6.2 查看命名空间
kubectl get namespace
kubectl get ns
[root@master ~]# kubectl get namespace
NAME STATUS AGE
default Active 26h
kube-node-lease Active 26h
kube-public Active 26h
kube-system Active 26h
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 26h
kube-node-lease Active 26h
kube-public Active 26h
kube-system Active 26h
2.7 命名空间操作
2.7.1 查看default命名空间的所有资源
kubectl get all [-n default]
由于deafult为缺省空间,当不指定命名空间时默认查看default命名空间
[root@node1 ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.244.64.1
2.7.2 创建命名空间app
kubectl create ns app
[root@node1 ~]# kubectl create ns app
namespace/app created
[root@node1 ~]# kubectl get ns
NAME STATUS AGE
app Active 3s
default Active 4h37m
ingress-controller Active 4h34m
kube-node-lease Active 4h37m
kube-public Active 4h37m
kube-system Active 4h37m
kubernetes-dashboard Active 4h33m
2.7.3 删除命名空间app
kubectl delete ns app
[root@node1 ~]# kubectl delete ns app
namespace "app" deleted
[root@node1 ~]# kubectl get ns
NAME STATUS AGE
default Active 4h37m
ingress-controller Active 4h35m
kube-node-lease Active 4h37m
kube-public Active 4h37m
kube-system Active 4h37m
kubernetes-dashboard Active 4h33m
2.8 deployment/pod操作
2.8.1 在命名空间kube-public创建副本控制器(deployment)来启动Pod(nginx-test)
kubectl create deployment nginx-test --image=nginx -n kube-public
[root@node1 ~]# kubectl get pod -n kube-public
No resources found in kube-public namespace.
[root@node1 ~]# kubectl create deployment nginx-test --image=nginx -n kube-public
deployment.apps/nginx-test created
[root@node1 ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 0/1 1 0 23s
[root@node1 ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 2m24s
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-n4nks 1/1 Running 0 2m58s
2.8.2 描述某个资源的详细信息
kubectl describe deployment nginx-test -n kube-public
[root@node1 ~]# kubectl describe deployment nginx-test -n kube-public
Name: nginx-test
Namespace: kube-public
CreationTimestamp: Thu, 16 Dec 2021 20:35:23 +0800
Labels: app=nginx-test
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-test
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: nginx
Port:
Host Port:
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nginx-test-795d659f45 (1/1 replicas created)
Events:
Type Reason Age From Message
Normal ScalingReplicaSet 4m26s deployment-controller Scaled up replica set nginx-test-795d659f45 to 1
kubectl describe pod nginx-test -n kube-public
[root@node1 ~]# kubectl describe pod nginx-test -n kube-public
......
2.8.3 查看命名空间kube-public中pod信息
kubectl get pods -n kube-public
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-n4nks 1/1 Running 0 6m4s
2.8.4 登录容器
kubectl exec 可以跨主机登录容器,docker exec 只能在容器所在主机登录
[root@node1 ~]# kubectl exec -it nginx-test-795d659f45-n4nks bash -n kube-public
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-test-795d659f45-n4nks:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
2.8.5 删除(重启)pod资源
由于存在 deployment/rc 之类的副本控制器,删除 pod 也会重新拉起来
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-n4nks 1/1 Running 0 12m
[root@node1 ~]# kubectl delete pod nginx-test-795d659f45-n4nks -n kube-public
pod "nginx-test-795d659f45-n4nks" deleted
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-65pwr 1/1 Running 0 11s
2.8.6 若无法删除,总是处于terminate状态,则要强行删除pod
kubectl delete pod [] -n [] --force --grace-period=0
grace-period表示过渡存活期,默认30s,在删除pod之前允许pod慢慢终止其上的容器进程,从而优雅的退出,0表示立即终止pod
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-r2jwz 1/1 Running 0 13s
[root@node1 ~]# kubectl delete pod nginx-test-795d659f45-r2jwz -n kube-public --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nginx-test-795d659f45-r2jwz" force deleted
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 0/1 ContainerCreating 0 9s
2.8.7 扩缩容
2.8.7.1 扩容
kubectl scale deployment nginx-test --replicas=3 -n kube-public
[root@node1 ~]# kubectl scale deployment nginx-test --replicas=3 -n kube-public
deployment.apps/nginx-test scaled
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 1/1 Running 0 7m
nginx-test-795d659f45-gl6z2 0/1 ContainerCreating 0 101s
nginx-test-795d659f45-p2q9s 0/1 ContainerCreating 0 101s
2.8.7.2 缩容
kubectl scale deployment nginx-test --replicas=1 -n kube-public
[root@node1 ~]# kubectl scale deployment nginx-test --replicas=1 -n kube-public
deployment.apps/nginx-test scaled
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 1/1 Running 0 8m4s
nginx-test-795d659f45-gl6z2 0/1 Terminating 0 2m45s
nginx-test-795d659f45-p2q9s 0/1 Terminating 0 2m45s
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 1/1 Running 0 8m19s
2.8.8 删除副本控制器
kubectl delete deployment nginx-test -n kube-public
kubectl delete deployment/nginx-test -n kube-public
[root@node1 ~]# kubectl delete deployment nginx-test -n kube-public
deployment.apps "nginx-test" deleted
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 0/1 Terminating 0 8m46s
[root@node1 ~]# kubectl get pod -n kube-public
No resources found in kube-public namespace.
2.9 增加/删除label
2.9.1 增加label
kubectl label deploy nginx-test version=nginx1.14
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 19m run=nginx-test
[root@master ~]# kubectl label deploy nginx-test version=nginx1.14
deployment.extensions/nginx-test labeled
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 19m run=nginx-test,version=nginx1.14
2.9.2 删除label
kubectl label deploy nginx-test version-
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 19m run=nginx-test,version=nginx1.14
[root@master ~]# kubectl label deploy nginx-test version-
deployment.extensions/nginx-test labeled
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 20m run=nginx-test
3. K8S模拟项目
3.1 项目的生命周期
创建–>发布–>更新–>回滚–>删除
3.2 创建kubectl run命令
● 创建并运行一个或多个容器镜像
● 创建一个deployment或job来管理容器
kubectl run --help查看使用帮助
启动nginx实例,暴露容器端口80,设置副本数3
kubectl run nginx --image=nginx:1.14 --port=80 --replicas=3
[root@master ~]# kubectl run nginx --image=nginx:1.14 --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
kubectl get pods
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-65fc77987d-cwvwl 1/1 Running 0 7s
nginx-65fc77987d-m7cnn 1/1 Running 0 7s
nginx-65fc77987d-z7hvx 1/1 Running 0 7s
kubectl get all
[root@master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-65fc77987d-cwvwl 1/1 Running 0 24s
pod/nginx-65fc77987d-m7cnn 1/1 Running 0 24s
pod/nginx-65fc77987d-z7hvx 1/1 Running 0 24s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 24s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-65fc77987d 3 3 3 24s
3.3 发布kubectl expose命令
● 将资源暴露为新的Service
kubectl expose --help查看使用帮助
为Deployment的nginx创建Service,并通过Service的80端口转发至容器的80端口上,Service的名称为nginx-service,类型为NodePort
kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
3.3.1 Service的作用
Kubernetes之所以需要Service,一方面是因为Pod的IP不是固定的(Pod可能会重建),另一方面是因为一组Pod实例之间总会有负载均衡的需求。
Service通过Label Selector实现的对一组的Pod的访问。
对于容器应用而言,Kubernetes提供了基于VIP(虚拟IP)的网桥的方式访问Service,再由Service重定向到相应的Pod。
3.3.2 Service的类型
● ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(Service默认类型)
● NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过NodeIP:NodePort的方式
● LoadBalancer:通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。
3.3.3 查看Pod网络状态详细信息和Service暴露的端口
kubectl get pods,svc -o wide
[root@master ~]# kubectl get pods,svc -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-65fc77987d-cwvwl 1/1 Running 0 61s 10.244.1.25 node01
pod/nginx-65fc77987d-m7cnn 1/1 Running 0 61s 10.244.1.24 node01
pod/nginx-65fc77987d-z7hvx 1/1 Running 0 61s 10.244.2.15 node02
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.1.0.1
service/nginx-service NodePort 10.1.155.154
3.3.4 查看关联后端的节点
kubectl get endpoints
[root@master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.122.10:6443 15m
nginx-service 10.244.1.24:80,10.244.1.25:80,10.244.2.15:80 13m
3.3.5 查看service的描述信息
kubectl describe svc nginx
[root@master ~]# kubectl describe svc nginx
Name: nginx-service
Namespace: default
Labels: run=nginx
Annotations:
Selector: run=nginx
Type: NodePort
IP: 10.1.155.154
Port:
TargetPort: 80/TCP
NodePort:
Endpoints: 10.244.1.24:80,10.244.1.25:80,10.244.2.15:80
Session Affinity: None
External Traffic Policy: Cluster
Events:
3.3.6 查看负载均衡端口
在node01节点上操作
[root@node01 ~]# yum install -y ipvsadm
[root@node01 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
......
TCP 192.168.122.11:32107 rr
外部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
TCP 10.1.155.154:80 rr
pod集群组内部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
在node02节点上操作
[root@node02 ~]# yum install -y ipvsadm
[root@node02 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
......
TCP 192.168.122.12:32107 rr
外部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
TCP 10.1.155.154:80 rr
pod集群组内部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
3.3.7 访问查看
curl 10.1.155.154
[root@master ~]# curl 10.1.155.154
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
curl 192.168.122.11:32107 [root@master ~]# curl 192.168.122.11:32107Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
3.3.8 查看访问日志 [root@master ~]# kubectl logs nginx-65fc77987d-cwvwl 10.244.0.0 - - [02/Nov/2021:12:58:38 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" [root@master ~]# kubectl logs nginx-65fc77987d-m7cnn 10.244.1.1 - - [02/Nov/2021:13:00:06 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" [root@master ~]# kubectl logs nginx-65fc77987d-z7hvx 10.244.2.1 - - [02/Nov/2021:13:00:10 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 3.4 更新kubectl set ● 更改现有应用资源一些信息。 kubectl set --help查看使用帮助 3.4.1 获取修改模板 kubectl set image --help获取 [root@master ~]# kubectl set image --help ...... Examples: # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'. kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1 ...... 3.4.2 查看当前nginx的版本号 [root@master ~]# curl -I 192.168.122.11:32107 #通过报文头查看版本号 HTTP/1.1 200 OK Server: nginx/1.14.2 #nginx版本号为1.14.2 Date: Tue, 02 Nov 2021 13:10:49 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT Connection: keep-alive ETag: "5c0692e1-264" Accept-Ranges: bytes 3.4.3 将nginx版本更新为1.15 kubectl set image deployment/nginx nginx=nginx:1.15 [root@master ~]# kubectl set image deployment/nginx nginx=nginx:1.15 deployment.extensions/nginx image updated 3.4.4 监听pod状态 处于动态监听pod状态,由于使用的是滚动更新方式,所以会先生成一个新的pod,然后删除一个旧的pod,往后以此类推 kubectl get pods -w [root@master ~]# kubectl get pods -w NAME READY STATUS RESTARTS AGE nginx-65fc77987d-cwvwl 1/1 Running 0 62m nginx-65fc77987d-m7cnn 1/1 Running 0 62m nginx-65fc77987d-z7hvx 1/1 Running 0 62m nginx-6cbd4b987c-65qtx 0/1 ContainerCreating 0 23s #新建第一个pod nginx-6cbd4b987c-65qtx 1/1 Running 0 24s nginx-65fc77987d-cwvwl 1/1 Terminating 0 63m #第一个新pod运行后,删除一个旧pod nginx-6cbd4b987c-27qz7 0/1 Pending 0 0s nginx-6cbd4b987c-27qz7 0/1 Pending 0 0s nginx-6cbd4b987c-27qz7 0/1 ContainerCreating 0 0s #新建第二个pod nginx-65fc77987d-cwvwl 0/1 Terminating 0 63m nginx-65fc77987d-cwvwl 0/1 Terminating 0 63m nginx-65fc77987d-cwvwl 0/1 Terminating 0 63m nginx-6cbd4b987c-27qz7 1/1 Running 0 8s nginx-65fc77987d-m7cnn 1/1 Terminating 0 63m #第二个新pod运行后,删除第二个旧pod nginx-6cbd4b987c-m467f 0/1 Pending 0 0s nginx-6cbd4b987c-m467f 0/1 Pending 0 0s nginx-6cbd4b987c-m467f 0/1 ContainerCreating 0 0s #新建第三个pod nginx-65fc77987d-m7cnn 0/1 Terminating 0 63m nginx-6cbd4b987c-m467f 1/1 Running 0 2s nginx-65fc77987d-z7hvx 1/1 Terminating 0 63m #第三个新pod运行后,删除第三个旧pod nginx-65fc77987d-z7hvx 0/1 Terminating 0 63m nginx-65fc77987d-m7cnn 0/1 Terminating 0 63m nginx-65fc77987d-m7cnn 0/1 Terminating 0 63m nginx-65fc77987d-z7hvx 0/1 Terminating 0 63m nginx-65fc77987d-z7hvx 0/1 Terminating 0 63m更新规则可通过“kubetl describe deployment nginx”的“RollingUpdateStrategy”查看,默认配置为“25% max unavailable, 25% max surge”,即按照25%的比例进行滚动更新。
3.4.5 查看pod的ip变化
kubectl get pod -o wide
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6cbd4b987c-27qz7 1/1 Running 0 6m19s 10.244.2.16 node02
nginx-6cbd4b987c-65qtx 1/1 Running 0 6m43s 10.244.1.26 node01
nginx-6cbd4b987c-m467f 1/1 Running 0 6m11s 10.244.1.27 node01
pod更新后,ip改变
3.4.6 重新查看nginx版本号
[root@master ~]# curl -I 192.168.122.11:32107
HTTP/1.1 200 OK
Server: nginx/1.15.12
nginx版本更新为1.15.12
Date: Tue, 02 Nov 2021 13:22:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
3.5 回滚kubectl rollout
● 对资源进行回滚管理
kubectl rollout --help查看使用帮助
3.5.1 查看历史版本
kubectl rollout history deployment/nginx
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1
2
3.5.2 执行回滚到上一个版本
kubectl rollout undo deployment/nginx
[root@master ~]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx rolled back
查看pod的ip变化
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
回滚后ip再次改变
nginx-65fc77987d-2525z 1/1 Running 0 42s 10.244.2.17 node02
nginx-65fc77987d-9qkzp 1/1 Running 0 40s 10.244.2.18 node02
nginx-65fc77987d-qg75q 1/1 Running 0 41s 10.244.1.28 node01
查看当前nginx版本
[root@master ~]# curl -I 192.168.122.11:32107
HTTP/1.1 200 OK
Server: nginx/1.14.2
nginx版本回到1.14.2
Date: Tue, 02 Nov 2021 13:31:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
3.5.3 执行回滚到指定版本
查看历史版本
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
2
3
回到revison2,即1.15版本
kubectl rollout undo deployment/nginx --to-revision=2
[root@master ~]# kubectl rollout undo deployment/nginx --to-revision=2
deployment.extensions/nginx rolled back
查看pod的ip变化
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
回滚后ip再次改变
nginx-6cbd4b987c-mhqbc 1/1 Running 0 37s 10.244.1.29 node01
nginx-6cbd4b987c-tf462 1/1 Running 0 36s 10.244.2.19 node02
nginx-6cbd4b987c-z86d6 1/1 Running 0 35s 10.244.1.30 node01
查看当前nginx版本
[root@master ~]# curl -I 192.168.122.11:32107
HTTP/1.1 200 OK
Server: nginx/1.15.12
nginx版本回到1.15.12
Date: Tue, 02 Nov 2021 13:36:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
3.5.4 检查回滚状态
kubectl rollout status deployment/nginx
[root@master ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
3.6 删除kubectl delete
3.6.1 删除副本控制器
kubectl delete deployment/nginx
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 88m
[root@master ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl get deploy
No resources found.
3.6.2 删除service
kubectl delete svc/nginx-service
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1
nginx-service NodePort 10.1.155.154
[root@master ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1
[root@master ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1
4. 金丝雀发布/灰度发布(Canary Release)
4.1 金丝雀发布简介
Deployment控制器支持自定义控制更新过程中的滚动节奏,如“暂停(pause)”或“继续(resume)”更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,在筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
4.2 更新deployment的版本,并配置暂停deployment
4.2.1 创建pods
kubectl run nginx-test --image=nginx:1.14 --replicas=3
[root@master ~]# kubectl run nginx-test --image=nginx:1.14 --replicas=3
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-test created
[root@master ~]# kubectl get pods,deploy -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 51s 10.244.2.23 node02
pod/nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 51s 10.244.1.37 node01
pod/nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 51s 10.244.1.38 node01
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.extensions/nginx-test 3/3 3 3 51s nginx-test nginx:1.14 run=nginx-test
4.2.2 发布服务
kubectl expose deploy nginx-test --port=80 --target-port=80 --name=nginx-service --type=NodePort
[root@master ~]# kubectl expose deploy nginx-test --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@master ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.1.0.1
nginx-service NodePort 10.1.36.134
4.2.3 查看nginx版本
[root@master ~]# curl -I 192.168.122.11:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
版本号为1.14.2
Date: Wed, 03 Nov 2021 11:54:11 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
4.2.4 定义版本CHANGE-CAUSE
4.2.4.1 查看历史版本
在不定义CHANGE-CAUSE的情况下,缺省值为,当历史版本较多时,不便于咱们回滚时辨认版本号。因此,建议定义CHANGE-CAUSE为服务版本以帮助咱们辨认当前服务。
kubectl rollout history deploy/nginx-test
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1
4.2.4.2 定义版本CHANGE-CAUSE
一般通过修改配置的方式定义change-cause
[root@master ~]# kubectl edit deploy/nginx-test
......
kind: Deployment
metadata:
annotations:
下行可定义历史版本revision
deployment.kubernetes.io/revision: "1"
在Deployment的matadata项下的annotations中如下行定义change-cause
kubernetes.io/change-cause: "nginx1.14"
......
4.2.4.3 再次查看历史版本
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
4.2.5 更新nginx版本为1.15并配置暂停
kubectl set image deploy/nginx-test nginx-test=nginx:1.15 && kubectl rollout pause deploy/nginx-test
[root@master ~]# kubectl set image deploy/nginx-test nginx-test=nginx:1.15 && kubectl rollout pause deploy/nginx-test
deployment.extensions/nginx-test image updated
deployment.extensions/nginx-test paused
4.2.6 观察更新状态
kubectl rollout status deploy/nginx-test
[root@master ~]# kubectl rollout status deploy/nginx-test
Waiting for deployment "nginx-test" rollout to finish: 1 out of 3 new replicas have been updated...
4.2.7 监控更新的过程
可以看到已经新增了一个pod,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令
kubectl get pods -w
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-679dcbd68d-zw8w5 1/1 Running 0 8m28s 10.244.2.24 node02
nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 48m 10.244.2.23 node02
nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 48m 10.244.1.37 node01
nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 48m 10.244.1.38 node01
4.2.8 查看nginx版本
kubectl get pod -o wide
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-679dcbd68d-zw8w5 1/1 Running 0 8m28s 10.244.2.24 node02
nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 48m 10.244.2.23 node02
nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 48m 10.244.1.37 node01
nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 48m 10.244.1.38 node01
查看nginx版本
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 03 Nov 2021 12:45:37 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 03 Nov 2021 12:46:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 03 Nov 2021 12:47:09 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.15.12
新pod为1.15
Date: Wed, 03 Nov 2021 12:47:12 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
4.2.9 查看并更新历史版本change-cause
kubectl rollout history deploy/nginx-test
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
2 nginx1.14
kubectl edit deploy/nginx-test
[root@master ~]# kubectl edit deploy/nginx-test
kind: Deployment
metadata:
annotations:
下行的revison自动更新为2
deployment.kubernetes.io/revision: "2"
修改下行的change-cause为nginx1.15
kubernetes.io/change-cause: nginx1.15
kubectl rollout history deploy/nginx-test
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
2 nginx1.15
4.2.10 resume继续更新
测试新版本没问题继续更新
kubectl rollout resume deploy/nginx-test
[root@master ~]# kubectl rollout resume deploy/nginx-test
deployment.extensions/nginx-test resumed
4.2.11 查看最后的更新情况
[root@master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-test-679dcbd68d-zw8w5 1/1 Running 0 61s
nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 41m
nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 41m
nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 41m
nginx-test-6cc7cd5547-gcbh5 1/1 Terminating 0 63m
nginx-test-679dcbd68d-cljzh 0/1 Pending 0 0s
nginx-test-679dcbd68d-cljzh 0/1 Pending 0 0s
nginx-test-679dcbd68d-cljzh 0/1 ContainerCreating 0 0s
nginx-test-6cc7cd5547-gcbh5 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-gcbh5 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-gcbh5 0/1 Terminating 0 63m
nginx-test-679dcbd68d-cljzh 1/1 Running 0 1s
nginx-test-6cc7cd5547-nw6k6 1/1 Terminating 0 63m
nginx-test-679dcbd68d-s2gck 0/1 Pending 0 0s
nginx-test-679dcbd68d-s2gck 0/1 Pending 0 0s
nginx-test-679dcbd68d-s2gck 0/1 ContainerCreating 0 0s
nginx-test-679dcbd68d-s2gck 1/1 Running 0 1s
nginx-test-6cc7cd5547-4xqcb 1/1 Terminating 0 63m
nginx-test-6cc7cd5547-nw6k6 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-4xqcb 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-nw6k6 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-nw6k6 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-4xqcb 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-4xqcb 0/1 Terminating 0 63m
五、声明式管理
- 声明式管理方法
- 适合于对资源的修改操作
- 声明式资源管理方法依赖于资源配置清明文件对资源进行管理
资源配置清单文件有两种格式:yaml(人性化,易读),json(易于api接口解析) - 对资源的观念里,是通过实现定义在同一资源配置清单内,再通过陈述式命令应用到k8s集群里
- 语法格式:kubectl create/apply/delete -f -o yaml
- 查看资源配置清单
kubectl get deploy/nginx-test -o yaml
[root@master ~]# kubectl get deploy/nginx-test -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
kubernetes.io/change-cause: nginx1.14
creationTimestamp: "2021-11-03T11:47:56Z"
generation: 7
labels:
run: nginx-test
name: nginx-test
namespace: default
resourceVersion: "105895"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-test
uid: 634d471e-3907-4717-a02e-f5cce101d2f4
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
run: nginx-test
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: nginx-test
spec:
containers:- image: nginx:1.14
imagePullPolicy: IfNotPresent
name: nginx-test
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 3
conditions:
- image: nginx:1.14
- lastTransitionTime: "2021-11-03T11:47:58Z"
lastUpdateTime: "2021-11-03T11:47:58Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available - lastTransitionTime: "2021-11-03T12:50:56Z"
lastUpdateTime: "2021-11-03T12:57:35Z"
message: ReplicaSet "nginx-test-6cc7cd5547" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 7
readyReplicas: 3
replicas: 3
updatedReplicas: 3
kubectl get service nginx-service -o yaml
[root@master ~]# kubectl get service nginx-service -o yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-11-03T11:52:40Z"
labels:
run: nginx-test
name: nginx-service
namespace: default
resourceVersion: "100051"
selfLink: /api/v1/namespaces/default/services/nginx-service
uid: b2ec9834-864a-4146-9296-5420ac15451d
spec:
clusterIP: 10.1.36.134
externalTrafficPolicy: Cluster
ports:
- nodePort: 30191
port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx-test
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
- 解释资源配置清单
kubectl explain deployment.metadata
[root@master ~]# kubectl explain deployment.metadata
KIND: Deployment
VERSION: extensions/v1beta1
RESOURCE: metadata