Kubernetes快速部署高可用PostgreSQL

介紹在Kubernetes快速部署高可用PostgreSQL集羣的方法,基於Stolon項目的工做。node

Stolon是由3個部分組成的:mysql

  • keeper:管理PostgreSQL實例,匯聚到由sentinel(s)提供的clusterview。
  • sentinel:發現並監控keeper,而且計算最理想的clusterview。
  • proxy:客戶端的接入點。它鏈接到PostgreSQL的master而且強制關閉非選舉產生master。

Stolon用etcd或者consul做爲主要的集羣狀態存儲,默認使用Kubernetes的存儲來保存集羣的狀態。git

第一步,安裝Helm chart

# 獲取項目代碼,包含一個Helm Chart及其默認參數。
$ git clone https://github.com/lwolf/stolon-chart

# 安裝到命名空間stolon,helm chart名稱爲postgresql。
$ cd stolon-chart
$ helm install ./stolon --name postgresql --namespace stolon

此時,打開Dashboard面板,應該能夠看到命名空間stolon下的運行pod和服務等資源。github

第二步,修改配置參數

編輯stolon的服務,修改網絡地址類型爲NodePort(端口號30900),以便外部訪問。sql

kubeedit svc/waxen-seal-stolon-proxy -n stolon

修改後的配置文件以下:json

#supermap@podc01:~/openthings/$kubectl get svc/waxen-seal-stolon-proxy -n stolon -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2018-12-25T07:59:48Z"
  labels:
    app: waxen-seal-stolon-proxy
    chart: stolon-0.7.0
    component: stolon-proxy
    heritage: Tiller
    release: waxen-seal
  name: waxen-seal-stolon-proxy
  namespace: stolon
  resourceVersion: "596639"
  selfLink: /api/v1/namespaces/stolon/services/waxen-seal-stolon-proxy
  uid: 0d0ef0b1-081b-11e9-822a-7085c2a625da
spec:
  clusterIP: 10.103.227.204
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30900
    port: 5432
    protocol: TCP
    targetPort: 5432
  selector:
    app: waxen-seal-stolon-proxy
    chart: stolon-0.7.0
    component: stolon-proxy
    release: waxen-seal
    stolon-cluster: waxen-seal-stolon
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

第三步,安裝客戶端並訪問服務

獲取代理服務服務的登陸密碼(用戶名爲stolon):api

PGPASSWORD=$(kubectl get secret --namespace stolon waxen-seal-stolon -o jsonpath="{.data.pg_su_password}" | base64 --decode; echo)

echo $PGPASSWORD

宿主機上,安裝postgresql的客戶端:網絡

sudo apt install postgresql-client-common postgresql-client

測試一下(建立表、添加記錄、查詢記錄):session

#psql --host <IP> --port 30900 postgres -U stolon -W
psql --host localhost --port 30900 postgres -U stolon -W
#輸入上面得到的登陸密碼,回車。

postgres=# create table test (id int primary key not null,
value text not null);
CREATE TABLE

postgres=# insert into test values (1, 'value1');
INSERT 0 1

postgres=# select * from test;
id | value
---- --------
1 | value1
(1 row)

完畢。app

更多參考:

MySQL, Vitess: Scaling MySQL with Sugu Sougoumarane

相關文章
相關標籤/搜索