云原生(十四) 容器

云原生(十四)  容器

深入万物基础-容器

一、思考

我们在 k8s 里面的容器和 docker 的容器有什么异同?

其实 docker 之前有自己的一套编排软件:docker swarm 它可以在多台主机中创建一个 docker 集群,但是也仅限于此了,docker 在很早就放弃了这个项目。docker machine 是配合 swarm 的一个预处理工具

k8s 全称:kubernetes,因为中间有 8 个字母,所以简称 k8s,是谷歌公司开发的一款容器编排工具,占据了 80%以上的市场份额。

k8s 的 Pod 是最小单位,Pod 中容器的配置需要注意以下常用的

Pod 里面的容器内容可以写的东西

args

command Entrypoint array. Not executed within a shell. The docker image’s ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME) are expanded using the container’s environment. If a variable cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax can be escaped with a double $, ie: $(VAR_NAME). Escaped references will never be expanded, regardless of whether the variable exists or not. Cannot be updated. More info: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell

env 容器要用的环境变量

envFrom List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence. Cannot be updated.

image 写镜像的名字

imagePullPolicy 下载策略: Always:总是去下载: 【默认】 先看网上有没有,有了就下载,(本机也有,docker 就相当于不用下载了) Never:总不去下载,一定保证当前 Pod 所在的机器有这个镜像 ;直接看本机 IfNotPresent:如果本机没有就去下载;先看本机,再看远程

lifecycle 生命周期钩子

livenessProbe Periodic probe of container liveness. Container will be restarted if the probe fails. Cannot be updated. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

name -required- 容器的名字

ports 端口:

readinessProbe Periodic probe of container service readiness. Container will be removed from service endpoints if the probe fails. Cannot be updated. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

resources Compute Resources required by this container. Cannot be updated. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/

securityContext Security options the pod should run with. More info: https://kubernetes.io/docs/concepts/policy/security-context/ More info: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/

startupProbe StartupProbe indicates that the Pod has successfully initialized. If specified, no other probes are executed until this completes successfully. If this probe fails, the Pod will be restarted, just as if the livenessProbe failed. This can be used to provide different probe parameters at the beginning of a Pod’s lifecycle, when it might take a long time to load data or warm a cache, than during steady-state operation. This cannot be updated. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

stdin Whether this container should allocate a buffer for stdin in the container runtime. If this is not set, reads from stdin in the container will always result in EOF. Default is false.

stdinOnce Whether the container runtime should close the stdin channel after it has been opened by a single attach. When stdin is true the stdin stream will remain open across multiple attach sessions. If stdinOnce is set to true, stdin is opened on container start, is empty until the first client attaches to stdin, and then remains open and accepts data until the client disconnects, at which time stdin is closed and remains closed until the container is restarted. If this flag is false, a container processes that reads from stdin will never receive an EOF. Default is false

terminationMessagePath Optional: Path at which the file to which the container’s termination message will be written is mounted into the container’s filesystem. Message written is intended to be brief final status, such as an assertion failure message. Will be truncated by the node if greater than 4096 bytes. The total message length across all containers will be limited to 12kb. Defaults to /dev/termination-log. Cannot be updated.

terminationMessagePolicy Indicate how the termination message should be populated. File will use the contents of terminationMessagePath to populate the container status message on both success and failure. FallbackToLogsOnError will use the last chunk of container log output if the termination message file is empty and the container exited with an error. The log output is limited to 2048 bytes or 80 lines, whichever is smaller. Defaults to File. Cannot be updated.

tty Whether this container should allocate a TTY for itself, also requires ‘stdin’ to be true. Default is false.

volumeDevices volumeDevices is the list of block devices to be used by the container.

volumeMounts Pod volumes to mount into the container’s filesystem. Cannot be updated.

workingDir 指定进容器的工作目录

二、镜像

在 Kubernetes 的 Pod 中使用容器镜像之前,我们必须将其推送到一个镜像仓库(或者使用仓库中已经有的容器镜像)。在 Kubernetes 的 Pod 定义中定义容器时,必须指定容器所使用的镜像,容器中的 image 字段支持与 docker 命令一样的语法,包括私有镜像仓库和标签。

如果使用 hub.dokcer.com Registry 中的镜像,可以省略 registry 地址和 registry 端口。例如:nginx:latest

Kubernetes 中,默认的镜像抓取策略是 IfNotPresent,使用此策略,kubelet 在发现本机有镜像的情况下,不会向镜像仓库抓取镜像。如果您期望每次启动 Pod 时,都强制从镜像仓库抓取镜像,可以尝试如下方式:

  • 设置 container 中的 imagePullPolicy 为 Always
  • 省略 imagePullPolicy 字段,并使用 :latest tag 的镜像
  • 省略 imagePullPolicy 字段和镜像的 tag
  • 激活 AlwaysPullImages 管理控制器

docker pull redis docker.io/library/redis:latest

下载私有仓库镜像

#这个秘钥默认在default名称空间,不能被hello名称空间共享kubectl create secret -n hello docker-registry my-aliyun –docker-server=registry.cn-hangzhou.aliyuncs.com –docker-username=lansonli –docker-password=lansonli123456789

apiVersion: v1kind: Podmetadata: name: foospec: containers: – name: foo image: registry.cn-zhangjiakou.aliyuncs.com/atguigudocker/atguigu-java-img:v1.0 imagePullSecrets: – name: mydocker

三、启动命令

四、环境变量

env 指定即可

五、生命周期容器钩子

