Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子

Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子,第1张

概述Stateful Set是Kubernetes 1.9版本新引入的一个概念,用于管理有状态的应用。 Kubernetes官方文档: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/ Manages the deployment and scaling of a set of Pods, and provid

Stateful Set是Kubernetes 1.9版本新引入的一个概念,用于管理有状态的应用。

Kubernetes官方文档:

https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/

Manages the deployment and scaling of a set of Pods,and provIDes guarantees about the ordering and uniqueness of these Pods.

like a Deployment,a StatefulSet manages Pods that are based on an IDentical container spec. Unlike a Deployment,a StatefulSet maintains a sticky IDentity for each of their Pods. These pods are created from the same spec,but are not interchangeable: each has a persistent IDentifIEr that it maintains across any rescheduling.

StatefulSet由以下几个部分组成:

1. 用于定义网络标志(DNS domain)的headless Service

2. 用于创建PersistentVolumes的volumeClaimTemplates

3. 定义具体应用的StatefulSet

下面我给出了一个实际应用中的StatefulSet的yaml文件:

@H_502_26@---APIVersion: apps/v1kind: StatefulSetMetadata:name: ads-db-statefulsetlabels:component: adsmodule: dbspec:servicename: ads-db-servicereplicas: 1selector:matchLabels:component: adsmodule: dbtemplate:Metadata:labels:component: adsmodule: dbspec:volumes:- name: initsecret:secretname: ads-db-secretitems:- key: initdb.sqlpath: initdb.sqlcontainers:- name: ads-db-podimage: postgres:9.6ports:- containerPort: 5432name: ads-db-portvolumeMounts:- name: ads-db-volumemountPath: /var/lib/postgresql/data/- name: initmountPath: /docker-entrypoint-initdb.d/env:- name: PGDATAvalueFrom:configMapKeyRef:name: ads-db-configmapkey: pgdata_value- name: POSTGRES_PASSWORDvalueFrom:secretKeyRef:name: ads-db-secretkey: postgres_password_valuevolumeClaimTemplates:- Metadata:name: ads-db-volumelabels:component: adsmodule: dbspec:accessModes: [ "ReaDWriteOnce" ]resources:requests:storage: 1Gi

使用kubectl get statefulset查看生成的statefulset:

生成的headless service:

生成的pod:

当我把statefulset yaml文件里的replicas从1改成3之后,果然观察到有两个新的pod正在启动,并且名称满足命名规范

使用kubectl describe查看创建的statefulset明细:

statefulSet自动创建的persistentVolumeClaim:

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".

The default database enCoding has accordingly been set to "UTF8".

The default text search configuration will be set to "english".

Data page checksums are Disabled.

fixing permissions on existing directory /var/lib/postgresql/data/pgdata ... ok

creating subdirectorIEs ... ok

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting dynamic shared memory implementation ... posix

creating configuration files ... ok

running bootstrap script ... ok

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

Success. You can Now start the database server using:

pg_ctl -D /var/lib/postgresql/data/pgdata -l logfile start

使用下面的命令登录到statefulset提供的postgresql服务器上:

1. kubectl run tester -it --rm --image=postgres:9.6 --env="PGCONNECT_TIMEOUT=5" --command -- bash

看到root$之后,说明我们已经连接上pod了。

使用如下命令行连接postgresql服务器:

psql -h ads-db-statefulset-0.ads-db-service -p 5432 -U adsuser -W ads

当然如果不用命令行,也可以使用pgadmin,以图形化界面连接statefulSet里的postgresql服务器:

sudo apt install pgadmin3

进行端口转发,这样我们可以使用localhost:5432进行连接:

kubectl port-forward ads-db-statefulset-0 5432:5432

也能成功连接:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

总结

以上是内存溢出为你收集整理的Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子全部内容,希望文章能够帮你解决Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1163703.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存