亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Kubernetes stateful set講解以及一個基于postgreSQL的具體例子

標簽:
Kubernetes

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文件:

---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

webp

webp

webp

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

webp

生成的headless service:

webp

生成的pod:

webp

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

webp

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

webp

webp

statefulSet自动创建的persistentVolumeClaim:

webp

webp

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

webp

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

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

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

webp

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

sudo apt install pgadmin3

webp

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

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

webp

webp

也能成功连接:

webp

webp



作者:JerryWangSAP
链接:https://www.jianshu.com/p/39a3b323c635


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消