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规划及组件
- 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集群快速部署
//所有主机需要创建数据目录,还有权限需要注意
//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
# 此部分内容仅仅用于测试环境使用
# ==========================
if [[ -d $ETCD_DATA ]]
then
echo "数据目录正常"
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
网友评论