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

Kubernetes series tutorials (5) getting to know the core concept pod

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Write at the front

The previous series of articles have introduced the kubernetes architecture, installation, upgrade and quick start. Readers have already had a preliminary understanding and understanding of kubernetes through the practical operation of the article. From this chapter, we will gradually introduce the basic concepts and core concepts in kubernetes, including: namespace,labels,annotations,pods,volumes, etc.; the core concepts include various controller in kubernetes, including the following:

Application replica controllers include Deployments,ReplicaSets,DaemonSets,StatefulSets; batch task controller Jobs and CronJob storage controller PersistentVoloume,PersistentVolumeClaim,StorageClass; service load balancing Service,Ingress,NetworkPolicy and DNS name resolution

Configuration and key ConfigMaps and Secrets

This article starts with the most basic concept of pod, followed by a step-by-step introduction of application deployment, storage, load balancing and other related controllers. Kubernetes is composed of several different controllers, each of which performs different functions.

1. In-depth study of pod1.1 Container introduction

Container is a portable, lightweight container virtualization technology, using linux cggroup technology to achieve the isolation of various resources, such as cpu,memory,pid,mount,IPC, compared with virtualization technology such as KVM, container technology is more lightweight, its generation mainly solves the problem of environment release. At present, the mainstream container technology is docker, when it comes to containers, it is generally equivalent to docker.

To run the container, you first need to have an image. The application and the environment that the application depends on run in the container. Instead of running container directly in kubernetes, it runs pod. A pod contains multiple container,container sharing the same namespace,network,storage, and so on. The image is stored in a private image or a public image, and the runtime pulls it to run locally through docker image pull. There are two pull strategies for images:

ImagePullPolicy is Always, regardless of whether it is downloaded locally or not, ImagePullPolicy is IfNotPresent. The default image pull policy is that it does not exist locally and the concept of pulling 1.2Pod is introduced.

Pods is the smallest scheduling unit in kubernetes. Pods runs one or more container,container network ip resources that share pod, stores volume resources, computing and other resources, and facilitates rapid access and interaction between container within pod.

As shown in the figure above, Pod is usually used in two ways:

One container runs in Pod. The most frequently used mode, container encapsulates scheduling in pod, is almost the same. However, K8s does not directly manage multiple containers running in Pod, and multiple containers are encapsulated in pod to schedule together. It is suitable for scenarios where there are data exchanges and calls between containers, such as sharing the same network namespace, storage namespace, process namespace and so on within app+redis,pod. 1.3 how to create a pod

In kubernetes, resources are defined in the way of life declaration, that is, by defining the required resources in the yaml file, kubernetes generates the required resources (match the current state to desired state) according to the resources defined in the yaml file through controller-manager. Resources are usually defined as yaml files in kubernetes, and then the configuration is applied through the kubectl create-f file .yaml, as shown below to create a nginx application.

1. Write a yaml file and define a pod resource

[root@node-1 demo] # cat nginx.yaml apiVersion: v1kind: Podmetadata: name: nginx-demo labels: name: nginx-demospec: containers:-name: nginx-demo image: nginx:1.7.9 imagePullPolicy: IfNotPresent ports:-name: nginx-port-80 protocol: TCP containerPort: 80

For the configuration file, the description is as follows:

For the version used by apiVersion api, kubectl api-versions can view the list of versions supported by the current system kind specifies the resource type, indicates the resource type of Pod metadata specifies the metadata of Pod, metadata.name specifies the name, metadata.labels specifies the tag to which Pod belongs, spec specifies the template attribute of Pod, spec.containers configuration container information, spec.containers.name specifies name, spec.containers.image specifies the name of container image. Spec.containers.imagePullPolicy is the download method of the image. IfNotPresent means to download when the image does not exist. Spec.containers.ports.name specifies the name of port. The spec.containers.ports.protocol protocol type is TCP,spec.containers.ports.containerPort, which is the container port.

2. Create pod application

[root@node-1 demo] # kubectl apply-f nginx.yaml pod/nginx-demo created

3. Access the application