Kubernetes 中为容器提供了两个 hook(钩子函数):

  • PostStart
  • 此钩子函数在容器创建后将立刻执行。但是,并不能保证该钩子函数在容器的 ENTRYPOINT 之前执行。该钩子函数没有输入参数。
  • PreStop
  • 此钩子函数在容器被 terminate(终止)之前执行,例如:
  • 通过接口调用删除容器所在 Pod
  • 某些管理事件的发生:健康检查失败、资源紧缺等
  • 如果容器已经被关闭或者进入了 completed 状态,preStop 钩子函数的调用将失败。该函数的执行是同步的,即,kubernetes 将在该函数完成执行之后才删除容器。该钩子函数没有输入参数。

apiVersion: v1kind: Podmetadata: name: lansonli-demospec: containers: – name: lansonli-demo-container image: alpine command: [“/bin/sh”, “-c”, “echo hello; “] volumeMounts: – name: mount1 mountPath: /app lifecycle: postStart: exec: command: [“/bin/sh”, “-c”, “echo world;”] preStop: exec: command: [“/bin/sh”,”-c”,”echo 66666;”]

  • Kubernetes 在容器启动后立刻发送 postStart 事件,但是并不能确保 postStart 事件处理程序在容器的 EntryPoint 之前执行。postStart 事件处理程序相对于容器中的进程来说是异步的(同时执行),然而,Kubernetes 在管理容器时,将一直等到 postStart 事件处理程序结束之后,才会将容器的状态标记为 Running。
  • Kubernetes 在决定关闭容器时,立刻发送 preStop 事件,并且,将一直等到 preStop 事件处理程序结束或者 Pod 的 –grace-period 超时,才删除容器

六、资源限制

pods/qos/qos-pod.yaml apiVersion: v1kind: Podmetadata: name: qos-demo namespace: qos-examplespec: containers: – name: qos-demo-ctr image: nginx resources: limits: # 限制最大大小 -Xmx memory: “200Mi” cpu: “700m” # 启动默认给分配的大小 -Xms requests: memory: “200Mi” cpu: “700m”

kubectl describe 可用来排错的,查看资源的状态

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
用户投稿
上一篇 2022年8月16日 06:11
下一篇 2022年8月16日 06:11

相关推荐

  • 推荐48个微商引流推广的方法(微商引流推广的方法有哪些)

    微商引流技能01——同行互推 资源共享,大家才会共赢。加入你是做穴位贴的,你的朋友是做化妆品的,这是两个没有交集的行业,你们可以友情互推,这样每个月的资源就都扩大了一倍,而且这些资…

    2022年11月27日
  • 存储过程语法(sql server存储过程语法)

    今天小编给各位分享存储过程语法的知识,其中也会对sql server存储过程语法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧! oracle存储过程基本语法…

    2022年11月26日
  • 京东快递跨省几天能到(京东一般多久到货)

    由于觉得京东平台的物流效劳十分的不错,而且商品的质量都比拟有保证。在京东平台购物的话,普通多久时间会到货?下面的内容停止引见。 京东普通多久到货? 详细要看发货地址和你所在的地址的…

    2022年11月26日
  • 地灵绝魂剑(地灵绝魂剑像素)

    今天小编给各位分享地灵绝魂剑的知识,其中也会对地灵绝魂剑像素进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧! dnf地灵绝魂剑怎么计算 地灵绝魂剑的附加攻击是…

    2022年11月25日
  • 事件营销案例

    篇一成功营销案例小故事成功销售案例小故事篇1模仿 一个人想做一套家具,就走到树林里砍倒一棵树,并动手把它锯成木板这个人锯树的时候,把树干的一头搁在树墩上,自己骑在树干上还往锯开的缝…

    2022年11月24日
  • 今天出入济南最新通知(出入济南政策最新消息今天)

    近日济南疫情感染人数也在不断增加,划分的高风险区也是越来越多了。据最新统计,截止2022年11月24日11时,济南共有低风险地区12处,高风险地区338处。很多朋友都担心现在济南无…

    2022年11月24日
  • 我叫MT归来墓园有什么用 我叫MT归来墓园什么时候开启?

    多小伙伴是不是都不知道我叫MT归来墓园有什么用?全明星激斗作为一款3D卡牌手游,受到了很多小伙伴的关注,我叫MT归来墓园攻略小伙伴们知道了吗?下面就和小编一起来了解一下吧。 我叫M…

    2022年11月22日
  • QQ发布6.8.8.6517测试版 新增GIF表情Tab

    腾讯 QQ 现已面向 macOS 用户发布了 6.8.8.6517 测试版更新,带来了新功能、体验优化和 Bug 修复。 新功能方面,测试版中,QQ 支持记忆消息输入区大小和群成员…

    2022年11月22日
  • 微信健康码怎么注销重新申请健康码 怎么更换绑定人

    在疫情常态化的管理之下,出行都需要健康码,那么如果因为种种原因注销了健康码怎么办呢?这种情况下是否还可以再申请健康码呢?下面一起来看看了解一下吧! 微信健康码注销了还能申请吗 健康…

    2022年11月22日
  • 今年是冷冬还是暖冬?拉尼娜事件将持续至2022/2023年冬季

    □ 拉尼娜事件是指赤道中、东太平洋的海水表面温度低于正常年份温度的现象,它与海面水温持续高于常年温度的厄尔尼诺事件相反,被认为是可能导致全球气候异常的重要信号。 □ 今年冬季影响我…

    2022年11月21日

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息