Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pod in Kubernetes

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Pod is the smallest unit of Kubernetes scheduling. A Pod can contain one or more containers, so it can be seen as a logical host for internal containers.

Resource restriction

Resource requests and restrictions for pod and container

# cpu upper limit spec.containers [] .resources.requests.CPU # memory limit spec.containers [] .resources.requests.memory # basic cpu resource allocated on creation spec.containers [] .resources.requests.cpu # basic memory resource allocated on creation spec.containers [] .resources.requests.memory example demonstrates (operating on the master1 side) creating a pod2.yaml file vim pod2.yamlapiVersion: v1kind: name: frontend # Pod resource name Name spec: containers:-name: db # container 1 image: mysql env:-name: MYSQL_ROOT_PASSWORD value: "password" resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"-name: wp # container 2 name Image: wordpress resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m" create resource kubectl apply-f pod2.yaml view resource details kubectl describe pod frontend view Pod resource occupancy on the corresponding node kubectl describe nodes 192.168.142.130 View namespace kubectl get ns restart policy 1:Always: when the container terminates launch Always restart the container. The default policy 2:Onfailure: restart the container 3:Never when the container exits abnormally (the exit code is non-0): never restart resources when the container terminates. Note: restart pod resources is not supported in K8s. Only the delete and rebuild example demonstrates that the default restart policy is Alwayskubectl edit deploy# input / restartPolicy lookup restartPolicy: when Always# does not set the restart policy, the default is Always# to create the pod3.yaml file vim pod3.yamlapiVersion: v1kind: Podmetadata: name: foospec: containers:-name: busybox image: busybox args: # Parameter-/ bin/sh # in the shell environment-- c # command command-sleep 30 Exit 3 # container hibernates for 30 seconds after startup, and the return status code for exception exit is a non-0 value. # creating a resource kubectl apply-f pod3.yamlkubectl get pods# will cause an Error error and exit due to the exception just set. When you check it later, the RESTARTS restart value will become 1. Add the restart policy Never# to modify the pod3.yaml file vim pod3.yamlapiVersion: v1kind: Podmetadata: name: foospec: containers:-name: busybox image: busybox args:-/ bin/sh-- c-sleep 10 # modify the dormancy time to 10s restartPolicy: Never# add restart policy # create resource kubectl apply-f pod3.yaml health check Also known as probe (Probe) Note: rules can also define livenessProbe if the check fails Will kill the container and operate the ReadinessProbe according to Pod's restartPolicy. If the check fails, kubernetes will remove the Pod from the service endpoints backend node. Probe supports three check methods: httpGet sends a http request. Return 200400 range status code for successful exec execute Shell command return status code is 0 for successful tcpSocket launch TCP Socket establish successful example demonstration create resource # create pod4.yaml file vim pod4.yamlapiVersion: v1kind: Podmetadata: labels: test: liveness name: liveness-execspec: containers:-name: liveness image: busybox args:-/ bin/sh-- c-touch / tmp/healthy Sleep 30 Rm-rf / tmp/healthy # create an empty file and hibernate for 30s Delete the empty file livenessProbe: exec: # probe healthy command: # command command-cat # execute the view command-/ tmp/healthy # empty file initialDelaySeconds: 5 # the container is created 5 seconds after the creation of the container starts the health check periodSeconds: 5 # interval frequency of the check Refresh the resource at a rate of 5 seconds # create the resource kubectl apply-f pod4.yamlkubectl get pods# and the state is constantly changing It means that it is constantly checking, and then constantly implementing the restart strategy, in which the RESTARTS restart value will also increase. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report