Obtain the IP address of the container [root@node-1 demo] # kubectl get pods-o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESdemo-7b86696648-8bq7h 1 Running 0 8h 10.244.1.11 node-2 demo-7b86696648-8qp46 1 node 1 Running 08h 10.244.1.10 -2 demo-7b86696648-d6hfw 1 Running 0 8h 10.244.1.12 node-2 nginx-demo 1 Running 050s 10.244.2.11 node-3 visit site content: [root@node-1 demo] # curl http://10.244.2.11Welcome to nginx! Body {width: 35eme; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif;} Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.Commercial support is available atnginx.com.

Thank you for using nginx.

Earlier, we learned that kubernetes supports rolling upgrade RollingUpdate, flexible expansion of replicas and other features. How to do rolling upgrade for Pod to ensure uninterrupted business, and how to improve the number of copies of Pod to ensure high availability? The answer is: no. Pod is a single and cannot support some advanced features, which can only be supported by advanced replica controllers such as ReplicaSets,Deployments,StatefulSets,DaemonSets. Pod is seldom used in practical application. In addition to testing and running some simple functions, it is recommended to use Deployments instead. The definition of Pod is embedded in the replica controller in the way of Template.

two。 How to write yaml files

We mentioned earlier that kubernetse deploys applications in a declarative manner, and the deployment of applications is defined in yaml files. How to write yaml files for applications? let me share the techniques used in the past two centuries:

1. Quickly generate templates by defining templates and generate them by kubectl create apps-o yaml-- dry-run.-- dry-run is only a trial run and does not actually run in K8s cluster. Output yaml format files by specifying-o yaml, which can be modified based on templates after generation, as shown below:

[root@node-1 demo] # kubectl create deployment demo-image=nginx:latest-dry-run o yamlapiVersion: apps/v1kind: Deploymentmetadata: creationTimestamp: null labels: app: demo name: demospec: replicas: 1 selector: matchLabels: app: demo strategy: {} template: metadata: creationTimestamp: null labels: app: demospec: containers:-image: nginx:latest name: nginx resources: {} status: {}

2. Explain command. The explain command can be called a syntax querier. You can find out the meaning of each field, how to use it and how to use it. If you want to view other fields supported by containers in the spec of Pod, you can query them by kubectl explain Pod.spec.containers, as follows:

[root@node-1 demo] # kubectl explain Pods.spec.containersKIND: PodVERSION: v1RESOURCE: containers DESCRIPTION: List of containers belonging to the pod. Containers cannot currently be added or removed. There must be at least one container in a Pod. Cannot be updated. A single application container that you want to run within a pod.FIELDS: args # command parameter Arguments to the entrypoint. The docker image's CMD 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 image # image definition Docker image name. More info: https://kubernetes.io/docs/concepts/containers/images This field is optional to allow higher level config management to default or override container images in workload controllers like Deployments and StatefulSets. Ports # Port definition 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. ReadinessProbe # Health check 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 # resource settings Compute Resources required by this container. Cannot be updated. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/... Omit part of the output. VolumeMounts # Mount storage Pod volumes to mount into the container's filesystem. Cannot be updated. WorkingDir Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image. Cannot be updated.

Explanation about the content of explain

Represents an object followed by a string that is followed by a list. The list needs to start with -, and multiple objects can be written to represent an object, and the object contains multiple attributes.

If you continue with the above, if you need to view the resource resource definition, you can use explain pods.spec.containers.resource to see how to use it.

Through the introduction of the above two tools, usually in the daily work to find the preparation of yaml file deployment application map, it is recommended to write more times by hand, pay attention to syntax locking, write a few more times to be familiar with. Pod is designed to have many features, such as resource allocation, health check, storage mount and so on (see appendix article). Later we will introduce in detail that Pod will be embedded in the replica controller such as Deployments in the form of Template.

4. Appendix

Container image introduction: https://kubernetes.io/docs/concepts/containers/images/

Introduction to Pod: https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/

Resource limited memory resources: https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/

Resource qualifies CPU resources: https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/

Pod mount storage: https://kubernetes.io/docs/tasks/configure-pod-container/configure-volume-storage/

Pod configuration Health check: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/

When your talent can't support your ambition, you should calm down and study.

Return to the kubernetes series tutorial directory

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