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

Introduction to ReplicaSet and StatefulSet controllers

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

Share

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

Today, the editor to share with you is a detailed introduction of ReplicaSet and StatefulSet controller, I believe that most people do not understand, in order to make you better understand, the editor summed up the following content, do not say much, let's look down.

# I. Common Pod controller and its meaning

# 1 、 ReplicaSets

ReplicaSet is the next generation replica controller. The only difference between ReplicaSet and Replication Controller is the current selector support. Replication Controller only supports equation-based selector (env=dev or mentality selector Qa), but ReplicaSet also supports new, set-based selector (version in (v1.0, v2.0) or env notin (dev, qa)).

Most kubectl commands that support Replication Controller also support ReplicaSets. There is one exception to the rolling-update command. If you want the scrolling update feature, consider using Deployments. In addition, the rolling-update command is required, while Deployments is declarative, so we recommend using Deployments through the rollout command.

Although ReplicaSets can be used independently, today it is mainly used by Deployments as a mechanism for coordinating pod creation, deletion and update. When using Deployments, you don't have to worry about managing the ReplicaSets they create. Deployments owns and manages its ReplicaSets.

# 2 、 Deployment

Deployment provides declarative updates for Pod and Replica Set (next-generation Replication Controller).

You only need to describe the desired goal state in Deployment, and Deployment controller will help you change the actual state of Pod and Replica Set to your goal state. You can define a new Deployment, or you can create a new one to replace the old Deployment.

A typical use case is as follows:

1. Use Deployment to create a ReplicaSet. ReplicaSet creates a pod in the background. Check the startup status to see if it succeeds or fails.

two。 Then, declare the new state of the Deployment by updating the Pod's PodTemplateSpec field. This creates a new ReplicaSet,Deployment that moves the pod from the old ReplicaSet to the new ReplicaSet at a controlled rate.

3. If the current state is unstable, roll back to the previous Deployment revision. The revision of Deployment is updated with each rollback.

4. Expand Deployment capacity to meet higher load.

5. Pause Deployment to apply multiple fixes for PodTemplateSpec, and then come back online.

6. Judge whether the online hang resides according to the status of Deployment.

7. Clean up old and unnecessary ReplicaSet.

# 3 、 StatefulSet

StatefulSet is designed to solve the problem of stateful service (corresponding to Deployments and ReplicaSets are designed for stateless service), and its application scenarios include

1. Stable persistent storage, that is, Pod can still access the same persistent data after rescheduling, which is based on PVC.

two。 Stable network sign, that is, its PodName and HostName remain unchanged after Pod rescheduling, which is based on Headless Service (that is, Service without Cluster IP).

3. Orderly deployment, orderly expansion, that is, Pod is sequentially deployed or expanded according to the defined order (that is, from 0 to Nmuri 1, all previous Pod must be Running and Ready states before the next Pod runs), which is implemented based on init containers.

4. Orderly contraction, orderly deletion (I. e., from Nmuri 1 to 0)

From the above application scenario, we can see that StatefulSet consists of the following parts:

1. Headless Service used to define the network flag (DNS domain)

two。 VolumeClaimTemplates for creating PersistentVolumes

3. Define the application-specific StatefulSet

The DNS format for each Pod in StatefulSet is statefulSetName- {0..N-1} .serviceName.namespace.svc.cluster.local, where

1.serviceName is the name of Headless Service

2.0..N-1 is the serial number where Pod is located, starting from 0 to NMUI 1

3.statefulSetName is the name of StatefulSet

The namespace,Headless Servic and StatefulSet where the 4.namespace is located must be on the same namespace

5.svc.cluster.local is Cluster Domain

# 4 、 DaemonSet

DaemonSet ensures that a copy of the container is run on each Node, which is often used to deploy cluster logging, monitoring, or other system management applications. Typical applications include:

Log collection, such as fluentd,logstash

System monitoring, such as Prometheus Node Exporter,collectd,New Relic agent,Ganglia gmond

System programs, such as kube-proxy, kube-dns, glusterd, ceph, etc.

# II. Examples of commonly used Pod controllers

# 1 、 Deployment

ApiVersion: apps/v1kind: Deploymentmetadata: name: myapp-deployspec:replicas: 5selector: matchLabels:app: myapprelease: canarytemplate:metadata:labels:app: myapprelease: canaryspec:containers:- name: myappimage: ikubernetes/myapp:v2ports:- name: httpdcontainerPort: 80

# 2. Deployment + DaemonSet

ApiVersion: apps/v1kind: Deploymentmetadata:name: redisnamespace: defaultspec:replicas: 1selector:matchLabels:app: redisrole: logstortemplate:metadata:labels:app: redisrole: logstorspec:containers:- name: redisimage: redis:4.0-alpineports:- name: rediscontainerPort: 6379

ApiVersion: apps/v1

Kind: DaemonSet

Metadata:

Name: filebeat-ds

Spec:

Selector:

MatchLabels:

App: filebeat

Release: stable

Template:

Metadata:

Labels:

App: filebeat

Release: stable

Spec:

Containers:

Name: filebeat

Image: ikubernetes/filebeat:5.6.5-alpine

Env:name: REDIS_HOST

Value: redis.default.svc.cluster.localname: REDIS_LOG_LEVEL

