docker容器化搭建postgres集群

postgres_cluster容器化Dockerfile

FROM centos:7
RUN rm -rf /etc/yum.repos.d/* && \
mkdir -p /home/postgres/{data,template,config,logs} && \
curl http://easyk8s-public.oss-cn-hangzhou.aliyuncs.com/Patroni/base_epel_pg.repo -o /etc/yum.repos.d/base_epel_pg.repo && \
curl http://easyk8s-public.oss-cn-hangzhou.aliyuncs.com/Patroni/patroni.yml.j2 -o /home/postgres/template/patroni.yml.j2 && \
curl http://easyk8s-public.oss-cn-hangzhou.aliyuncs.com/Patroni/patroni_callback.sh.j2 -o /home/postgres/template/patroni_callback.sh.j2 && \
curl http://easyk8s-public.oss-cn-hangzhou.aliyuncs.com/Patroni/start_Patroni.sh -o /home/postgres/start_Patroni.sh && \
curl http://easyk8s-public.oss-cn-hangzhou.aliyuncs.com/Patroni/create_config.py -o /home/postgres/create_config.py && \
yum install -y kde-l10n-Chinese glibc-common centos-release-scl-rh \
postgresql14 postgresql14-server postgresql14-libs \
postgresql14-contrib postgresql14-devel expect python3 \
python3-devel python3-psycopg2 centos-release-scl-rh sudo iproute && \
python3 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple && \
python3 -m pip install psycopg2-binary -i https://pypi.tuna.tsinghua.edu.cn/simple && \
python3 -m pip install patroni[etcd] -i https://pypi.tuna.tsinghua.edu.cn/simple && \
python3 -m pip install jinja2 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
echo 'LANG="zh_CN.uft8"' > /etc/locale.conf && localedef -c -f UTF-8 -i zh_CN zh_CN.uft8 && \
echo 'LANG="zh_CN.UTF-8"' > /etc/environment && echo 'LC_ALL="zh_CN.UTF-8"' > /etc/environment && \
echo "Asia/shanghai" > /etc/timezone && \
cat /etc/sudoers | grep postgres || sed -i "100apostgres        ALL=(root)        NOPASSWD: ALL" /etc/sudoers && \
chown postgres:postgres -R /home/postgres/ && \
yum clean all && rm -rf /var/cache/yum/*
ENV LC_ALL=zh_CN.uft8 LANG=zh_CN.uft8 LANGUAGE=zh_CN.uft8
USER postgres
WORKDIR /home/postgres/
CMD ["bash","/home/postgres/start_Patroni.sh"]

主機IP規(guī)劃及組件

  • 192.168.26.101 [etcd,postgres_cluster]
  • 192.168.26.101 [etcd,postgres_cluster]
  • 192.168.26.101 [etcd,postgres_cluster]
  • 192.168.26.200 [VIP地址配合postgres_cluster使用]
etcd集群快速部署
//所有主機需要創(chuàng)建數(shù)據(jù)目錄,還有權(quán)限需要注意

//192.168.26.101
HOST_NAME=etcd-1
ETCD_ENDPOINT=etcd-1=http://192.168.26.101:2380,etcd-2=http://192.168.26.102:2380,etcd-3=http://192.168.26.103:2380
HOST_IP=`python -c "import socket;res = socket.gethostbyname(socket.gethostname());print(res)"`
ETCD_DATA=/opt/etcd/data

docker run -d --restart=always --net=host \
-v $ETCD_DATA:/etcd-data \
--name $HOST_NAME \
quay.io/coreos/etcd:v3.5.7 \
/usr/local/bin/etcd \
-name $HOST_NAME \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://$HOST_IP:2380 \
--initial-cluster "$ETCD_ENDPOINT" \
--initial-cluster-token tkn \
--initial-cluster-state new \
--log-level info \
--logger zap \
--log-outputs stderr

//192.168.26.102
HOST_NAME=etcd-2
ETCD_ENDPOINT=etcd-1=http://192.168.26.101:2380,etcd-2=http://192.168.26.102:2380,etcd-3=http://192.168.26.103:2380
HOST_IP=`python -c "import socket;res = socket.gethostbyname(socket.gethostname());print(res)"`
ETCD_DATA=/opt/etcd/data

docker run -d --restart=always --net=host \
-v $ETCD_DATA:/etcd-data \
--name $HOST_NAME \
quay.io/coreos/etcd:v3.5.7 \
/usr/local/bin/etcd \
-name $HOST_NAME \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://$HOST_IP:2380 \
--initial-cluster "$ETCD_ENDPOINT" \
--initial-cluster-token tkn \
--initial-cluster-state new \
--log-level info \
--logger zap \
--log-outputs stderr



//192.168.26.103
HOST_NAME=etcd-3
ETCD_ENDPOINT=etcd-1=http://192.168.26.101:2380,etcd-2=http://192.168.26.102:2380,etcd-3=http://192.168.26.103:2380
HOST_IP=`python -c "import socket;res = socket.gethostbyname(socket.gethostname());print(res)"`
ETCD_DATA=/opt/etcd/data

docker run -d --restart=always --net=host \
-v $ETCD_DATA:/etcd-data \
--name $HOST_NAME \
quay.io/coreos/etcd:v3.5.7 \
/usr/local/bin/etcd \
-name $HOST_NAME \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://$HOST_IP:2380 \
--initial-cluster "$ETCD_ENDPOINT" \
--initial-cluster-token tkn \
--initial-cluster-state new \
--log-level info \
--logger zap \
--log-outputs stderr

# 驗證ETCD集群
# docker exec -it etcd-[1/2/3] bash
$ export ETCDCTL_API=3
$ ENDPOINTS=192.168.26.101:2379,192.168.26.102:2379,192.168.26.103:2379
$ etcdctl --write-out=table --endpoints=$ENDPOINTS member list
$ etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint health
$ etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
etcd快速腳本
# cat start_etcd_cluster.sh
#!/bin/bash
# 需要修改主機名HOST_NAME及ETCD_ENDPOINT
HOST_NAME=etcd-1
ETCD_ENDPOINT=etcd-1=http://192.168.26.101:2380,etcd-2=http://192.168.26.102:2380,etcd-3=http://192.168.26.103:2380
HOST_IP=`python -c "import socket;res = socket.gethostbyname(socket.gethostname());print(res)"`
ETCD_DATA=/opt/etcd/data

# 此部分內(nèi)容僅僅用于測試環(huán)境使用
# ==========================
if [[ -d $ETCD_DATA ]]
then
  echo "數(shù)據(jù)目錄正常"
  rm -rf $ETCD_DATA/*
else
  mkdir -p $ETCD_DATA
  chmod 777 $ETCD_DATA
fi
# ==========================

docker run -d --restart=always --net=host \
-v $ETCD_DATA:/etcd-data \
--name $HOST_NAME \
quay.io/coreos/etcd:v3.5.7 \
/usr/local/bin/etcd \
-name $HOST_NAME \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://$HOST_IP:2380 \
--initial-cluster "$ETCD_ENDPOINT" \
--initial-cluster-token tkn \
--initial-cluster-state new \
--log-level info \
--logger zap \
--log-outputs stderr

postgres_cluster集群搭建(可以使用上面Dockerfile自行編譯的)

//192.168.26.101 主機
# docker run -d --restart=always --privileged --net=host --hostname=patroni-1 --name=patroni-1 \
-e ETCD_ENDPION=192.168.26.101:2379,192.168.26.102:2379,192.168.26.103:2379 \
-e PATRONI_PASSWD='123456' \
-e PATRONI_VIP=192.168.26.200 \
-e PATRONI_BRD=192.168.26.255 \
-v /opt/postgres/data/:/home/postgres/data/ \
swr.cn-north-4.myhuaweicloud.com/easyk8s.com/postgres_cluster:2023_02_07

//192.168.26.102 主機
# docker run -d --restart=always --privileged --net=host --hostname=patroni-2 --name=patroni-2 \
-e ETCD_ENDPION=192.168.26.101:2379,192.168.26.102:2379,192.168.26.103:2379 \
-e PATRONI_PASSWD='123456' \
-e PATRONI_VIP=192.168.26.200 \
-e PATRONI_BRD=192.168.26.255 \
-v /opt/postgres/data/:/home/postgres/data/ \
swr.cn-north-4.myhuaweicloud.com/easyk8s.com/postgres_cluster:2023_02_07

//192.168.26.103 主機
# docker run -d --restart=always --privileged --net=host --hostname=patroni-3 --name=patroni-3 \
-e ETCD_ENDPION=192.168.26.101:2379,192.168.26.102:2379,192.168.26.103:2379 \
-e PATRONI_PASSWD='123456' \
-e PATRONI_VIP=192.168.26.200 \
-e PATRONI_BRD=192.168.26.255 \
-v /opt/postgres/data/:/home/postgres/data/ \
swr.cn-north-4.myhuaweicloud.com/easyk8s.com/postgres_cluster:2023_02_07

# 驗證postgres_cluster集群
# docker exec -it patroni-[1/2/3] bash
$ patronictl -c /home/postgres/config/patroni.yml list
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡狠毯,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門杂腰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來垃你,“玉大人,你說我怎么就攤上這事喂很∠模” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵少辣,是天一觀的道長凌摄。 經(jīng)常有香客問我,道長漓帅,這世上最難降的妖魔是什么锨亏? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮忙干,結(jié)果婚禮上器予,老公的妹妹穿的比我還像新娘。我一直安慰自己捐迫,他們只是感情好乾翔,可當(dāng)我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般反浓。 火紅的嫁衣襯著肌膚如雪萌丈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天雷则,我揣著相機與錄音辆雾,去河邊找鬼。 笑死月劈,一個胖子當(dāng)著我的面吹牛度迂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播艺栈,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼英岭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了湿右?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤罚勾,失蹤者是張志新(化名)和其女友劉穎毅人,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體尖殃,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡丈莺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了送丰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缔俄。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖器躏,靈堂內(nèi)的尸體忽然破棺而出俐载,到底是詐尸還是另有隱情,我是刑警寧澤登失,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布遏佣,位于F島的核電站,受9級特大地震影響揽浙,放射性物質(zhì)發(fā)生泄漏状婶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一馅巷、第九天 我趴在偏房一處隱蔽的房頂上張望膛虫。 院中可真熱鬧,春花似錦钓猬、人聲如沸稍刀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掉丽。三九已至跌榔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間捶障,已是汗流浹背僧须。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留项炼,地道東北人担平。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像锭部,于是被迫代替她去往敵國和親暂论。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容