Pod 是在 Kubernetes 中创建和管理的、最小的可部署的计算单元。Pod(就像在鲸鱼荚或者豌豆荚中)是一组(一个或多个) 容器; 这些容器共享存储、网络、以及怎样运行这些容器的声明。 Pod 中的内容总是并置(colocated)的并且一同调度,在共享的上下文中运行。 Pod 所建模的是特定于应用的 “逻辑主机”,其中包含一个或多个应用容器, 这些容器相对紧密地耦合在一起。 在非云环境中,在相同的物理机或虚拟机上运行的应用类似于在同一逻辑主机上运行的云应用。
Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 5s default-scheduler Successfully assigned default/pod-imagepull to work1.host.com Normal Pulling 4s kubelet Pulling image "docker.io/library/nginx:1.23.1" Normal Pulled 1s kubelet Successfully pulled image "docker.io/library/nginx:1.23.1"in 2.762104819s Normal Created 1s kubelet Created container mynginx Normal Started 1s kubelet Started container mynginx Normal Pulled 1s (x2 over 1s) kubelet Container image "docker.io/library/busybox:1.35.0" already present on machine Normal Created 1s (x2 over 1s) kubelet Created container mybusybox Normal Started 1s kubelet Started container mybusybox
# 创建pod [root@master yaml]# kubectl create -f Pod-Command.yaml pod/pod-command created
# 查看Pod状态 # 这个时候俩容器就都正常运行了 [root@master yaml]# kubectl get pod [root@master yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE pod-command 2/2 Running 0 1m38s
[root@master yaml]# kubectl explain pod.spec.containers.ports KIND: Pod VERSION: v1
RESOURCE: ports <[]Object>
DESCRIPTION: List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.
ContainerPort represents a network port in a single container.
FIELDS: containerPort <integer> -required- # 容器要监听的端口(0<x<65536) Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536. hostIP <string> # 要将外部端口绑定到的主机IP(一般省略) What host IP to bind the external port to. hostPort <integer> # 容器要在主机上公开的端口,如果设置,主机上只能运行容器的一个副本(一般省略) Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this. name <string> # 端口名称,如果指定,必须保证name在pod中是唯一的 If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services. protocol <string> # 端口协议。必须是UDP、TCP或SCTP。默认为“TCP”。 Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP". Possible enum values: - `"SCTP"` is the SCTP protocol. - `"TCP"` is the TCP protocol. - `"UDP"` is the UDP protocol.
# 访问服务 # 访问容器中的程序需要使用的是`podIp:containerPort` [root@master yaml]# curl http://10.244.52.207:80 <!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>
# 创建Pod [root@master yaml]# kubectl create -f Pod-Resources.yaml pod/pod-resources created
# 查看Pod状态,Pod启动失败 [root@master yaml]# kubectl get pod pod-resources -n default NAME READY STATUS RESTARTS AGE pod-resources 0/1 Pending 0 29s
# 查看Pod详细信息会看到报错 [root@master yaml]# kubectl describe pod pod-resources -n default ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedScheduling 97s default-scheduler 0/3 nodes are available: 1 node(s) had untolerated taint {node-role.kubernetes.io/master: }, 3 Insufficient memory. preemption: 0/3 nodes are available: 1 Preemption is not helpful for scheduling, 2 No preemption victims found for incoming pod.
# 创建Pod [root@master yaml]# kubectl create -f Pod-InitContainer.yaml pod/pod-initcontainer created
# 查看状态 # 发现pod一直卡在第一个初始化容器过程中,后面的容器不会运行 [root@master yaml]# kubectl describe pod pod-initcontainer -n default ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 66s default-scheduler Successfully assigned default/pod-initcontainer to work1.host.com Normal Pulled 66s kubelet Container image "docker.io/library/busybox:1.35.0" already present on machine Normal Created 66s kubelet Created container test-mysql Normal Started 66s kubelet Started container test-mysql
# 创建Pod [root@master yaml]# kubectl create -f Pod-Liveness-Exec.yaml pod/pod-liveness-exec created
# 查看Pod详情 # 发现nginx容器启动之后就进行了健康检查 # 检查失败之后容器就呗kill掉了,之后容器 [root@master yaml]# kubectl describe pods pod-liveness-exec -n default ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 25s default-scheduler Successfully assigned default/pod-liveness-exec to work1.host.com Normal Pulled 24s kubelet Container image "docker.io/library/nginx:1.23.1" already present on machine Normal Created 24s kubelet Created container nginx Normal Started 24s kubelet Started container nginx Warning Unhealthy 5s (x2 over 15s) kubelet Liveness probe failed: /bin/cat: /tmp/hello.txt: No such file or directory
# 查看Pod状态 # 发现RESTARTS一直在增长 [root@master yaml]# kubectl get pods pod-liveness-exec -n default NAME READY STATUS RESTARTS AGE pod-liveness-exec 0/1 CrashLoopBackOff 4 (12s ago) 2m43s
# 创建Pod [root@master yaml]# kubectl create -f Pod-Liveness-Tcpsocket.yaml pod/pod-liveness-tcpsocket created
# 查看Pod详情 # 发现容器尝试访问8080端口,但是失败了 [root@master yaml]# kubectl describe pods pod-liveness-tcpsocket -n default ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 31s default-scheduler Successfully assigned default/pod-liveness-tcpsocket to work1.host.com Normal Pulled 1s (x2 over 30s) kubelet Container image "docker.io/library/nginx:1.23.1" already present on machine Normal Created 1s (x2 over 30s) kubelet Created container nginx Normal Started 1s (x2 over 30s) kubelet Started container nginx Warning Unhealthy 1s (x3 over 21s) kubelet Liveness probe failed: dial tcp 10.244.67.89:8080: connect: connection refused Normal Killing 1s kubelet Container nginx failed liveness probe, will be restarted
# 查看Pod状态 # 发现RESTARTS一直在增长 [root@master yaml]# kubectl get pods pod-liveness-tcpsocket -n default NAME READY STATUS RESTARTS AGE pod-liveness-tcpsocket 1/1 Running 4 (7s ago) 2m7s
# 创建Pod [root@master yaml]# kubectl create -f Pod-Liveness-Httpget.yaml pod/pod-liveness-httpget created
# 查看Pod详情 [root@master yaml]# kubectl describe pod pod-liveness-httpget -n default ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 22s default-scheduler Successfully assigned default/pod-liveness-httpget to work2.host.com Normal Pulled 22s kubelet Container image "docker.io/library/nginx:1.23.1" already present on machine Normal Created 21s kubelet Created container nginx Normal Started 21s kubelet Started container nginx Warning Unhealthy 2s (x2 over 12s) kubelet Liveness probe failed: HTTP probe failed with statuscode: 404
# 查看Pod状态 # 发现RESTARTS一直在增长 [root@master yaml]# kubectl get pod pod-liveness-httpget -n default NAME READY STATUS RESTARTS AGE pod-liveness-httpget 1/1 Running 2 (26s ago) 86s
[root@master yaml]# kubectl explain pod.spec.containers.livenessProbe KIND: Pod VERSION: v1
RESOURCE: livenessProbe <Object>
DESCRIPTION: 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
Probe describes a health check to be performed against a container to determine whether it is alive or ready to receive traffic.
FIELDS: exec <Object> Exec specifies the action to take.
failureThreshold <integer> # 连续探测失败多少次才被认定为失败。默认是3。最小值是1 Minimum consecutive failures for the probe to be considered failed after having succeeded. Defaults to 3. Minimum value is 1.
grpc <Object> GRPC specifies an action involving a GRPC port. This is a beta field and requires enabling GRPCContainerProbe feature gate.
httpGet <Object> HTTPGet specifies the http request to perform.
initialDelaySeconds <integer> # 容器启动后等待多少秒执行第一次探测 Number of seconds after the container has started before liveness probes are initiated. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
periodSeconds <integer> # 执行探测的频率。默认是10秒,最小1秒 How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1.
successThreshold <integer> # 连续探测成功多少次才被认定为成功。默认是1 Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Must be 1 for liveness and startup. Minimum value is 1.
tcpSocket <Object> TCPSocket specifies an action involving a TCP port.
terminationGracePeriodSeconds <integer> Optional duration in seconds the pod needs to terminate gracefully upon probe failure. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup timefor your process. If this value is nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this value overrides the value provided by the pod spec. Value must be non-negative integer. The value zero indicates stop immediately via the kill signal (no opportunity to shut down). This is a beta field and requires enabling ProbeTerminationGracePeriod feature gate. Minimum value is 1. spec.terminationGracePeriodSeconds is used if unset. timeoutSeconds <integer> # 探测超时时间。默认1秒,最小1秒 Number of seconds after which the probe times out. Defaults to 1 second. Minimum value is 1. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
# 创建Pod [root@master yaml]# kubectl create -f Pod-Restartpolicy.yaml pod/pod-restartpolicy created
# 查看Pod详情,发现nginx容器的健康检查失败 [root@master yaml]# kubectl describe pods pod-restartpolicy -n default ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 49s default-scheduler Successfully assigned default/pod-restartpolicy to work1.host.com Normal Pulled 48s kubelet Container image "docker.io/library/nginx:1.23.1" already present on machine Normal Created 48s kubelet Created container nginx Normal Started 48s kubelet Started container nginx Warning Unhealthy 19s (x3 over 39s) kubelet Liveness probe failed: HTTP probe failed with statuscode: 404 Normal Killing 19s kubelet Stopping container nginx
# 过一会之后,查看pod的状态,发现重启次数一直是0 [root@master yaml]# kubectl get pods pod-restartpolicy -n default NAME READY STATUS RESTARTS AGE pod-restartpolicy 0/1 Completed 0 2m7s
# 创建Pod [root@master yaml]# kubectl create -f Pod-Nodeaffinity-Preferred.yaml pod/pod-nodeaffinity-preferred created
# 查看Pod状态 # 发现Pod成功调度 [root@master yaml]# kubectl get pod pod-nodeaffinity-preferred -n default NAME READY STATUS RESTARTS AGE pod-nodeaffinity-preferred 1/1 Running 0 27s