Value: info###3 、 StatefulSet

ApiVersion: v1

Kind: Service

Metadata:

Name: nginx

Labels:

App: nginx

Spec:

Ports:

Port: 80

Name: web

ClusterIP: None

Selector:

App: nginx

ApiVersion: apps/v1beta1

Kind: StatefulSet

Metadata:

Name: web

Spec:

ServiceName: "nginx"

Replicas: 2

Template:

Metadata:

Labels:

App: nginx

Spec:

Containers:

Name: nginx

Image: gcr.io/google_containers/nginx-slim:0.8

Ports:containerPort: 80

Name: web

VolumeMounts:name: www

MountPath: / usr/share/nginx/html

VolumeClaimTemplates:metadata:

Name: www

Annotations:

Volume.alpha.kubernetes.io/storage-class: anything

Spec:

AccessModes: ["ReadWriteOnce"]

Resources:

Requests:

Storage: 1Gi# III. Pod complete sample fields explain the complete contents of the pod definition file in yaml format:

ApiVersion: v1 # required, version number, such as v1

Kind: Pod # required, Pod

Metadata: # required, metadata

Name: string # required, Pod name

Namespace: required for string #, namespace to which Pod belongs

Labels: # Custom tag

Name: string # Custom tag name

Annotations: # Custom comment list name: string

Spec: # required, detailed definition of container in Pod

Containers: # required, container list in Pod name: string # required, container name

Image: string # is required, and the image name of the container

ImagePullPolicy: [Always | Never | IfNotPresent] # Policy for obtaining images Alawys means downloading images IfnotPresent means to use local images first, otherwise downloading images, Nerver means only local images are used

Command: [string] # list of startup commands for the container, if not specified, use the startup commands used when packaging

Args: [string] # list of startup command parameters for the container

WorkingDir: working directory of the string # container

VolumeMounts: # configuration of storage volumes mounted to the container name: string # refers to the name of the shared storage volume defined by pod, using the volume name defined in the volumes [] section

MountPath: the absolute path of mount for string # storage volumes in the container, which should be less than 512 characters

ReadOnly: whether boolean # is in read-only mode

Ports: # list of port library numbers to be exposed name: string # port number name

ContainerPort: the port number that the int # container needs to listen to

HostPort: the port number to be listened to by the host where the int # container resides, which is the same as Container by default

Protocol: string # port protocol, supports TCP and UDP, default TCP

Env: # list of environment variables to be set before the container runs name: string # environment variable name

Value: the value of the string # environment variable

Resources: # Resource limits and request settings

Limits: # Settings of resource limits

Cpu: the limit of string # Cpu (in core), which will be used for the parameter docker run-- cpu-shares

Memory: string # memory limit, which can be in Mib/Gib and will be used for the docker run-- memory parameter

Requests: # Settings for resource requests

Cpu: string # Cpu request, the initial available quantity initiated by the container

Memory: string # has clear memory and the initial available quantity of container startup

LivenessProbe: # set the health check of a container in Pod. If the probe fails to respond several times, the container will be automatically restarted. The check methods include exec, httpGet and tcpSocket. You only need to set one of these methods for a container.

Exec: # set the check mode in Pod container to exec mode

Command: [string] # commands or scripts that need to be developed in exec mode

HttpGet: # set the health check method of a container in Pod to HttpGet, and you need to set Path and port

Path: string

Port: number

Host: string

Scheme: string

HttpHeaders:name: string

Value: string

TcpSocket: # set the health check mode of a container in Pod to tcpSocket mode

Port: number

InitialDelaySeconds: the time of the first probe after the 0 # container is started (in seconds)

TimeoutSeconds: 0 # timeout for container health check probe waiting for response (in seconds). Default is 1 second.

PeriodSeconds: 0 # regular detection time setting for container monitoring check (in seconds). Default is 10 seconds.

SuccessThreshold: 0

FailureThreshold: 0

SecurityContext:

Privileged: false

RestartPolicy: [Always | Never | OnFailure] # Pod restart policy. Always indicates that once the operation is terminated, kubelet will restart. OnFailure indicates that only Pod will restart if it exits with a non-0 exit code. Nerver indicates that the Pod will not be restarted.

NodeSelector: obeject # setting NodeSelector means dispatching the Pod to the node containing this label and specifying it in key:value format

ImagePullSecrets: # secret name used when Pull is mirrored, specifying name: string in key:secretkey format

HostNetwork: whether false # uses host network mode. Default is false. If set to true, host network is used.

Volumes: # define shared storage volume list on this pod name: string # shared storage volume name (there are many volumes types)

EmptyDir: a storage volume of type emtyDir, a temporary directory with the same life cycle as Pod. Null value

HostPath: string # storage volume of type hostPath, indicating the directory of the host where the Pod is mounted

Path: the directory of the host where string # Pod resides, which will be used for the directory of mount in the same period

Secret: # Storage volume of type secret, which mounts the cluster and defined secre objects into the container

Scretname: string

Items:key: string

Path: string

ConfigMap: # Storage volume of type configMap, which mounts predefined configMap objects into the container

Name: string

Items:key: string

Path: string, after reading the above, do you have a general understanding of ReplicaSet and StatefulSet controller? If you want to know more about the content of the article, welcome to follow the industry information channel, 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