Initial commit

This commit is contained in:
tangweijie 2025-04-10 11:26:55 +08:00
commit 84c07990b4
10 changed files with 347 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"DockerRun.DisableDockerrc": true
}

63
README.md Normal file
View File

@ -0,0 +1,63 @@
# OpenGauss Docker 单节点部署
这个Ansible项目用于快速部署OpenGauss Docker单节点实例基于官方OpenGauss容器镜像。
## 前提条件
- 目标服务器能够连接互联网
- 目标服务器上已安装Python 3
- 部署服务器上已安装Ansible
## 快速开始
### 1. 配置部署
1. 编辑 `inventory.ini` 文件,配置目标服务器信息:
```ini
[opengauss_servers]
opengauss ansible_host=192.168.1.100 ansible_user=root ansible_port=22
```
2. 根据需要修改 `vars/main.yml` 中的变量:
```yaml
# OpenGauss Docker配置
opengauss_version: "latest" # 使用最新版本
opengauss_container_name: "opengauss" # 容器名称
opengauss_port: 5432 # 容器内端口
opengauss_host_port: 8888 # 宿主机映射端口
opengauss_password: "Gauss@123" # 数据库密码,请修改为符合复杂度要求的密码
```
### 2. 执行部署
```bash
./deploy.sh
```
部署成功后,将显示连接信息。
### 3. 连接数据库
使用以下命令连接数据库:
```bash
gsql -d postgres -U gaussdb -W'您的密码' -h 服务器IP -p 8888
```
## 密码复杂度要求
OpenGauss密码必须符合以下要求
- 长度8个字符以上
- 必须同时包含大写字母、小写字母、数字、以及特殊符号
- 特殊符号仅包含"#?!@$%^&*-"
- "!$&"需要用转义符"\"进行转义
## 数据持久化
数据被持久化保存在目标服务器的 `/opengauss` 目录,可以通过修改 `vars/main.yml` 文件中的 `opengauss_data_dir` 变量来更改。
## 官方文档
更多信息请参考[OpenGauss容器镜像安装官方文档](https://docs.opengauss.org/zh/docs/latest/docs/InstallationGuide/%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E5%AE%89%E8%A3%85.html)

10
ansible.cfg Normal file
View File

@ -0,0 +1,10 @@
[defaults]
inventory = inventory.ini
host_key_checking = False
deprecation_warnings = False
timeout = 30
pipelining = True
[ssh_connection]
retries = 3
ssh_args = -o ControlMaster=auto -o ControlPersist=60s

44
deploy.sh Executable file
View File

@ -0,0 +1,44 @@
#!/bin/bash
# 颜色设置
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # 无颜色
# 显示欢迎信息
echo -e "${BLUE}====================================================${NC}"
echo -e "${BLUE} OpenGauss Docker 单节点部署工具 ${NC}"
echo -e "${BLUE}====================================================${NC}"
# 检查ansible是否安装
if ! command -v ansible &> /dev/null; then
echo -e "${RED}错误: 未检测到ansible命令, 请先安装ansible${NC}"
echo "安装命令: pip install ansible"
exit 1
fi
# 检查inventory.ini文件是否已经配置
if grep -q "<your_server_ip>" inventory.ini; then
echo -e "${RED}错误: 请先修改inventory.ini文件配置服务器IP、用户名和SSH端口${NC}"
exit 1
fi
# 运行ansible playbook
echo -e "${GREEN}开始部署 OpenGauss Docker 单节点...${NC}"
ansible-playbook -i inventory.ini deploy.yml
# 检查部署结果
if [ $? -eq 0 ]; then
echo -e "${GREEN}OpenGauss Docker 单节点部署成功!${NC}"
echo -e "${GREEN}以下是连接信息:${NC}"
echo -e "${GREEN} 主机: 目标服务器IP${NC}"
echo -e "${GREEN} 端口: 8888${NC}"
echo -e "${GREEN} 用户: gaussdb${NC}"
echo -e "${GREEN} 密码: 在vars/main.yml中配置的密码${NC}"
echo -e "${GREEN} 连接命令: gsql -d postgres -U gaussdb -W'您的密码' -h 服务器IP -p 8888${NC}"
else
echo -e "${RED}OpenGauss Docker 单节点部署失败!${NC}"
echo -e "${RED}请检查错误输出并尝试修复问题.${NC}"
exit 1
fi

8
deploy.yml Normal file
View File

@ -0,0 +1,8 @@
---
- name: 部署OpenGauss Docker单节点
hosts: opengauss_servers
become: true
vars_files:
- vars/main.yml
roles:
- opengauss-docker

View File

@ -0,0 +1,23 @@
---
# Docker 相关配置
docker_repository: https://download.docker.com/linux/centos/docker-ce.repo
docker_packages:
- docker-ce
- docker-ce-cli
- containerd.io
# OpenGauss 相关配置
opengauss_version: "latest"
opengauss_container_name: "opengauss-server"
opengauss_data_dir: "/data/opengauss"
opengauss_container_data_dir: "/var/lib/opengauss/data"
opengauss_host_port: "8888"
opengauss_port: "5432"
opengauss_password: "Gauss@123"
opengauss_nodename: "gaussdb"
opengauss_username: "gaussdb"
opengauss_dbname: "postgres"
# 确保所有端口类型变量都是字符串
opengauss_host_port: "{{ opengauss_host_port | string }}"
opengauss_port: "{{ opengauss_port | string }}"

5
inventory.ini Normal file
View File

@ -0,0 +1,5 @@
[opengauss_servers]
opengauss ansible_host=ihwpc2.1msoft.cn ansible_user=root ansible_port=31822
[all:vars]
ansible_python_interpreter=/usr/bin/python3

View File

@ -0,0 +1,23 @@
---
# 检查Python
- name: 检查Python版本
command: python3 --version
register: python_version
ignore_errors: yes
changed_when: false
- name: 显示Python版本
debug:
msg: "Python版本: {{ python_version.stdout }}"
when: python_version.rc == 0
# 检查Docker是否已安装
- name: 检查Docker是否已安装
command: docker --version
register: docker_check
ignore_errors: yes
changed_when: false
- name: 显示Docker安装状态
debug:
msg: "Docker已安装: {{ docker_check.stdout if docker_check.rc == 0 else '未安装' }}"

View File

@ -0,0 +1,147 @@
---
# 检查前置条件
- name: 检查前置条件
include_tasks: check_prerequisites.yml
# 安装依赖包
- name: 安装必要的系统工具 (RedHat系列)
package:
name:
- yum-utils
- device-mapper-persistent-data
- lvm2
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: 安装必要的系统工具 (Debian系列)
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
state: present
when: ansible_facts['os_family'] == "Debian"
- name: 安装必要的系统工具 (Darwin)
homebrew:
name:
- docker
state: present
when: ansible_facts['os_family'] == "Darwin"
# 添加Docker仓库
- name: 添加Docker仓库
get_url:
url: "{{ docker_repository }}"
dest: /etc/yum.repos.d/docker-ce.repo
mode: '0644'
when: ansible_facts['os_family'] == "RedHat"
# 安装Docker
- name: 安装Docker
package:
name: "{{ docker_packages }}"
state: present
when: docker_check.rc != 0
# 启动Docker服务
- name: 启动并启用Docker服务
systemd:
name: docker
state: started
enabled: yes
# 创建OpenGauss数据目录
- name: 创建OpenGauss数据目录
file:
path: "{{ opengauss_data_dir }}"
state: directory
mode: '0777'
# 拉取官方OpenGauss镜像
- name: 拉取OpenGauss Docker镜像
docker_image:
name: "opengauss/opengauss-server:{{ opengauss_version }}"
source: pull
register: pull_image_result
# 停止并移除已存在的容器(如果存在)
- name: 检查容器是否存在
command: docker ps -a -f name={{ opengauss_container_name }}
register: container_exists
changed_when: false
ignore_errors: true
- name: 移除已存在的容器
command: docker rm -f {{ opengauss_container_name }}
when: opengauss_container_name in container_exists.stdout
ignore_errors: true
# 运行OpenGauss容器
- name: 运行OpenGauss Docker容器
docker_container:
name: "{{ opengauss_container_name }}"
image: "opengauss/opengauss-server:{{ opengauss_version }}"
state: started
privileged: yes
restart_policy: always
ports:
- "{{ opengauss_host_port }}:{{ opengauss_port }}"
env:
GS_PASSWORD: "{{ opengauss_password | string }}"
GS_NODENAME: "{{ opengauss_nodename | string }}"
GS_USERNAME: "{{ opengauss_username | string }}"
GS_USER_PASSWORD: "{{ opengauss_password | string }}"
GS_PORT: "{{ opengauss_port | string }}"
GS_DB: "{{ opengauss_dbname | string }}"
volumes:
- "{{ opengauss_data_dir }}:{{ opengauss_container_data_dir }}"
healthcheck:
test: ["CMD-SHELL", "gs_ctl query -D {{ opengauss_container_data_dir }}"]
interval: 10s
timeout: 5s
retries: 3
# 等待OpenGauss服务启动
- name: 等待OpenGauss服务启动
pause:
seconds: 60 # 增加等待时间确保服务完全启动
# 检查OpenGauss连接
- name: 检查OpenGauss容器状态
command: docker ps -f name={{ opengauss_container_name }} --format "{{ '{{' }}.Status{{ '}}' }}"
register: container_status
changed_when: false
retries: 5
delay: 10
until: container_status.stdout.find("healthy") != -1 or container_status.stdout.find("Up") != -1
- name: 显示OpenGauss容器状态
debug:
msg: "OpenGauss容器状态: {{ container_status.stdout }}"
# 验证数据库连接
- name: 等待数据库端口可用
wait_for:
host: localhost
port: "{{ opengauss_port }}"
timeout: 300
state: started
- name: 测试数据库连接
command: >
docker exec {{ opengauss_container_name }}
gsql -d {{ opengauss_dbname }}
-U {{ opengauss_username }}
-W {{ opengauss_password }}
-p {{ opengauss_port }}
-c "SELECT version();"
register: db_test
changed_when: false
ignore_errors: true
- name: 显示数据库连接测试结果
debug:
msg: "数据库连接测试结果: {{ db_test.stdout if db_test.rc == 0 else db_test.stderr }}"

21
vars/main.yml Normal file
View File

@ -0,0 +1,21 @@
---
# OpenGauss Docker配置
opengauss_version: "latest"
opengauss_container_name: "opengauss"
opengauss_port: 5432
opengauss_host_port: 8888
opengauss_password: "Gauss@123" # 请修改为符合复杂度要求的密码
opengauss_nodename: "gaussdb"
opengauss_username: "gaussdb"
opengauss_dbname: "postgres"
# 数据持久化
opengauss_data_dir: "/opengauss"
opengauss_container_data_dir: "/var/lib/opengauss"
# Docker配置
docker_repository: "https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo"
docker_packages:
- docker-ce
- docker-ce-cli
- containerd.io