二、Kubernetes中pod的管理及优化

news2024/9/20 8:14:01

目录

一 kubernetes 中的资源

1.1 资源管理介绍

1.2 资源管理方式

1.2.1 命令式对象管理

1.2.2 资源类型

1.2.3 基本命令示例

1.2.4 运行和调试命令示例

1.2.5 高级命令示例

二 什么是pod

2.1 创建自主式pod (生产不推荐)

2.2 利用控制器管理pod(推荐)

2.3 应用版本的更新

2.4 利用yaml文件部署应用

2.4.1 用yaml文件部署应用有以下优点

2.4.2 资源清单参数

2.4.3 如何获得资源帮助

2.4.4 编写示例

2.4.4.1 示例1:运行简单的单个容器pod

2.4.4.2 示例2:运行多个容器pod

2.4.4.3 示例3:理解pod间的网络整合

2.4.4.4 示例4:端口映射

2.4.4.5 示例5:如何设定环境变量

2.4.4.6 示例6:资源限制

2.4.4.7 示例7 容器启动管理

2.4.4.8 示例8 选择运行节点

2.4.4.9 示例9 共享宿主机网络

三 pod的生命周期

3.1 INIT 容器

3.1.1 INIT 容器的功能

3.1.2 INIT 容器示例

3.2 探针

3.2.1 探针实例

3.2.1.1 存活探针示例:


一 kubernetes 中的资源

1.1 资源管理介绍

  • 在kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes。

  • kubernetes的本质上就是一个集群系统,用户可以在集群中部署各种服务

  • 所谓的部署服务,其实就是在kubernetes集群中运行一个个的容器,并将指定的程序跑在容器中。

  • kubernetes的最小管理单元是pod而不是容器,只能将容器放在Pod中,

  • kubernetes一般也不会直接管理Pod,而是通过Pod控制器来管理Pod的。

  • Pod中服务服务的访问是由kubernetes提供的Service资源来实现。

  • Pod中程序的数据需要持久化是由kubernetes提供的各种存储系统来实现

1.2 资源管理方式

  • 命令式对象管理:直接使用命令去操作kubernetes资源

    kubectl run nginx-pod --image=nginx:latest --port=80

  • 命令式对象配置:通过命令配置和配置文件去操作kubernetes资源

    kubectl create/patch -f nginx-pod.yaml

  • 声明式对象配置:通过apply命令和配置文件去操作kubernetes资源

    kubectl apply -f nginx-pod.yaml

类型适用环境优点缺点
命令式对象管理测试简单只能操作活动对象,无法审计、跟踪
命令式对象配置开发可以审计、跟踪项目大时,配置文件多,操作麻烦
声明式对象配置开发支持目录操作意外情况下难以调试

1.2.1 命令式对象管理

kubectl是kubernetes集群的命令行工具,通过它能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署

kubectl命令的语法如下:

kubectl [command] [type] [name] [flags]

comand:指定要对资源执行的操作,例如create、get、delete

type:指定资源类型,比如deployment、pod、service

name:指定资源的名称,名称大小写敏感

flags:指定额外的可选参数

# 查看所有pod
kubectl get pod 
​
# 查看某个pod
kubectl get pod pod_name
​
# 查看某个pod,以yaml格式展示结果
kubectl get pod pod_name -o yaml

1.2.2 资源类型

kubernetes中所有的内容都抽象为资源

kubectl api-resources

常用资源类型

kubect 常见命令操作

1.2.3 基本命令示例

kubectl的详细说明地址:Kubectl Reference Docs

#显示集群版本
[root@k8s-master ~]# kubectl version 
Client Version: v1.30.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.30.0

#显示集群信息
[root@k8s-master ~]# kubectl cluster-info 
Kubernetes control plane is running at https://172.25.254.100:6443
CoreDNS is running at https://172.25.254.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

#创建一个webcluster控制器,控制器中pod数量为2
[root@k8s-master ~]# kubectl create deployment web --image www.test.com/luohailin/nginx --replicas 2
deployment.apps/web created

#查看控制器
[root@k8s-master ~]# kubectl get deployments.apps 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    2/2     2            2           5s

#查看资源帮助
[root@k8s-master ~]# kubectl explain deployment
GROUP:      apps
KIND:       Deployment
VERSION:    v1

DESCRIPTION:
    Deployment enables declarative updates for Pods and ReplicaSets.

FIELDS:
  apiVersion    <string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

  kind  <string>
    Kind is a string value representing the REST resource this object
    represents. Servers may infer this from the endpoint the client submits
    requests to. Cannot be updated. In CamelCase. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

  metadata      <ObjectMeta>
    Standard object's metadata. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

  spec  <DeploymentSpec>
    Specification of the desired behavior of the Deployment.

  status        <DeploymentStatus>
    Most recently observed status of the Deployment.

#查看控制器参数帮助 
[root@k8s-master ~]# kubectl explain deployment.spec
GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: spec <DeploymentSpec>


DESCRIPTION:
    Specification of the desired behavior of the Deployment.
    DeploymentSpec is the specification of the desired behavior of the
    Deployment.

FIELDS:
  minReadySeconds       <integer>
    Minimum number of seconds for which a newly created pod should be ready
    without any of its container crashing, for it to be considered available.
    Defaults to 0 (pod will be considered available as soon as it is ready)

  paused        <boolean>
    Indicates that the deployment is paused.

  progressDeadlineSeconds       <integer>
    The maximum time in seconds for a deployment to make progress before it is
    considered to be failed. The deployment controller will continue to process
    failed deployments and a condition with a ProgressDeadlineExceeded reason
    will be surfaced in the deployment status. Note that progress will not be
    estimated during the time a deployment is paused. Defaults to 600s.

  replicas      <integer>
    Number of desired pods. This is a pointer to distinguish between explicit
    zero and not specified. Defaults to 1.

  revisionHistoryLimit  <integer>
    The number of old ReplicaSets to retain to allow rollback. This is a pointer
    to distinguish between explicit zero and not specified. Defaults to 10.

  selector      <LabelSelector> -required-
    Label selector for pods. Existing ReplicaSets whose pods are selected by
    this will be the ones affected by this deployment. It must match the pod
    template's labels.

  strategy      <DeploymentStrategy>
    The deployment strategy to use to replace existing pods with new ones.

  template      <PodTemplateSpec> -required-
    Template describes the pods that will be created. The only allowed
    template.spec.restartPolicy value is "Always".

