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

i042416發表於2018-11-20

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

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

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

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

使用kubectl get statefulset檢視生成的statefulset:

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

生成的headless service:

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

生成的pod:

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

當我把statefulset yaml檔案裡的replicas從1改成3之後,果然觀察到有兩個新的pod正在啟動,並且名稱滿足命名規範<stateful set name >-X。

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

使用kubectl describe檢視建立的statefulset明細:

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

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

statefulSet自動建立的persistentVolumeClaim:

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

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

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

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

看到root$之後,說明我們已經連線上pod了。

使用如下命令列連線postgreSQL伺服器:

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

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

當然如果不用命令列,也可以使用pgadmin,以圖形化介面連線statefulSet裡的postgreSQL伺服器:

sudo apt install pgadmin3

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

進行埠轉發,這樣我們可以使用localhost:5432進行連線:

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

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

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

也能成功連線:

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

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

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2220825/,如需轉載,請註明出處,否則將追究法律責任。

相關文章