做分色找工作网站,做网站开发的公司,跨境电商开店,上海网站建设一般多少钱目录
一.metadata常用属性
二.spec.containers子属性介绍
explain pod.spec.containers给出的参考
1.command示例演示
2.env和envFrom示例演示
3.ports部分详解
4.resources部分详解
5.startupProbe格式演示
6.terminationMessagePath和terminationMessagePolicy格式演…目录
一.metadata常用属性
二.spec.containers子属性介绍
explain pod.spec.containers给出的参考
1.command示例演示
2.env和envFrom示例演示
3.ports部分详解
4.resources部分详解
5.startupProbe格式演示
6.terminationMessagePath和terminationMessagePolicy格式演示
7.volumeDevices格式演示
8.volumeMounts格式演示
三.spec.volumes子属性介绍 一.metadata常用属性
[rootk8s-master pod]# kubectl explain pod.metadata
KIND: Pod
VERSION: v1
FIELD: metadata ObjectMeta
FIELDS:
labels map[string]string#指定资源的label列表
name string#自定义名称namespace string#指定名称空间
二.spec.containers子属性介绍
explain pod.spec.containers给出的参考
[rootk8s-master pod]# kubectl explain pod.spec.containers
KIND: Pod
VERSION: v1
FIELD: containers []Container
DESCRIPTION:List of containers belonging to the pod. Containers cannot currently beadded or removed. There must be at least one container in a Pod. Cannot beupdated.A single application container that you want to run within a pod.FIELDS:args []string#容器的启动命令需要的参数列表主要用于和docker和Dockerfile相关的配置
command []string#启动容器后执行的命令不指定时使用打包时使用的启动命令
env []EnvVar#容器环境变量的配置列表
envFrom []EnvFromSource#使用了ConfigMap来指定变量文件时用envFrom来指定
image string#镜像名称
imagePullPolicy string#镜像拉取策略指定tag为latest默认alwaystag为具体版本号默认IfNotPresent。#always表示每次都尝试重新拉取镜像#ifNotPresent表示如果本地有那个镜像就使用本地的不存在时才拉取#Nerver表示仅使用本地有的镜像绝不拉取本地没有时报错
lifecycle Lifecycle#定义容器的生命周期使得容器在启动和终止时执行特定的任务#可选portStart在容器创建后立即执行的操作。这可以用于执行一些初始化任务比如检查容器依赖的服务是否可用或者向外部系统注册当前容器的信息等#和preStop在容器终止之前执行的操作。这可以用于执行一些清理任务比如保存数据、发送信号给其他服务表明当前容器即将停止等两个配置
livenessProbe Probe#指定对容器进行存活性检测检测失败会执行重启容器等操作。#HTTP通过向容器提供的 HTTP 端点发送请求并根据返回的状态码来判断容器的存活状态#TCP通过尝试建立 TCP 连接来判断容器的存活状态#Exec通过在容器内执行特定的命令并根据命令的返回状态来判断容器的存活状态三种探测方式
name string -required-#容器名称
ports []ContainerPort#容器需要暴露的端口列表
readinessProbe Probe#对容器实行就绪检测确定容器是否已经准备好接受流量检测失败则会剔除该服务直至再次检测成功#和livenessProbe具有同样含义的三种方式
resources ResourceRequirements#资源限制和资源请求的配置
restartPolicy string#指定容器的重新启动策略#Always无论什么原因导致容器退出将始终尝试重新启动容器以确保容器一直处于运行状态#OnFailture只有在容器以非零状态退出时才会尝试重新启动容器。换句话说如果容器正常退出状态码为 0则不会重新启动它#Never永远不会尝试重新启动容器。这意味着一旦容器退出它将保持停止状态直到人为干预为止
startupProbe Probe#后文有格式演示与livenessProbe和readinessProbe类似仅在容器启动时运行一次用于确定容器是否已经准备好接收流量stdin boolean#true/false是否可以通过 kubectl attach 命令将本地标准输入连接到容器的标准输入从而与容器进行交互
stdinOnce boolean#同stdin但只接受第一个进程的标准输入后即关闭
terminationMessagePath string#指定容器终止时记录终止消息的文件路径
terminationMessagePolicy string#指定容器终止时记录终止消息的策略#File使用terminationMessagePath所设置的值来记录#FallbackToLogsOnError如果无法将终止消息写入文件则将使用容器的日志作为终止消息的内容#后文有格式演示tty boolean#是否分配一个交互终端环境
volumeDevices []VolumeDevice#设置将主机上的块设备映射到容器内部#name指定名称devicePath指定设备路径#后文有格式演示volumeMounts []VolumeMount#后文有格式演示用于将卷挂载到容器内部特定的位置mountPath指定目标路径readOnly可以设置是否只读
workingDir string#用于指定容器启动时的进程所在目录对于需要在特定目录下执行命令或者读取特定路径的应用程序非常有用
1.command示例演示
1运行一个pod其中有httpd和busybox两个容器启动busybox后执行以下操作每隔60s向/hello.txt中追加一次hello。
使用/bin/sh来执行命令-c后面指定要执行的命令sleep可以减少循环过多导致的资源浪费
[rootk8s-master pod]# cat mybusybox.yaml
apiVersion: v1
kind: Pod
metadata:name: mybusyboxnamespace: myns
spec:containers:- name: httpdimage: httpd- name: busybox #busybox比较特殊它相当于一个工具而不是一个软件程序直接运行时会报错的image: busyboxcommand: [/bin/sh,-c,touch /hello.txt;while true;do /bin/echo hello /hello.txt; sleep 60; done;][rootk8s-master pod]# kubectl exec -it mybusybox -c busybox -n myns -- /bin/sh -c cat /hello.txt
hello
hello
hello
hello
#在对运行的容器进行操作时也可以使用-c去指定要执行的命令而不需要页面进入容器
2注意当你的k8s是docker部署还存在Dockerfile的情况args选项的配置显得比较重要command和args要实现去覆盖Dockerfile中的ENTRYPOINT功能
command和args都没有写时会使用Dockerfile的配置command写args没有写Dockerfile默认的配置不会生效执行command部分command没写args写Dockerfile中配置的ENTRYPOINT的命令会生效并使用当前args的参数command和args都写Dockerfile的配置被忽略执行command和args指定的参数
2.env和envFrom示例演示
1运行一个podpod内运行busybox容器容器运行后为在其中创建一个用户通过引用设置的环境变量来为该用户设置名称和密码
[rootk8s-master pod]# cat user.yaml
apiVersion: v1
kind: Pod
metadata:name: myusernamespace: myns
spec:containers:- name: my-userimage: busyboxcommand: [/bin/sh,-c,while true;do /bin/adduser -D $username; echo $username:$password | /bin/chpasswd; sleep 3600; done;]env:- name: usernamevalue: SLB- name: passwordvalue: hello123
[rootk8s-master pod]# kubectl exec -it myuser -c my-user -n myns -- /bin/sh -c cat /etc/passwd | grep SLB
SLB:x:1000:1000:Linux User,,,:/home/SLB:/bin/sh
注意env设置的变量在容器内都可以引用[rootk8s-master pod]# kubectl exec -it myuser -c my-user -n myns -- /bin/sh
/ # echo $username
SLB
/ # echo $password
hello123
2常用方式是使用ConfigMap来将env变量存放在单独的配置文件中再在配置中通过configMapRef进行引用如下所示
#create去在同一个命名空间内创建一个user-config来存放用户的两个变量数据
[rootk8s-master pod]# kubectl create configmap user-config --from-literalusernameSLB1 --from-literalpasswordhello123 -n myns
configmap/user-config created
[rootk8s-master pod]# kubectl get configMap user-config -n myns -o yaml
apiVersion: v1
data:password: hello123username: SLB1
kind: ConfigMap
metadata:creationTimestamp: 2023-11-11T14:14:51Zname: user-confignamespace: mynsresourceVersion: 45414uid: 86c13358-0a38-4120-84eb-e15c01aa91f3
[rootk8s-master pod]# cat user.yaml
apiVersion: v1
kind: Pod
metadata:name: myusernamespace: myns
spec:containers:- name: my-userimage: busyboxcommand: [/bin/sh,-c,while true;do /bin/adduser -D $username; echo $username:$password | /bin/chpasswd; sleep 3600; done;]envFrom:- configMapRef: name: user-config
[rootk8s-master pod]# kubectl exec -it myuser -c my-user -n myns -- /bin/sh -c cat /etc/passwd | grep SLB1
SLB1:x:1000:1000:Linux User,,,:/home/SLB1:/bin/sh
3.ports部分详解
1kubectl explain pod.spec.containers.ports
[rootk8s-master pod]# kubectl explain pod.spec.containers.ports
KIND: Pod
VERSION: v1
hostIP string#要将外部端口绑定到的主机的IP
hostPort integer#容器在主机上公开的端口如果设置了这个选项主机上只能是运行容器副本name string#端口的名称需要具有唯一性
protocol string#端口协议UDP/TCP/STCP默认TCPcontainerPort integer -required-#容器要监听的端口0~65535
2示例
创建一个nginx容器暴露80端口
[rootk8s-master pod]# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:name: mynginxnamespace: mynslabels:run: nginxuser: sulibao
spec:containers:- name: mynginximage: nginxports:- name: mynginx-portcontainerPort: 80
[rootk8s-master pod]# kubectl describe pod mynginx -n myns | grep -A 10 Containers: | tail -n 2mynginx:Container ID: containerd://727e31321598fc3065f38d9ecb50b601f81a7723d916c2d550951c6510dcfc06Image: nginxImage ID: docker.io/library/nginxsha256:86e53c4c16a6a276b204b0fd3a8143d86547c967dc8258b3d47c3a21bb68d3c6Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Sat, 11 Nov 2023 22:30:08 0800Ready: TrueRestart Count: 0
4.resources部分详解
1容器运行时的资源配额可以通过resources部分来指定若该容器资源配置过少自己启动不了配置过多又会影响其他容器的正常运行
2参数解析
limits是用于限制容器的上限资源占用情况一旦容器真正启动后占用资源超过limits处配置则会被终止并重启
requests是用于规定容器的下限资源占用情况若环境资源不足容器无法启动
cpu是用于指定core数
memory是用于指定内存大小单位可以是M(10^6字节)、Mi(二进制兆节为1024^2字节)、G(10^9字节、Gi(二进制千兆字节为1024^3字节)等
3示例
运行一个nginx的pod资源上限为2cpu、1G内存资源下为2cpu、2M内存
[rootk8s-master pod]# cat nginx.yaml
apiiVersion: v1
kind: Pod
metadata:name: mynginxnamespace: mynslabels:run: nginxuser: sulibao
spec:containers:- name: mynginximage: nginxports:- name: mynginx-portcontainerPort: 80resources:limits:cpu: 2memory: 1Grequests:cpu: 2memory: 2M
5.startupProbe格式演示
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: mycontainerimage: nginxstartupProbe:httpGet:path: /myport: 8080
6.terminationMessagePath和terminationMessagePolicy格式演示
terminationMessagePolicy 设置为 File且通过 terminationMessagePath 指定了记录终止消息的文件路径
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: mycontainerimage: nginxterminationMessagePath: /var/log/termination.logterminationMessagePolicy: File
7.volumeDevices格式演示
volumeDevices 将 /dev/sdb 设备映射到 mydevice 的卷中并将其挂载到 mycontainer 容器内部
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: mycontainerimage: nginxvolumeDevices:- name: mydevicedevicePath: /dev/sdbvolumes:- name: mydevicehostPath:path: /dev/sdb
8.volumeMounts格式演示
volumeMounts 将 myvolume 的卷挂载到了 /data 路径上且设置为只读模式
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: mycontainerimage: nginxvolumeMounts:- name: myvolumemountPath: /datareadOnly: truevolumes:- name: myvolumehostPath:path: /mnt/data
三.spec.volumes子属性介绍
[rootk8s-master pod]# kubectl explain pod.spec.volumes
KIND: Pod
VERSION: v1
FIELD: volumes []Volume
azureDisk AzureDiskVolumeSource#azureDisk挂载到容器内部
azureFile AzureFileVolumeSource#azureDisk挂载到容器内部容器可以访问Azure存储中的文件
cephfs CephFSVolumeSource#Ceph 文件系统挂载到 Pod 内部使得容器可以访问 Ceph 集群中的文件系统
cinder CinderVolumeSource#将 OpenStack Cinder 卷挂载到 Pod 内部使得容器可以访问 OpenStack 中的块存储卷
configMap ConfigMapVolumeSource#将ConfigMap中的配置数据挂载到Pod内部可以自己创建ConfigMap使得容器可以访问这些配置数据
csi CSIVolumeSource#将外部存储系统通过 CSI 插件挂载到 Pod 内部使得容器可以访问这些外部存储
emptyDir EmptyDirVolumeSource#在 Pod 启动时创建一个空目录并将其挂载到 Pod 内部使得容器可以在该目录中进行读写操作。这个空目录的生命周期与 Pod 相关联当 Pod 被删除时这个空目录也会被清除不应该用于持久化存储配置hostPath HostPathVolumeSource#将主机上的特定文件或目录挂载到Pod内部使得容器可以访问主机上的文件系统内容。这种方式可以用于访问主机上的特定配置文件、日志目录或其他主机文件系统中的内容。#hostPath中的路径path是Pod创建节点的绝对路径Pod删除后该路径下的数据不会被删除
name string -required-#指定卷的名称该名称将在容器的 volumeMounts 中使用以将卷挂载到容器中nfs NFSVolumeSource#将远程的 NFS 存储挂载到 Pod 内部使得容器可以访问远程存储中的文件。这种方式可以用于实现多个 Pod 之间共享文件或数据同时也可以将持久化的存储挂载到 Pod 中而且这些存储可以跨节点访问#通过server指定nfs服务器地址
persistentVolumeClaim PersistentVolumeClaimVolumeSource#将持久化存储声明Persistent Volume Claim挂载为卷用于访问持久化存储如 PersistentVolumes
secret SecretVolumeSource#将 Secret 对象中的数据挂载为卷使得容器可以访问其中的敏感数据如密码、CA证书等