#编辑控制器配置
[root@k8s-master ~]# kubectl edit deployments.apps web
@@@@省略内容@@@@@@
spec:
  progressDeadlineSeconds: 600
  replicas: 2

[root@k8s-master ~]# kubectl get deployments.apps
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    2/2     2            2           73m

#利用补丁更改控制器配置
[root@k8s-master ~]# kubectl patch deployments.apps web -p '{"spec":{"replicas":4}}'
deployment.apps/web patched
[root@k8s-master ~]# kubectl get deployments.apps 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    4/4     4            4           7m46s

#删除资源
[root@k8s-master ~]# kubectl delete deployments.apps web
deployment.apps "web" deleted
[root@k8s-master ~]# kubectl get deployments.apps 
No resources found in default namespace.

1.2.4 运行和调试命令示例

#运行pod
[root@k8s-master ~]# kubectl run testpod --image nginx
pod/testpod created
[root@k8s-master ~]# kubectl get pods 
NAME      READY   STATUS    RESTARTS   AGE
testpod   1/1     Running   0          12s


#端口暴露
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   18h
[root@k8s-master ~]# kubectl expose pod testpod --port 80 --target-port 80
service/testpod exposed
[root@k8s-master ~]# kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   18h
testpod      ClusterIP   10.110.40.210   <none>        80/TCP    11s
[root@k8s-master ~]# curl 10.110.40.210
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#查看日志
[root@k8s-master ~]# kubectl logs pods/testpod 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/09/03 02:55:14 [notice] 1#1: using the "epoll" event method
2024/09/03 02:55:14 [notice] 1#1: nginx/1.27.1
2024/09/03 02:55:14 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2024/09/03 02:55:14 [notice] 1#1: OS: Linux 5.14.0-427.13.1.el9_4.x86_64
2024/09/03 02:55:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1073741816:1073741816
2024/09/03 02:55:14 [notice] 1#1: start worker processes
2024/09/03 02:55:14 [notice] 1#1: start worker process 30
2024/09/03 02:55:14 [notice] 1#1: start worker process 31
10.244.0.0 - - [03/Sep/2024:02:56:34 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.76.1" "-"


#运行交互pod
[root@k8s-master ~]# kubectl run -it testpod --image www.test.com/luohailin/busybox
If you don't see a command prompt, try pressing enter.
/ # 
/ # 
/ # Session ended, resume using 'kubectl attach testpod -c testpod -i -t' command when the pod is running

#运行非交互pod
[root@k8s-master ~]# kubectl run nginx --image www.test.com/luohailin/nginx
pod/nginx created

#进入到已经运行的容器,且容器有交互环境
[root@k8s-master ~]# kubectl attach pods/testpod -it
If you don't see a command prompt, try pressing enter.
/ # 
/ # 
/ # Session ended, resume using 'kubectl attach testpod -c testpod -i -t' command when the pod is running

#在已经运行的pod中运行指定命令
[root@k8s-master ~]# kubectl exec -it pods/nginx /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx:/# 

#日志文件到pod中
[root@k8s-master ~]# kubectl exec -it pods/nginx /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx:/# ls
anaconda-ks.cfg  dev		       etc   lib64  opt   run	sys  var
bin		 docker-entrypoint.d   home  media  proc  sbin	tmp
boot		 docker-entrypoint.sh  lib   mnt    root  srv	usr
root@nginx:/# 

#复制pod中的文件到本机
[root@k8s-master ~]# kubectl cp nginx:/anaconda-ks.cfg anaconda-ks.cfg 
tar: Removing leading `/' from member names

1.2.5 高级命令示例

#利用命令生成yam1模板文件
[root@k8s-master ~]# kubectl create deployment --image nginx web --dry-run=client -o yaml > web.yml

#利用yam1文件生成资源
[root@k8s-master ~]# cat web.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
[root@k8s-master ~]# kubectl apply -f web.yml 
deployment.apps/web created
[root@k8s-master ~]# kubectl get deployments.apps 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    2/2     2            2           12s
[root@k8s-master ~]# kubectl delete -f web.yml 
deployment.apps "web" deleted


#管理资源标签
[root@k8s-master ~]# kubectl run nginx --image nginx
pod/nginx created
[root@k8s-master ~]# kubectl get pods --show-labels 
NAME      READY   STATUS    RESTARTS   AGE   LABELS
nginx     1/1     Running   0          24s   run=nginx
testpod   1/1     Running   0          43m   run=testpod
[root@k8s-master ~]# kubectl label pods nginx app=redhat
pod/nginx labeled
[root@k8s-master ~]# kubectl get pods --show-labels 
NAME      READY   STATUS    RESTARTS   AGE   LABELS
nginx     1/1     Running   0          67s   app=redhat,run=nginx
testpod   1/1     Running   0          44m   run=testpod

#更改标签
[root@k8s-master ~]# kubectl label pods nginx app=web --overwrite
pod/nginx labeled
[root@k8s-master ~]# kubectl get pods --show-labels 
NAME      READY   STATUS    RESTARTS   AGE    LABELS
nginx     1/1     Running   0          107s   app=web,run=nginx
testpod   1/1     Running   0          45m    run=testpod


#删除标签
[root@k8s-master ~]# kubectl label pods nginx app-
pod/nginx unlabeled
[root@k8s-master ~]# kubectl get pods --show-labels 
NAME      READY   STATUS    RESTARTS   AGE     LABELS
nginx     1/1     Running   0          2m57s   run=nginx
testpod   1/1     Running   0          46m     run=testpod

#标签时控制器识别pod示例的标识
[root@k8s-master ~]# kubectl get pods --show-labels
NAME                          READY   STATUS    RESTARTS   AGE     LABELS
nginx                         1/1     Running   0          11m     run=nginx
webcluster-7c584f774b-66zbd   1/1     Running   0          2m10s   app=webcluster,pod-template-hash=7c584f774b
webcluster-7c584f774b-9x2x2   1/1     Running   0          35m     app=webcluster,pod-template-hash=7c584f774b

#删除pod上的标签
root@k8s-master ~]# kubectl label pods webcluster-7c584f774b-66zbd app-
pod/webcluster-7c584f774b-66zbd unlabeled

#控制器会重新启动新pod
[root@k8s-master ~]# kubectl get pods --show-labels
NAME                          READY   STATUS    RESTARTS   AGE     LABELS
nginx                         1/1     Running   0          11m     run=nginx
webcluster-7c584f774b-66zbd   1/1     Running   0          2m39s   pod-template-hash=7c584f774b
webcluster-7c584f774b-9x2x2   1/1     Running   0          36m     app=webcluster,pod-template-hash=7c584f774b
webcluster-7c584f774b-hgprn   1/1     Running   0          2s      app=webcluster,pod-template-hash=7c584f774b

二 什么是pod

  • Pod是可以创建和管理Kubernetes计算的最小可部署单元

  • 一个Pod代表着集群中运行的一个进程,每个pod都有一个唯一的ip。

  • 一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker)

  • 多个容器间共享IPC、Network和UTC namespace。

2.1 创建自主式pod (生产不推荐)

优点:

灵活性高

  • 可以精确控制 Pod 的各种配置参数,包括容器的镜像、资源限制、环境变量、命令和参数等,满足特定的应用需求。

学习和调试方便

  • 对于学习 Kubernetes 的原理和机制非常有帮助,通过手动创建 Pod 可以深入了解 Pod 的结构和配置方式。在调试问题时,可以更直接地观察和调整 Pod 的设置。

适用于特殊场景

  • 在一些特殊情况下,如进行一次性任务、快速验证概念或在资源受限的环境中进行特定配置时,手动创建 Pod 可能是一种有效的方式。

缺点:

管理复杂

  • 如果需要管理大量的 Pod,手动创建和维护会变得非常繁琐和耗时。难以实现自动化的扩缩容、故障恢复等操作。

缺乏高级功能

  • 无法自动享受 Kubernetes 提供的高级功能,如自动部署、滚动更新、服务发现等。这可能导致应用的部署和管理效率低下。

可维护性差

  • 手动创建的 Pod 在更新应用版本或修改配置时需要手动干预,容易出现错误,并且难以保证一致性。相比之下,通过声明式配置或使用 Kubernetes 的部署工具可以更方便地进行应用的维护和更新。

#查看所有pods
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.

#建立一个名为timinglee的pod
[root@k8s-master ~]# kubectl run test --image nginx
pod/timinglee created


[root@k8s-master ~]# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
test         1/1     Running   0          6s

#显示pod的较为详细的信息
[root@k8s-master ~]# kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP            NODE                      NOMINATED NODE   READINESS GATES
timinglee   1/1     Running   0          11s   10.244.1.17   k8s-node1.exam.com        <none>           <none>

2.2 利用控制器管理pod(推荐)

高可用性和可靠性

  • 自动故障恢复:如果一个 Pod 失败或被删除,控制器会自动创建新的 Pod 来维持期望的副本数量。确保应用始终处于可用状态,减少因单个 Pod 故障导致的服务中断。

  • 健康检查和自愈:可以配置控制器对 Pod 进行健康检查(如存活探针和就绪探针)。如果 Pod 不健康,控制器会采取适当的行动,如重启 Pod 或删除并重新创建它,以保证应用的正常运行。

可扩展性

  • 轻松扩缩容:可以通过简单的命令或配置更改来增加或减少 Pod 的数量,以满足不同的工作负载需求。例如,在高流量期间可以快速扩展以处理更多请求,在低流量期间可以缩容以节省资源。

  • 水平自动扩缩容(HPA):可以基于自定义指标(如 CPU 利用率、内存使用情况或应用特定的指标)自动调整 Pod 的数量,实现动态的资源分配和成本优化。

版本管理和更新

  • 滚动更新:对于 Deployment 等控制器,可以执行滚动更新来逐步替换旧版本的 Pod 为新版本,确保应用在更新过程中始终保持可用。可以控制更新的速率和策略,以减少对用户的影响。

  • 回滚:如果更新出现问题,可以轻松回滚到上一个稳定版本,保证应用的稳定性和可靠性。

声明式配置

  • 简洁的配置方式:使用 YAML 或 JSON 格式的声明式配置文件来定义应用的部署需求。这种方式使得配置易于理解、维护和版本控制,同时也方便团队协作。

  • 期望状态管理:只需要定义应用的期望状态(如副本数量、容器镜像等),控制器会自动调整实际状态与期望状态保持一致。无需手动管理每个 Pod 的创建和删除,提高了管理效率。

服务发现和负载均衡

  • 自动注册和发现:Kubernetes 中的服务(Service)可以自动发现由控制器管理的 Pod,并将流量路由到它们。这使得应用的服务发现和负载均衡变得简单和可靠,无需手动配置负载均衡器。

  • 流量分发:可以根据不同的策略(如轮询、随机等)将请求分发到不同的 Pod,提高应用的性能和可用性。

多环境一致性

  • 一致的部署方式:在不同的环境(如开发、测试、生产)中,可以使用相同的控制器和配置来部署应用,确保应用在不同环境中的行为一致。这有助于减少部署差异和错误,提高开发和运维效率。

示例:

#建立控制器并自动运行pod
[root@k8s-master ~]# kubectl create deployment test --image nginx
deployment.apps/test created
[root@k8s-master ~]# kubectl get pods
NAME                   READY   STATUS              RESTARTS   AGE
test                   1/1     Running             0          75s
test-7895cc554-4n9b9   0/1     ContainerCreating   0          5s

#为test扩容
[root@k8s-master ~]# kubectl scale deployment test --replicas 6
deployment.apps/test scaled
[root@k8s-master ~]# kubectl get pods
NAME                   READY   STATUS              RESTARTS   AGE
test                   1/1     Running             0          96s
test-7895cc554-4n9b9   1/1     Running             0          26s
test-7895cc554-76thx   0/1     ContainerCreating   0          3s
test-7895cc554-blqxb   0/1     ContainerCreating   0          3s
test-7895cc554-cn9pg   0/1     ContainerCreating   0          3s
test-7895cc554-l24tp   0/1     ContainerCreating   0          3s
test-7895cc554-wqwz4   0/1     ContainerCreating   0          3s

#为test缩容
[root@k8s-master ~]# kubectl scale deployment test --replicas 2
deployment.apps/test scaled
[root@k8s-master ~]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
test                   1/1     Running   0          2m8s
test-7895cc554-4n9b9   1/1     Running   0          58s
test-7895cc554-wqwz4   1/1     Running   0          35s

2.3 应用版本的更新

#利用控制器建立pod
[root@k8s-master ~]# kubectl create  deployment test --image www.test.com/luohailin/myapp:v1 --replicas 2
deployment.apps/test created

#暴漏端口
[root@k8s-master ~]# kubectl expose deployment test --port 80 --target-port 80
[root@k8s-master ~]# kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   2d4h
test         ClusterIP   10.109.25.167   <none>        80/TCP    1s

#访问服务
[root@k8s-master ~]# curl 10.98.103.206
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl 10.98.103.206
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

#产看历史版本
[root@k8s-master ~]# kubectl rollout history deployment test 
deployment.apps/test 
REVISION  CHANGE-CAUSE
1         <none>

#更新控制器镜像版本
[root@k8s-master ~]#  kubectl set image deployments/test myapp=myapp:v2
deployment.apps/test image updated

#查看历史版本
[root@k8s-master ~]# kubectl rollout history deployment test 
deployment.apps/test 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

#访问内容测试
[root@k8s-master ~]# curl 10.109.25.167
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

#版本回滚
[root@k8s-master ~]#  kubectl rollout undo deployment test --to-revision 1
deployment.apps/test rolled back

2.4 利用yaml文件部署应用

2.4.1 用yaml文件部署应用有以下优点

声明式配置

  • 清晰表达期望状态:以声明式的方式描述应用的部署需求,包括副本数量、容器配置、网络设置等。这使得配置易于理解和维护,并且可以方便地查看应用的预期状态。

  • 可重复性和版本控制:配置文件可以被版本控制,确保在不同环境中的部署一致性。可以轻松回滚到以前的版本或在不同环境中重复使用相同的配置。

  • 团队协作:便于团队成员之间共享和协作,大家可以对配置文件进行审查和修改,提高部署的可靠性和稳定性。

灵活性和可扩展性

  • 丰富的配置选项:可以通过 YAML 文件详细地配置各种 Kubernetes 资源,如 Deployment、Service、ConfigMap、Secret 等。可以根据应用的特定需求进行高度定制化。

  • 组合和扩展:可以将多个资源的配置组合在一个或多个 YAML 文件中,实现复杂的应用部署架构。同时,可以轻松地添加新的资源或修改现有资源以满足不断变化的需求。

与工具集成

  • 与 CI/CD 流程集成:可以将 YAML 配置文件与持续集成和持续部署(CI/CD)工具集成,实现自动化的应用部署。例如,可以在代码提交后自动触发部署流程,使用配置文件来部署应用到不同的环境。

  • 命令行工具支持:Kubernetes 的命令行工具 kubectl 对 YAML 配置文件有很好的支持,可以方便地应用、更新和删除配置。同时,还可以使用其他工具来验证和分析 YAML 配置文件,确保其正确性和安全性。

2.4.2 资源清单参数

参数名称类型参数说明
versionString这里是指的是K8S API的版本,目前基本上是v1,可以用kubectl api-versions命令查询
kindString这里指的是yaml文件定义的资源类型和角色,比如:Pod
metadataObject元数据对象,固定值就写metadata
metadata.nameString元数据对象的名字,这里由我们编写,比如命名Pod的名字
metadata.namespaceString元数据对象的命名空间,由我们自身定义
SpecObject详细定义对象,固定值就写Spec
spec.containers[]list这里是Spec对象的容器列表定义,是个列表
spec.containers[].nameString这里定义容器的名字
spec.containers[].imagestring这里定义要用到的镜像名称
spec.containers[].imagePullPolicyString定义镜像拉取策略,有三个值可选: (1) Always: 每次都尝试重新拉取镜像 (2) IfNotPresent:如果本地有镜像就使用本地镜像 (3) )Never:表示仅使用本地镜像
spec.containers[].command[]list指定容器运行时启动的命令,若未指定则运行容器打包时指定的命令
spec.containers[].args[]list指定容器运行参数,可以指定多个
spec.containers[].workingDirString指定容器工作目录
spec.containers[].volumeMounts[]list指定容器内部的存储卷配置
spec.containers[].volumeMounts[].nameString指定可以被容器挂载的存储卷的名称
spec.containers[].volumeMounts[].mountPathString指定可以被容器挂载的存储卷的路径
spec.containers[].volumeMounts[].readOnlyString设置存储卷路径的读写模式,ture或false,默认为读写模式
spec.containers[].ports[]list指定容器需要用到的端口列表
spec.containers[].ports[].nameString指定端口名称
spec.containers[].ports[].containerPortString指定容器需要监听的端口号
spec.containers[] ports[].hostPortString指定容器所在主机需要监听的端口号,默认跟上面containerPort相同,注意设置了hostPort同一台主机无法启动该容器的相同副本(因为主机的端口号不能相同,这样会冲突)
spec.containers[].ports[].protocolString指定端口协议,支持TCP和UDP,默认值为 TCP
spec.containers[].env[]list指定容器运行前需设置的环境变量列表
spec.containers[].env[].nameString指定环境变量名称
spec.containers[].env[].valueString指定环境变量值
spec.containers[].resourcesObject指定资源限制和资源请求的值(这里开始就是设置容器的资源上限)
spec.containers[].resources.limitsObject指定设置容器运行时资源的运行上限
spec.containers[].resources.limits.cpuString指定CPU的限制,单位为核心数,1=1000m
spec.containers[].resources.limits.memoryString指定MEM内存的限制,单位为MIB、GiB
spec.containers[].resources.requestsObject指定容器启动和调度时的限制设置
spec.containers[].resources.requests.cpuStringCPU请求,单位为core数,容器启动时初始化可用数量
spec.containers[].resources.requests.memoryString内存请求,单位为MIB、GIB,容器启动的初始化可用数量
spec.restartPolicystring定义Pod的重启策略,默认值为Always. (1)Always: Pod-旦终止运行,无论容器是如何 终止的,kubelet服务都将重启它 (2)OnFailure: 只有Pod以非零退出码终止时,kubelet才会重启该容器。如果容器正常结束(退出码为0),则kubelet将不会重启它 (3) Never: Pod终止后,kubelet将退出码报告给Master,不会重启该
spec.nodeSelectorObject定义Node的Label过滤标签,以key:value格式指定
spec.imagePullSecretsObject定义pull镜像时使用secret名称,以name:secretkey格式指定
spec.hostNetworkBoolean定义是否使用主机网络模式,默认值为false。设置true表示使用宿主机网络,不使用docker网桥,同时设置了true将无法在同一台宿主机 上启动第二个副本

2.4.3 如何获得资源帮助

kubectl explain pod.spec.containers

2.4.4 编写示例

2.4.4.1 示例1:运行简单的单个容器pod

用命令获取yaml模板

[root@k8s-master ~]# kubectl run test --image luohailin/myapp:v1 --dry-run=client -o yaml > pod.yml
[root@k8s-master ~]# vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: timing			#pod标签
  name: test		#pod名称
spec:
  containers:
  - image: luohailin/myapp:v1		#pod镜像
    name: test		#容器名称
2.4.4.2 示例2:运行多个容器pod

[!WARNING]

注意如果多个容器运行在一个pod中,资源共享的同时在使用相同资源时也会干扰,比如端口

#一个端口干扰示例:
[root@k8s-master ~]# vim pod.yml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: timing
  name: test
spec:
  containers:
    - image:  luohailin/nginx:latest
      name: web1

    - image: luohailin/nginx:latest
      name: web2
      
[root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created

[root@k8s-master ~]# kubectl get pods
NAME        READY   STATUS   RESTARTS      AGE
test        1/2     Error    1 (14s ago)   18s

#查看日志
[root@k8s-master ~]# kubectl logs test web2
2024/08/31 12:43:20 [emerg] 1#1: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2024/08/31 12:43:20 [notice] 1#1: try again to bind() after 500ms
2024/08/31 12:43:20 [emerg] 1#1: still could not bind()
nginx: [emerg] still could not bind()

在一个pod中开启多个容器时一定要确保容器彼此不能互相干扰

[root@k8s-master ~]# vim pod.yml

[root@k8s-master ~]# kubectl apply -f pod.yml
pod/timinglee created
apiVersion: v1
kind: Pod 
metadata:
  labels:
    run: timing
  name: test
spec:
  containers:
    - image: luohailin/nginx:latest
      name: web1

    - image: luohailin/busybox:latest
      name: busybox
      command: ["/bin/sh","-c","sleep 1000000"]

[root@k8s-master ~]# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
test        2/2     Running   0          19s
2.4.4.3 示例3:理解pod间的网络整合

同在一个pod中的容器公用一个网络

[root@k8s-master ~]# vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  containers:
  - image: www.test.com/luohailin/myapp:v1
    name: test
  - image: www.test.com/luohailin/busybox:latest
    name: busybox
    command: ["/bin/sh","-c","sleep 1000000"]

[root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created
[root@k8s-master ~]# kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
test   2/2     Running   0          4s
[root@k8s-master ~]# kubectl exec test -c busybox -- wget -qO- localhost
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

2.4.4.4 示例4:端口映射
[root@k8s-master ~]# vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  containers:
  - image: www.test.com/luohailin/myapp:v1
    name: test
    ports:
    - name: webport
      containerPort: 80
      hostPort: 80
      protocol: TCP

[root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created
[root@k8s-master ~]# kubectl get pods  -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP            NODE                 NOMINATED NODE   READINESS GATES
test   1/1     Running   0          13s   10.244.3.48   k8s-node2.exam.com   <none>           <none>
[root@k8s-master ~]# 
[root@k8s-master ~]# curl k8s-node2.exam.com
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

2.4.4.5 示例5:如何设定环境变量
[root@k8s-master ~]# vim  pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  containers:
  - image: www.test.com/luohailin/busybox:latest
    name: test
    command: ["/bin/sh","-c","echo $NAME;sleep 3000000"]
    env:
    - name: NAME
      value: test

[root@k8s-master ~]# kubectl apply -f pod.yml 
pod/test created
[root@k8s-master ~]# kubectl logs pods/test
test

2.4.4.6 示例6:资源限制

资源限制会影响pod的Qos Class资源优先级,资源优先级分为Guaranteed > Burstable > BestEffort

QoS(Quality of Service)即服务质量

[root@k8s-master ~]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  containers:
  - image: www.test.com/luohailin/busybox:latest
    name: test
    resources:
      limits:
        cpu: 500m
        memory: 200M
      requests:
        cpu: 500m
        memory: 100M

root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created

[root@k8s-master ~]# kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
test   1/1     Running   0          3s

[root@k8s-master ~]# kubectl describe pods test
    Limits:
      cpu:     500m
      memory:  100M
    Requests:
      cpu:        500m
      memory:     100M
QoS Class:                   Guaranteed
2.4.4.7 示例7 容器启动管理
[root@k8s-master ~]# vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  restartPolicy: Always
  containers:
    - image: luohailin/myapp:v1
      name: myapp
[root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created

[root@k8s-master ~]# kubectl get pods  -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
test   1/1     Running   0          6s    10.244.2.3   k8s-node2   <none>           <none>

[root@k8s-node2 ~]# docker rm -f ccac1d64ea81
2.4.4.8 示例8 选择运行节点
[root@k8s-master ~]# vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  nodeSelector:
    kubernetes.io/hostname: k8s-node1
  restartPolicy: Always
  containers:
    - image: luohailin/myapp:v1
      name: myapp

[root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created

[root@k8s-master ~]# kubectl get pods  -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
test   1/1     Running   0          21s   10.244.1.5   k8s-node1   <none>           <none>
2.4.4.9 示例9 共享宿主机网络
[root@k8s-master ~]# vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  hostNetwork: true
  restartPolicy: Always
  containers:
    - image: luohailin/busybox:latest
      name: busybox
      command: ["/bin/sh","-c","sleep 100000"]
[root@k8s-master ~]# kubectl apply -f pod.yml
pod/test created
[root@k8s-master ~]# kubectl exec -it pods/test -c busybox -- /bin/sh
/ # ifconfig
cni0      Link encap:Ethernet  HWaddr E6:D4:AA:81:12:B4
          inet addr:10.244.2.1  Bcast:10.244.2.255  Mask:255.255.255.0
          inet6 addr: fe80::e4d4:aaff:fe81:12b4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:6259 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6495 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:506704 (494.8 KiB)  TX bytes:625439 (610.7 KiB)

docker0   Link encap:Ethernet  HWaddr 02:42:99:4A:30:DC
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:6A:A8:61
          inet addr:172.25.254.20  Bcast:172.25.254.255  Mask:255.255.255.0
          inet6 addr: fe80::8ff3:f39c:dc0c:1f0e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:27858 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14454 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:26591259 (25.3 MiB)  TX bytes:1756895 (1.6 MiB)

flannel.1 Link encap:Ethernet  HWaddr EA:36:60:20:12:05
          inet addr:10.244.2.0  Bcast:0.0.0.0  Mask:255.255.255.255
          inet6 addr: fe80::e836:60ff:fe20:1205/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:40 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:163 errors:0 dropped:0 overruns:0 frame:0
          TX packets:163 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:13630 (13.3 KiB)  TX bytes:13630 (13.3 KiB)

veth9a516531 Link encap:Ethernet  HWaddr 7A:92:08:90:DE:B2
          inet6 addr: fe80::7892:8ff:fe90:deb2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:6236 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6476 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:592532 (578.6 KiB)  TX bytes:622765 (608.1 KiB)

/ # exit

三 pod的生命周期

3.1 INIT 容器

官方文档:Pod | Kubernetes

  • Pod 可以包含多个容器,应用运行在这些容器里面,同时 Pod 也可以有一个或多个先于应用容器启动的 Init 容器。

  • Init 容器与普通的容器非常像,除了如下两点:

    • 它们总是运行到完成

    • init 容器不支持 Readiness,因为它们必须在 Pod 就绪之前运行完成,每个 Init 容器必须运行成功,下一个才能够运行。

  • 如果Pod的 Init 容器失败,Kubernetes 会不断地重启该 Pod,直到 Init 容器成功为止。但是,如果 Pod 对应的 restartPolicy 值为 Never,它不会重新启动。

3.1.1 INIT 容器的功能

  • Init 容器可以包含一些安装过程中应用容器中不存在的实用工具或个性化代码。

  • Init 容器可以安全地运行这些工具,避免这些工具导致应用镜像的安全性降低。

  • 应用镜像的创建者和部署者可以各自独立工作,而没有必要联合构建一个单独的应用镜像。

  • Init 容器能以不同于Pod内应用容器的文件系统视图运行。因此,Init容器可具有访问 Secrets 的权限,而应用容器不能够访问。

  • 由于 Init 容器必须在应用容器启动之前运行完成,因此 Init 容器提供了一种机制来阻塞或延迟应用容器的启动,直到满足了一组先决条件。一旦前置条件满足,Pod内的所有的应用容器会并行启动。

3.1.2 INIT 容器示例

[root@k8s-master ~]# vim pod.yml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: initpod
  name: initpod
spec:
  containers:
   - image: luohailin/myapp:v1
     name: myapp
  initContainers:
   - name: init-myservice
     image: www.test.com/luohailin/busybox:latest
     command: ["sh","-c","until test -e /testfile;do echo wating for myservice; sleep 2;done"]


[root@k8s-master ~]# kubectl apply -f pod.yml 
pod/initpod created
[root@k8s-master ~]# kubectl get pods
NAME        READY   STATUS                  RESTARTS   AGE
initpod     0/1     Init:0/1                0          8s
readiness   0/1     Init:ImagePullBackOff   0          7m52s
[root@k8s-master ~]# kubectl logs pods/initpod init-myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
wating for myservice
[root@k8s-master ~]# kubectl exec pods/initpod -c init-myservice -- /bin/sh -c "touch /testfile"
[root@k8s-master ~]# kubectl get  pods 
NAME        READY   STATUS                  RESTARTS   AGE
initpod     1/1     Running                 0          39s
readiness   0/1     Init:ImagePullBackOff   0          8m23s

3.2 探针

探针是由 kubelet 对容器执行的定期诊断:

  • ExecAction:在容器内执行指定命令。如果命令退出时返回码为 0 则认为诊断成功。

  • TCPSocketAction:对指定端口上的容器的 IP 地址进行 TCP 检查。如果端口打开,则诊断被认为是成功的。

  • HTTPGetAction:对指定的端口和路径上的容器的 IP 地址执行 HTTP Get 请求。如果响应的状态码大于等于200 且小于 400,则诊断被认为是成功的。

每次探测都将获得以下三种结果之一:

  • 成功:容器通过了诊断。

  • 失败:容器未通过诊断。

  • 未知:诊断失败,因此不会采取任何行动。

Kubelet 可以选择是否执行在容器上运行的三种探针执行和做出反应:

  • livenessProbe:指示容器是否正在运行。如果存活探测失败,则 kubelet 会杀死容器,并且容器将受到其 重启策略 的影响。如果容器不提供存活探针,则默认状态为 Success。

  • readinessProbe:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与 Pod 匹配的所有 Service 的端点中删除该 Pod 的 IP 地址。初始延迟之前的就绪状态默认为 Failure。如果容器不提供就绪探针,则默认状态为 Success。

  • startupProbe: 指示容器中的应用是否已经启动。如果提供了启动探测(startup probe),则禁用所有其他探测,直到它成功为止。如果启动探测失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探测,则默认状态为成功Success。

ReadinessProbe 与 LivenessProbe 的区别

  • ReadinessProbe 当检测失败后,将 Pod 的 IP:Port 从对应的 EndPoint 列表中删除。

  • LivenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施

StartupProbe 与 ReadinessProbe、LivenessProbe 的区别如果三个探针同时存在,先执行 StartupProbe 探针,其他两个探针将会被暂时禁用,直到 pod 满足 StartupProbe 探针配置的条件,其他 2 个探针启动,如果不满足按照规则重启容器。

  • 另外两种探针在容器启动后,会按照配置,直到容器消亡才停止探测,而 StartupProbe 探针只是在容器启动后按照配置满足一次后,不在进行后续的探测。

3.2.1 探针实例

3.2.1.1 存活探针示例:
[root@k8s-master ~]# cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: readiness
  name: readiness
spec:
  containers:
   - image: luohailin/myapp:v1
     name: myapp
     readinessProbe:
       httpGet:
         path: /test.html
         port: 80
       initialDelaySeconds: 1
       periodSeconds: 3
       timeoutSeconds: 1

#测试
[root@k8s-master ~]# kubectl apply -f pod.yml

[root@k8s-master ~]# kubectl expose pod readiness --port 80 --target-port 80 service/readiness exposed

[root@k8s-master ~]# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
readiness   0/1     Running   0          14s
[root@k8s-master ~]# kubectl describe pods readiness

 Warning  Unhealthy  2s (x9 over 22s)  kubelet            Readiness probe failed: HTTP probe failed with statuscode: 404


[root@k8s-master ~]# kubectl describe services readiness
Name:              readiness
Namespace:         default
Labels:            run=readiness
Annotations:       <none>
Selector:          run=readiness
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.103.7.196
IPs:               10.103.7.196
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         			#没有暴漏端口,就绪探针探测不满足暴漏条件
Session Affinity:  None
Events:            <none>
[root@k8s-master ~]# kubectl exec pods/readiness -c myapp -- /bin/sh -c "echo test > /usr/share/nginx/html/test.html"


[root@k8s-master ~]# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
readiness   1/1     Running   0          119s
[root@k8s-master ~]# kubectl describe services readiness
Name:              readiness
Namespace:         default
Labels:            run=readiness
Annotations:       <none>
Selector:          run=readiness
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.103.7.196
IPs:               10.103.7.196
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.57:80		#满足条件端口暴漏
Session Affinity:  None
Events:            <none>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2140094.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CPLEX+Yalmip+MATLAB2022a配置

来源&#xff1a;yalmipcplex12.10文件及安装教程-CSDN博客https://blog.csdn.net/qq_41944352/article/details/126421198 安装包 来源&#xff1a;yalmipcplex12.10文件及安装教程-CSDN博客 Cplex 需下载&#xff1a; Microsoft Visual C 2015 Redistributable 添加路径&a…

时空大数据平台:激活新质生产力的智慧引擎

在数字化转型的浪潮中&#xff0c;时空大数据平台以其独特的价值&#xff0c;成为推动新质生产力发展的关键力量。本文不仅深入剖析时空大数据平台的定义与内涵&#xff0c;探讨其在智慧城市、智慧农业、环境管理、应急管理等领域的应用成效&#xff0c;还将详尽阐述平台如何通…

【C++】unordered系列

前言&#xff1a; 在C11及以后的标准中&#xff0c;unordered容器是标准模板库&#xff08;STL&#xff09;的一部分&#xff0c;提供了高效的数据结构选项&#xff0c;适用于需要快速查找和插入操作的场景。 unordered通常与关联容器一起使用&#xff0c;特别是unordered_map和…

【ESP32】ESP-IDF开发 | GPIO通用输入输出+LED点灯和按键输入例程

1. 简介 ESP32芯片有34个物理GPIO pad&#xff0c;每个GPIO pad都可用作一个通用IO或连接一个内部的外设信号。IO_MUX、RTC IO_MUX和GPIO交换矩阵用于将信号从外设传输至GPIO pad。 从上面看到&#xff0c;每个pad可以配置成GPIO功能&#xff08;连接GPIO交换矩阵&#xff09;或…

7-17 汉诺塔的非递归实现

输入样例: 3输出样例: a -> c a -> b c -> b a -> c b -> a b -> c a -> c 分析&#xff1a; 不会汉罗塔的uu们&#xff0c;先看看图解&#xff1a; 非递归代码&#xff1a; #include<iostream> #include<stack> using namespace std; s…

tomcat的Catalinalog和localhostlog乱码

找到tomcat安装目录的loging文件 乱码这两个由UTF-8改为GBK

C++笔记---多态

1. 多态的概念 多态(polymorphism)的概念&#xff1a;通俗来说&#xff0c;就是多种形态。 多态分为编译时多态(静态多态)和运行时多态(动态多态)&#xff0c;这里我们重点讲运行时多态&#xff0c;编译时多态(静态多态)和运行时多态(动态多态)。 编译时多态(静态多态)主要就…

MySQL中定义空值

如果一行中的某个列缺少数据值&#xff0c;该值被置为null&#xff0c;或者说包含一个空。 空是一个难以获得的、未分配的、未知的&#xff0c;或不适用的值。空和0或者空格不相同。0是一个数字&#xff0c;而空格是一个字符。 算术表达式中的空值 示例&#xff1a;计算年薪包…

CSS 布局技巧实现元素左右排列

开发中经常会遇到一个场景&#xff0c;使用 CSS 实现一个子元素靠右&#xff0c;其余子元素靠左。 这里总结一下常见的实现方式。 1. flex 布局 flexbox 是一种常用且灵活的布局方式&#xff0c;适合完成这种需求。将父容器设置为 display: flex&#xff0c;然后使用 margin…

Matlab Simulink 主时间步(major time step)、子时间步(minor time step)

高亮颜色说明&#xff1a;突出重点 个人觉得&#xff0c;&#xff1a;待核准个人观点是否有误 高亮颜色超链接 文章目录 对Simulink 时间步的理解Simulink 采样时间的类型Discrete Sample Times(离散采样时间)Controllable Sample Time(可控采样时间) Continuous Sample Times(…

51单片机-系列-单片机基础知识入门流水灯

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 单片机基础知识入门 常用的单片机封装 DIP直插 在DIP直插中&#xff0c;我们根据引脚数量的不同分为8P,14P,16P,18P,20P&#xff0c;这些是窄体&#xff0c;除了窄体之外&…

调用百度翻译API遇到的跨域问题解决方案

&#x1f389; 前言 这几天在学习前端的时候需要写一个实例&#xff0c;是关于翻译功能的。于是便想着在网上找一些API看能不能调用。这里遇到一个很坑的问题&#xff0c;就是我在暑假学习的时候曾经调用过心知天气的API、QQ音乐的API和今日头条的API&#xff0c;都未曾遇到过…

RT-DETR改进策略:BackBone改进|Swin Transformer,最强主干改进RT-DETR

摘要 在深度学习与计算机视觉领域,Swin Transformer作为一种强大的视觉Transformer架构,以其卓越的特征提取能力和自注意力机制,正逐步引领着图像识别与检测技术的革新。近期,我们成功地将Swin Transformer引入并深度整合至RT-DERT(一种高效的实时目标检测与识别框架)中…

BSV区块链上的覆盖网络服务现已开放公测

​​发表时间&#xff1a;2024年8月30日 BSV区块链的覆盖网络服务现已正式开放公测。对于BSV区块链生态系统内的特定交易类型和数据管理及访问&#xff0c;覆盖网络服务都可以为它们提供强大、可扩展、并且合规的解决方案。覆盖网络以及其它即将推出的BSV服务将赋予开发者、企业…

文件误删除?助你一键恢复

文件误删除之痛 在日常的数字生活中&#xff0c;文件误删除是许多用户不时会遭遇的“小确丧”。无论是手滑点击了“删除”键&#xff0c;还是系统崩溃导致的文件丢失&#xff0c;这些意外事件总能让人心急如焚。文件误删除不仅可能意味着重要资料的永久消失&#xff0c;还可能…

Linux驱动编程 - platform平台设备驱动总线

目录 简介&#xff1a; 一、初识platform平台设备驱动 1、platform_driver驱动代码框架 2、platform_device设备代码框架 3、测试结果 3.1 Makefile编译 3.2 加载驱动 二、platform框架分析 1、注册platform总线 1.1 创建platform平台总线函数调用流程 1.2 platform_b…

鸿蒙开发之ArkTS 基础三 数组

数组可以存储多个数据 语法为: let 数组名字:数组类型[] [数据一,数据二 ,数据三 ,数据四 ,数据5⃣️] 例如:学生类数组 let students:string[] [小美,小红,小张,小西] console.log("students",students) 输出 小美,小红,小张,小西 这里不需要遍历就能输出内容…

C Primer Plus 第5章习题

你该逆袭了 红色标注的是&#xff1a;错误的答案 蓝色标注的是&#xff1a;正确的答案 绿色标注的是&#xff1a;做题时有疑问的地方 橙色标注的是&#xff1a;答案中需要着重注意的地方 练习题 一、复习题1、2、3、4、错误答案&#xff1a;正确答案&#xff1a; 5、我的答案&a…

十三,Spring Boot 中注入 Servlet,Filter,Listener

十三&#xff0c;Spring Boot 中注入 Servlet&#xff0c;Filter&#xff0c;Listener 文章目录 十三&#xff0c;Spring Boot 中注入 Servlet&#xff0c;Filter&#xff0c;Listener1. 基本介绍2. 第一种方式&#xff1a;使用注解方式注入&#xff1a;Servlet&#xff0c;Fil…

Cobbler 搭建方法

统信服务器操作系统行业版V20-1000c【Cobbler 搭建】手册 统信服务器操作系统行业版 V20版本上Cobbler 搭建方法 文章目录 功能概述一、使用范围二、cobbler工作流程1. Server 端2. Client 端三、 环境准备1. 测试环境告知,以提供配置时参考:2. 关闭防火墙、selinux:3. 注意…