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

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

生成的headless service:

生成的pod:

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

使用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的具体例子的更多相关文章

  1. [转帖]Greenplum :基于 PostgreSQL 的分布式数据库内核揭秘 (上篇)

    Greenplum :基于 PostgreSQL 的分布式数据库内核揭秘 (上篇) https://www.infoq.cn/article/3IJ7L8HVR2MXhqaqI2RA 学长的文章.. ...

  2. WeText项目:一个基于.NET实现的DDD、CQRS与微服务架构的演示案例

    最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间的努力,凭着自己对微服务架构的理解,从无到有,基于.NET打造了一个演示微服务架 ...

  3. Pivotal开源基于PostgreSQL的数据库Greenplum

    http://www.infoq.com/cn/news/2015/11/PostgreSQL-Pivotal 近日,Pivotal宣布开源大规模并行处理(MPP)数据库Greenplum,其架构是针 ...

  4. 教你如何使用Java手写一个基于链表的队列

    在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ...

  5. JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识

    JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...

  6. GPUImage ==> 一个基于GPU图像和视频处理的开源iOS框架

    Logo 项目介绍: GPUImage是Brad Larson在github托管的开源项目. GPUImage是一个基于GPU图像和视频处理的开源iOS框架,提供各种各样的图像处理滤镜,并且支持照相机 ...

  7. Raknet是一个基于UDP网络传输协议的C++网络库(还有一些其它库,比如nanomsg,fastsocket等等)

    Raknet是一个基于UDP网络传输协议的C++网络库,允许程序员在他们自己的程序中实现高效的网络传输服务.通常情况下用于游戏,但也可以用于其它项目. Raknet有以下好处: 高性能 在同一台计算机 ...

  8. [转帖]Greenplum: 基于PostgreSQL的分布式数据库内核揭秘(下篇)

    Greenplum: 基于PostgreSQL的分布式数据库内核揭秘(下篇) http://www.postgres.cn/v2/news/viewone/1/454 原作者:姚延栋 创作时间:201 ...

  9. PostgreSQL 优势,MySQL 数据库自身的特性并不十分丰富,触发器和存储过程的支持较弱,Greenplum、AWS 的 Redshift 等都是基于 PostgreSQL 开发的

    PostgreSQL 优势 2016-10-20 21:36 686人阅读 评论(0) 收藏 举报  分类: MYSQL数据库(5)  PostgreSQL 是一个自由的对象-关系数据库服务器(数据库 ...

随机推荐

  1. 给JFinal添加 Sqlite 数据库支持

    [转自] http://my.oschina.net/u/237078/blog/69934 Sqlite 的单文件便携性.高性能在开发中方便性无与伦比,即使部署在中小型应用中也胜任有余. 在JFin ...

  2. poj1318 Word Amalgamation 字符串排序(qsort)

    Word Amalgamation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9794   Accepted: 4701 ...

  3. 【Python】探测网站是否可以访问

    首先贴上简陋的python脚本 #coding:utf-8 import urllib,linecache for line in linecache.updatecache('url.txt'): ...

  4. centos 7 查看系统版本信息

    2018-11-06 1. 查看版本号  CentOS的版本号信息一般存放在配置文件当中,在CentOS中,与其版本相关的配置文件中都有centos关键字,该文件一般存放在/etc/目录下,所以说我们 ...

  5. 《The Python Tutorial》——Errors and Exceptions 阅读笔记

    Errors and Exceptions 官方文档:https://docs.python.org/3.5/tutorial/errors.html python中所有的异常都继承自BaseExce ...

  6. zabbix CentOS7 配置安装

    一,LAMP+zabbix环境安装 官网: https://www.zabbix.com/download rpm -ivh https://mirrors.aliyun.com/zabbix/zab ...

  7. [Matlab] fprintf

    %s format as a string%d format with no fractional part (integer format)%f format as a oating-point v ...

  8. VS 连接数据库报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误

    VS报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...

  9. rsync的配置

    1. 安装rsync,如果要讲1.1.1.1的数据备份到2.2.2.2上,则在1.1.1.1上配置如下: /etc/rsyncd.conf uid = nobody gid = nobody use ...

  10. avalon实现日期联动

    前言 上一篇我们说了下Avalon的一些概念,以及一些主要特性,至于Avalon的一些基础教程,正美老师已经做了非常全面的讲述,参见:http://www.cnblogs.com/rubylouvre ...