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 PodPreset in kubernetes

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use PodPreset in kubernetes, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

One: brief introduction

PodPreset resource objects are only supported by kubernetes 1.8 or later. Preset is a preset. Sometimes you want a batch of containers to inject some information, such as secret, volume, volume mount and environment variables, but do not want to change the tmeplate of these Pod one by one, so you can use PodPreset as a resource object. This object is used to inject some specific information into the Pod when it is created. This information can include secret, volume, volume mount, and environment variables.

Pod Preset is an API resource used to inject additional runtime requirements into a Pod when it is created. You can use label selector to specify which Pod to apply Pod Preset for. Using Pod Preset allows authors of pod templates to not have to explicitly provide all the information for each Pod. In this way, the author of the pod template does not need to know all the details about the service.

Two: enable PodPreset

1. Enable API: add-- runtime-config=settings.k8s.io/v1alpha1/podpreset to the apiserver configuration file

two。 Open the admission controller: add-admission-control=PodPreset to the apiserver configuration file

Third: PodPreset working mechanism

When a Pod creation request occurs, the system does the following:

1. Retrieve all available PodPresets.

two。 Check the tag on the PodPreset tag selector to see if it matches the tag on the Pod you are creating.

3. Try to merge the various resources defined by PodPreset into the Pod you are creating.

4. When an error occurs, an event of a record merge error is raised on the Pod, and PodPreset does not inject any resources into the created Pod.

5. Comment on the modified Pod spec just generated to indicate that it has been modified by PodPreset. The format of the comment is podpreset.admission.kubernetes.io/podpreset- ":".

Each Pod can match zero or more Pod Prestet; and each PodPreset can be applied to zero or more Pod. When PodPreset is applied to one or more Pod, Kubernetes modifies the Pod Spec. For changes to Env, EnvFrom, and VolumeMounts, Kubernetes modifies container spec; for all containers in Pod to Volume, and Kubernetes modifies Pod Spec.

Four: examples

1. Add environment variables and PodPreset for storage volumes

PodPreset

Kind: PodPreset

ApiVersion: settings.k8s.io/v1alpha1

Metadata:

Name: allow-database

Namespace: myns

Spec:

Selector:

MatchLabels:

Role: frontend

Env:

-name: DB_PORT

Value: "6379"

VolumeMounts:

-mountPath: / cache

Name: cache-volume

Volumes:

-name: cache-volume

EmptyDir: {}

Pod submitted by the user

ApiVersion: v1

Kind: Pod

Metadata:

Name: website

Labels:

App: website

Role: frontend

Spec:

Containers:

-name: website

Image: ecorp/website

Ports:

-containerPort: 80

After the admission control PodPreset, Pod automatically increases the environment variables and storage volumes

ApiVersion: v1

Kind: Pod

Metadata:

Name: website

Labels:

App: website

Role: frontend

Annotations:

Podpreset.admission.kubernetes.io/allow-database: "resource version"

Spec:

Containers:

-name: website

Image: ecorp/website

VolumeMounts:

-mountPath: / cache

Name: cache-volume

Ports:

-containerPort: 80

Env:

-name: DB_PORT

Value: "6379"

Volumes:

-name: cache-volume

EmptyDir: {}

2.ConfigMap example

ApiVersion: v1

Kind: ConfigMap

Metadata:

Name: etcd-env-config

Data:

Number_of_members: "1"

Initial_cluster_state: new

Initial_cluster_token: DUMMY_ETCD_INITIAL_CLUSTER_TOKEN

Discovery_token: DUMMY_ETCD_DISCOVERY_TOKEN

Discovery_url: http://etcd_discovery:2379

Etcdctl_peers: http://etcd:2379

Duplicate_key: FROM_CONFIG_MAP

REPLACE_ME: "a value"

Kind: PodPreset

ApiVersion: settings.k8s.io/v1alpha1

Metadata:

Name: allow-database

Namespace: myns

Spec:

Selector:

MatchLabels:

Role: frontend

Env:

-name: DB_PORT

Value: 6379

-name: duplicate_key

Value: FROM_ENV

-name: expansion

Value: $(REPLACE_ME)

EnvFrom:

-configMapRef:

Name: etcd-env-config

VolumeMounts:

-mountPath: / cache

Name: cache-volume

-mountPath: / etc/app/config.json

ReadOnly: true

Name: secret-volume

Volumes:

-name: cache-volume

EmptyDir: {}

-name: secret-volume

SecretName: config-details

Pod submitted by the user

ApiVersion: v1

Kind: Pod

Metadata:

Name: website

Labels:

App: website

Role: frontend

Spec:

Containers:

-name: website

Image: ecorp/website

Ports:

-containerPort: 80

After the admission control PodPreset, Pod automatically increases the ConfigMap environment variable

ApiVersion: v1

Kind: Pod

Metadata:

Name: website

Labels:

App: website

Role: frontend

Annotations:

Podpreset.admission.kubernetes.io/allow-database: "resource version"

Spec:

Containers:

-name: website

Image: ecorp/website

VolumeMounts:

-mountPath: / cache

Name: cache-volume

-mountPath: / etc/app/config.json

ReadOnly: true

Name: secret-volume

Ports:

-containerPort: 80

Env:

-name: DB_PORT

Value: "6379"

-name: duplicate_key

Value: FROM_ENV

-name: expansion

Value: $(REPLACE_ME)

EnvFrom:

-configMapRef:

Name: etcd-env-config

Volumes:

-name: cache-volume

EmptyDir: {}

-name: secret-volume

SecretName: config-details

Thank you for reading this article carefully. I hope the article "how to use PodPreset in kubernetes" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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