Stateful Set is a concept introduced in Kubernetes 1.9 to manage Stateful applications.

Kubernetes

Kubernetes. IO/docs/concep…

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 consists of the following parts:

1. Headless Service used to define DNS domains

VolumeClaimTemplates to create PersistentVolumes

3. Define StatefulSet for the specific application

Here is a yamL file for StatefulSet in action:

---

apiVersion: apps/v1

kind: StatefulSet

metadata:

name: ads-db-statefulset

labels:

component: ads

module: db

spec:

serviceName: ads-db-service

replicas: 1

selector:

matchLabels:

component: ads

module: db

template:

metadata:

labels:

component: ads

module: db

spec:

volumes:

- name: init

secret:

secretName: ads-db-secret

items:

- key: initdb.sql

path: initdb.sql

containers:

- name: ads-db-pod

image: Postgres: 9.6

ports:

- containerPort: 5432

name: ads-db-port

volumeMounts:

- name: ads-db-volume

mountPath: /var/lib/postgresql/data/

- name: init

mountPath: /docker-entrypoint-initdb.d/

env:

- name: PGDATA

valueFrom:

configMapKeyRef:

name: ads-db-configmap

key: pgdata_value

- name: POSTGRES_PASSWORD

valueFrom:

secretKeyRef:

name: ads-db-secret

key: postgres_password_value

volumeClaimTemplates:

- metadata:

name: ads-db-volume

labels:

component: ads

module: db

spec:

accessModes: [ "ReadWriteOnce" ]

resources:

requests:

storage: 1Gi
Copy the code

Use Kubectl get StatefulSet to view the generated StatefulSet:

Generated Headless service:

Generated pods:

When I changed replicas in the Statefulset YAML file from 1 to 3, sure enough, I observed that two new pods were being started with names that met the naming convention -x.

Use Kubectl Describe to view the statefulSet details created:

PersistentVolumeClaim automatically created by statefulSet:

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

Use the following command to log in to the postgreSQL server provided by StatefulSet:

Kubectl run tester it –rm –image=postgres:9.6 –env=”PGCONNECT_TIMEOUT=5″ –command — bash

When we see root$, we are connected to pod.

Run the following command to connect to the postgreSQL server:

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

You can also use pgadmin to connect to the postgreSQL server in statefulSet using a graphical interface:

sudo apt install pgadmin3

Port forwarding, so we can connect with localhost: 5432:

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

Can also connect successfully:

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: