主頁(yè) > 知識(shí)庫(kù) > Docker部署SQL Server 2019 Always On集群的實(shí)現(xiàn)

Docker部署SQL Server 2019 Always On集群的實(shí)現(xiàn)

熱門標(biāo)簽:河南防封號(hào)電銷機(jī)器人是什么 宿城區(qū)電話機(jī)器人找哪家 麗江真人語(yǔ)音電話外呼系統(tǒng) 怎么找到?jīng)]有地圖標(biāo)注的店 上海申請(qǐng)高400開頭的電話 10086外包用的什么外呼系統(tǒng) 福州企業(yè)電銷機(jī)器人排名 400電話辦理介紹信 打400電話怎么辦理收費(fèi)

Docker部署Always on集群

SQL Server在2016年開始支持Linux。隨著2017和2019版本的發(fā)布,它開始支持Linux和容器平臺(tái)上的HA/DR、Kubernetes和大數(shù)據(jù)集群解決方案。

在本文中,我們將在3個(gè)節(jié)點(diǎn)的Docker容器上安裝SQL Server 2019,并創(chuàng)建AlwaysOn可用性組。

我們的目標(biāo)是使用單個(gè)配置文件快速準(zhǔn)備好環(huán)境。因此,開發(fā)人員或測(cè)試團(tuán)隊(duì)可以快速執(zhí)行諸如兼容性、連通性、代碼功能等測(cè)試。

在本節(jié)中,我們將首先準(zhǔn)備一個(gè)基于Ubuntu的映像,以便能夠在容器上安裝可用性組。然后我們將執(zhí)行必要的安裝。

重要提示:不建議在生產(chǎn)環(huán)境中執(zhí)行操作。安裝是在Ubuntu 18.04上執(zhí)行的。

安裝Docker

安裝Docker就不介紹了,自行安裝即可.

架構(gòu)

主機(jī)名 IP 端口 角色
sqlNode1 宿主機(jī)IP 1501:1433
sqlNode2 宿主機(jī)IP 1502:1433 副本
sqlNode3 宿主機(jī)IP 1503:1433 副本

端口表示:外網(wǎng)端口:內(nèi)網(wǎng)端口

準(zhǔn)備相關(guān)容器鏡像

拉取操作系統(tǒng)和數(shù)據(jù)庫(kù)的Docker鏡像,如下

操作系統(tǒng)

docker pull ubuntu:18.04

SQL Server 2019

docker pull mcr.microsoft.com/mssql/server:2019-latest

可通過(guò)docker images來(lái)查看已下載的鏡像信息。

鏡像地址:https://hub.docker.com/_/microsoft-mssql-server

開始配置-容器

環(huán)境準(zhǔn)備完畢后,開始正式的配置安裝。

步驟1:創(chuàng)建Dockerfile

創(chuàng)建目錄用于存放dockerfile、docker-compose.yml等文件。

mkdir /sql2019ha
cd /sql2019ha
touch dockerfile
vi dockerfile

dockerfile內(nèi)容如下

FROM ubuntu:18.04
 
RUN apt-get update
 
RUN apt install sudo wget curl gnupg gnupg1 gnupg2 -y
RUN apt install software-properties-common systemd vim -y
RUN wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
 
RUN add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/18.04/mssql-server-2019.list)"
RUN apt-get update
RUN apt-get install -y mssql-server
 
RUN /opt/mssql/bin/mssql-conf set hadr.hadrenabled  1
RUN /opt/mssql/bin/mssql-conf set sqlagent.enabled true
 
EXPOSE 1433
 
ENTRYPOINT /opt/mssql/bin/sqlservr

說(shuō)明

  • FROM:表示基于什么鏡像進(jìn)行安裝的
  • RUN:在鏡像中進(jìn)行的操作
  • EXPOSE:指定服務(wù)端口
  • ENTRYPOINT:運(yùn)行命令

步驟2:編譯鏡像

通過(guò)dockerfile來(lái)編譯鏡像,用于后面的安裝,命令:docker build -t sqlag2019:ha .

其中sqlag2019為鏡像名稱,ha是鏡像標(biāo)簽,.表示在當(dāng)前目錄下編譯,因?yàn)閐ockerfile就在當(dāng)前目錄下。

以下輸出是精簡(jiǎn)的,實(shí)際上輸出非常多…也需要一定時(shí)間(安裝一些包、數(shù)據(jù)庫(kù)等),由網(wǎng)速?zèng)Q定

$ docker build -t sqlag2019:ha .
Sending build context to Docker daemon   2.56kB
Step 1/12 : FROM ubuntu:18.04
 ---> c3c304cb4f22
Step 2/12 : RUN apt-get update
 ---> Running in 950e50f80f00
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [932 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]...
Step 3/12 : RUN apt install sudo wget curl gnupg gnupg1 gnupg2 -y
 ---> Running in edc9d15b2383
..
..
Step 8/12 : RUN sudo apt-get install -y mssql-server
 ---> Running in 43d82a503f8a
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
Step 9/12 : RUN sudo /opt/mssql/bin/mssql-conf set hadr.hadrenabled  1
 ---> Running in 166c6596d2dd
SQL Server needs to be restarted in order to apply this setting. Please run
'systemctl restart mssql-server.service'.
Removing intermediate container 166c6596d2dd
 ---> bcdb057fed43
Step 10/12 : RUN sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
 ---> Running in 22dd6a93d1ef
SQL Server needs to be restarted in order to apply this setting. Please run
'systemctl restart mssql-server.service'.
Removing intermediate container 22dd6a93d1ef
 ---> 6b90afbaf94e
Step 11/12 : EXPOSE 1433
 ---> Running in bcc14f3b0bad
Removing intermediate container bcc14f3b0bad
 ---> 4aae1563aa74
Step 12/12 : ENTRYPOINT /opt/mssql/bin/sqlservr
 ---> Running in 68b6ed45ff6a
Removing intermediate container 68b6ed45ff6a
 ---> b7467618c371
Successfully built b7467618c371
Successfully tagged sqlag2019:ha

最后出現(xiàn)Successfully表示編譯成功,否則根據(jù)錯(cuò)誤信息進(jìn)行解決。

步驟3:創(chuàng)建容器

現(xiàn)在鏡像編譯好了,下面再通過(guò)docker-compose文件來(lái)創(chuàng)建、配置3個(gè)容器,具體內(nèi)容如下:

$ touch docker-compose.yml
$ vi docker-compose.yml
version: '3'
 
services:
  db1:
    container_name: sqlNode1
    image: sqlag2019:ha
    hostname: sqlNode1
    domainname: lab.local
    environment:
      SA_PASSWORD: "MyPassWord123"
      ACCEPT_EULA: "Y"
    ports:
    - "1501:1433"
    extra_hosts:
      sqlNode2.labl.local: "172.16.238.22"
      sqlNode3.labl.local: "172.16.238.23"
    networks:
        internal:
                ipv4_address: 172.16.238.21
 
  db2:
    container_name: sqlNode2
    image: sqlag2019:ha
    hostname: sqlNode2
    domainname: lab.local
    environment:
      SA_PASSWORD: "MyPassWord123"
      ACCEPT_EULA: "Y"
    ports:
    - "1502:1433"
    extra_hosts:
      sqlNode1.lab.local: "172.16.238.21"
      sqlNode3.lab.local: "172.16.238.23"
    networks:
        internal:
                ipv4_address: 172.16.238.22
 
  db3:
    container_name: sqlNode3
    image: sqlag2019:ha
    hostname: sqlNode3
    domainname: lab.local
    environment:
      SA_PASSWORD: "MyPassWord123"
      ACCEPT_EULA: "Y"
    ports:
    - "1503:1433"
    extra_hosts:
      sqlNode1.lab.local: "172.16.238.21"
      sqlNode2.lab.local: "172.16.238.22"
    networks:
        internal:
                ipv4_address: 172.16.238.23
 
networks:
    internal:
      ipam:
            driver: default
            config:
                - subnet: 172.16.238.0/24

步驟4:?jiǎn)?dòng)容器

然后通過(guò)docker-compose up -d命令啟動(dòng)三個(gè)容器,其中-d表示在后臺(tái)運(yùn)行。

$ docker-compose up -d
Creating network "sql2019hademo_internal" with the default driver
Creating sqlNode2 ...
Creating sqlNode1 ...
Creating sqlNode2
Creating sqlNode3 ...
Creating sqlNode1
Creating sqlNode2 ... done

注意:docker-compose是需要單獨(dú)安裝的,就是一個(gè)可執(zhí)行文件??赏ㄟ^(guò)apt、yum來(lái)安裝。

查看容器狀態(tài)

$ docker-compose ps
Name                Command               State           Ports
--------------------------------------------------------------------------
sqlNode1   /bin/sh -c /opt/mssql/bin/ ...   Up      0.0.0.0:1501->1433/tcp
sqlNode2   /bin/sh -c /opt/mssql/bin/ ...   Up      0.0.0.0:1502->1433/tcp
sqlNode3   /bin/sh -c /opt/mssql/bin/ ...   Up      0.0.0.0:1503->1433/tcp

至此容器已經(jīng)啟動(dòng)完成,下面通過(guò)SSMS連接數(shù)據(jù)庫(kù)進(jìn)行相關(guān)檢查和配置ALWAYSON。

步驟5:SSMS連接MSSQL

通過(guò)宿主機(jī)的外網(wǎng)IP+端口連接相應(yīng)的數(shù)據(jù)庫(kù),如下:

注意:IP和端口之間是逗號(hào)


可以看到數(shù)據(jù)庫(kù)的圖標(biāo)也是Linux的圖標(biāo)。

配置-數(shù)據(jù)庫(kù)

這部分就是在數(shù)據(jù)庫(kù)中進(jìn)行相關(guān)配置,如:創(chuàng)建KEY加密文件,管理用戶、可用組等。

步驟1:連接主庫(kù)-sqlNode1

主庫(kù)也就是節(jié)點(diǎn)1,端口是1501,連接方法如上圖。

我們將證書和私鑰提取到/tmp/dbm_certificate.cer/tmp/dbm_certificate.pvk文件中。

我們將這些文件復(fù)制到其他節(jié)點(diǎn),并根據(jù)以下文件創(chuàng)建主密鑰和證書:執(zhí)行以下腳本

USE master
GO
 
CREATE LOGIN dbm_login WITH PASSWORD = 'MyStr0ngPa$w0rd';
CREATE USER dbm_user FOR LOGIN dbm_login;
GO
 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MyStr0ngPa$w0rd';
go
CREATE CERTIFICATE dbm_certificate WITH SUBJECT = 'dbm';
BACKUP CERTIFICATE dbm_certificate
TO FILE = '/tmp/dbm_certificate.cer'
WITH PRIVATE KEY (
        FILE = '/tmp/dbm_certificate.pvk',
        ENCRYPTION BY PASSWORD = 'MyStr0ngPa$w0rd'
    );
GO

將文件拷貝到其他兩個(gè)節(jié)點(diǎn):

$ docker cp sqlNode1:/tmp/dbm_certificate.cer .
$ docker cp sqlNode1:/tmp/dbm_certificate.pvk .
$ docker cp dbm_certificate.cer sqlNode2:/tmp/
$ docker cp dbm_certificate.pvk sqlNode2:/tmp/
$ docker cp dbm_certificate.cer sqlNode3:/tmp/
$ docker cp dbm_certificate.pvk sqlNode3:/tmp/

步驟2:連接從庫(kù)-sqlNode2和sqlNode3

兩個(gè)從庫(kù)的端口分別是:1502和1503.然后重復(fù)主庫(kù)執(zhí)行的操作,如下:

CREATE LOGIN dbm_login WITH PASSWORD = 'MyStr0ngPa$w0rd';
CREATE USER dbm_user FOR LOGIN dbm_login;
GO
 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MyStr0ngPa$w0rd';
GO
CREATE CERTIFICATE dbm_certificate   
    AUTHORIZATION dbm_user
    FROM FILE = '/tmp/dbm_certificate.cer'
    WITH PRIVATE KEY (
    FILE = '/tmp/dbm_certificate.pvk',
    DECRYPTION BY PASSWORD = 'MyStr0ngPa$w0rd'
);
GO

步驟3:所有節(jié)點(diǎn)

在所有節(jié)點(diǎn)上執(zhí)行以下命令

CREATE ENDPOINT [Hadr_endpoint]
    AS TCP (LISTENER_IP = (0.0.0.0), LISTENER_PORT = 5022)
    FOR DATA_MIRRORING (
        ROLE = ALL,
        AUTHENTICATION = CERTIFICATE dbm_certificate,
        ENCRYPTION = REQUIRED ALGORITHM AES
        );
ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;
GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [dbm_login];

啟用開機(jī)自啟動(dòng)ALWAYON,在所有節(jié)點(diǎn)執(zhí)行以下命令

ALTER EVENT SESSION  AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON);
GO

步驟4:創(chuàng)建高可用組

可以用SSMS工具和T-SQL兩種方式,下面以T-SQL為例:

運(yùn)行以下腳本在主節(jié)點(diǎn)中創(chuàng)建一個(gè)可用性組。 請(qǐng)注意,選擇CLUSTER_TYPE = NONE選項(xiàng)是因?yàn)樗窃跊]有諸如Pacemaker或Windows Server故障轉(zhuǎn)移群集之類的群集管理平臺(tái)的情況下安裝的。

如果要在Linux上安裝AlwaysOn AG,則應(yīng)為Pacemaker選擇CLUSTER_TYPE = EXTERNAL:

CREATE AVAILABILITY GROUP [AG1]
        WITH (CLUSTER_TYPE = NONE)
        FOR REPLICA ON
        N'sqlNode1'
            WITH (
            ENDPOINT_URL = N'tcp://sqlNode1:5022',
            AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,
                SEEDING_MODE = AUTOMATIC,
                FAILOVER_MODE = MANUAL,
            SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
                ),
        N'sqlNode2'
            WITH (
            ENDPOINT_URL = N'tcp://sqlNode2:5022',
            AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,
                SEEDING_MODE = AUTOMATIC,
                FAILOVER_MODE = MANUAL,
            SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
                ),
        N'sqlNode3'
            WITH (
            ENDPOINT_URL = N'tcp://sqlNode3:5022',
            AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,
                SEEDING_MODE = AUTOMATIC,
                FAILOVER_MODE = MANUAL,
            SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
                );
GO

在從庫(kù)中執(zhí)行以下命令,將從庫(kù)加入到AG組中

ALTER AVAILABILITY GROUP [ag1] JOIN WITH (CLUSTER_TYPE = NONE);
ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;
GO

至此在Docker容器中安裝SQL Server Alwayson集群已經(jīng)完成了!

注意:當(dāng)指定CLUSTER_TYPE = NONE創(chuàng)建可用組時(shí),在執(zhí)行故障轉(zhuǎn)移時(shí)需執(zhí)行以下命令

ALTER AVAILABILITY GROUP [ag1] FORCE_FAILOVER_ALLOW_DATA_LOSS

測(cè)試

在主庫(kù)上創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),并加入到可用組AG中。

CREATE DATABASE agtestdb;
GO
ALTER DATABASE agtestdb SET RECOVERY FULL;
GO
BACKUP DATABASE agtestdb TO DISK = '/var/opt/mssql/data/agtestdb.bak';
GO
ALTER AVAILABILITY GROUP [ag1] ADD DATABASE [agtestdb];
GO

通過(guò)SSMS查看同步狀態(tài)是否正常.

參考連接

https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-create-availability-group?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-mssql-conf?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-environment-variables?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-availability-group-cluster-ubuntu?view=sql-server-linux-ver15
https://docs.docker.com/engine/install/ubuntu/
https://docs.docker.com/compose/

到此這篇關(guān)于Docker部署SQL Server 2019 Always On集群的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Docker部署SQLServer集群內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:遵義 隴南 運(yùn)城 連云港 雞西 朝陽(yáng) 荊門 面試通知

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Docker部署SQL Server 2019 Always On集群的實(shí)現(xiàn)》,本文關(guān)鍵詞  Docker,部署,SQL,Server,2019,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Docker部署SQL Server 2019 Always On集群的實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Docker部署SQL Server 2019 Always On集群的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章