rancher1.6高可用集羣搭建

rancher高可用集羣搭建

1、搭建環境

1.安裝系統

  • 下載centos最新版, http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso
  • 最小化安裝
  • 配置網絡
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=95a50ea2-3ad5-4601-9f80-93b7f1913eab
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.0.106
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
  • 安裝經常使用工具,wget、vim、curl
  • 修改yum源,http://mirrors.163.com/.help/centos.html
cd /etc/yum.repos.d/
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache

2.安裝docker

  • 安裝docker,選擇社區版https://docs.docker.com/install/linux/docker-ce/centos/
#安裝所需的包
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
#添加源  
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
# 安裝docker-ce
sudo yum install docker-ce
#啓動Docker
sudo systemctl start docker
#docker經過運行hello-world 映像驗證安裝是否正確
sudo docker run hello-world

若是被牆,不能鏈接download.docker.com,則能夠下載該 .rpm文件手動安裝html

  • 修改鏡像源
vim /etc/docker/daemon.json
#增長一行 {"registry-mirrors": ["https://fu1ctwn9.mirror.aliyuncs.com"]}
sudo systemctl daemon-reload
sudo systemctl restart docker

#或者
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://你的的地址.m.daocloud.io

2、搭建高可用rancher

準備至少三臺機器。node

node0 192.168.0.106
node1 192.168.0.107
node2 192.168.0.108

1.搭建Galera Cluster高一致性MySQL集羣架構

使用galera cluster保證高可用和高一致性,mariadb 10.1起默認安裝了galera,mysql

不要用mariadb 10.3,目前rancher sql語句有bug,已提給rancher官方。
docker run -d --name mariadb-cluster0 -p 3306:3306 -p 4567:4567 -p 4568:4568 -p 4444:4444 -v /home/docker/mariadb/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=20053140 mariadb:10.2

mariadb默認配置文件linux

[mysqld]
server_id=100
pid-file    = /var/run/mysqld/mysqld.pid
socket        = /var/run/mysqld/mysqld.sock
port        = 3306
basedir        = /usr
datadir        = /var/lib/mysql
log-error=/var/log/mysqld.log
user=mysql

default-time-zone=UTC
#默認存儲字符集編碼格式
character-set-server=utf8
#禁止域名解析的
skip_name_resolve
#確保二進制日誌格式設置爲使用行級複製,而不是語句級複製
binlog_format=ROW
#確保mysqld沒有綁定到127.0.0.1
bind-address=0.0.0.0 

#最大鏈接數
max_connections     = 500
connect_timeout     = 5
wait_timeout        = 600
max_allowed_packet  = 16M
thread_cache_size   = 128
sort_buffer_size    = 4M
bulk_insert_buffer_size =16M
tmp_table_size      = 32M
max_heap_table_size = 32M

#確保默認存儲引擎是InnoDB,Galera不適用於MyISAM或其餘非事務性存儲引擎
default_storage_engine=innodb
#確保用於生成自動增量值的InnoDB鎖定模式設置爲交替鎖定模式
innodb_autoinc_lock_mode=2
#確保將InnoDB日誌緩衝區每秒寫入一次文件,而不是每次提交,以提升性能。
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=2G

#log-bin=/app/galera/mysql-bin  #若是不接從庫,註釋掉  
#log_slave_updates=1         #若是不接從庫,註釋掉

[galera]
#節點應用完事務才返回查詢請求  
wsrep_causal_reads=ON
#同步複製緩衝池
wsrep_provider_options="gcache.size=300M;gcache.page_size=300M"  
#爲沒有顯式申明主鍵的表生成一個用於certificationtest的主鍵,默認爲ON  
wsrep_certify_nonPK=ON   
#開啓全同步複製模式
wsrep_on=ON
#galera library  
wsrep_provider=/usr/lib/galera/libgalera_smm.so 
wsrep_sst_auth=syncuser:syncuser
#wsrep_sst_method=xtrabackup-v2
wsrep_sst_method=rsync
#開啓並行複製線程,考慮使用兩倍的CPU內核數量,默認1
wsrep_slave_threads=1
wsrep_cluster_name=MariaDB-Galera-Cluster
#galera cluster URL 
#wsrep_cluster_address="gcomm://192.168.0.106:4567,192.168.0.107:4567,192.168.0.108:4567"
#節點名稱
wsrep_node_name=mariadb-0 
wsrep_node_address=192.168.0.106

進去容器添加用戶web

[root@localhost cluster0]# docker exec -it 929 bash
root@92902e6ff803:/# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.5-MariaDB-10.3.5+maria~jessie mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '20053140' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'syncuser'@'%'IDENTIFIED BY 'syncuser' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

其餘節點啓動容器後,重複添加用戶操做redis

docker run -d --name mariadb-cluster0 -p 3306:3306 -p 4567:4567 -p 4568:4568 -p 4444:4444 -v /home/docker/mariadb/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=20053140 mariadb:10.2

解開 wsrep_cluster_address 註釋,修改 wsrep_node_namewsrep_node_addresssql

刪除全部mariadb容器,從新run.docker

--wsrep-new-cluster 只有在第一次集羣,第一個節點,才須要加上,下次啓動不須要此參數數據庫

cluster0json

docker run -d --name mariadb-cluster0 -p 3306:3306 -p 4567:4567 -p 4568:4568 -p 4444:4444 -v /home/docker/mariadb/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=20053140 mariadb:10.2 --wsrep-new-cluster

cluster1

docker run -d --name mariadb-cluster1 -p 3306:3306 -p 4567:4567 -p 4568:4568 -p 4444:4444 -v /home/docker/mariadb/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=20053140 mariadb:10.2

cluster2

docker run -d --name mariadb-cluster2 -p 3306:3306 -p 4567:4567 -p 4568:4568 -p 4444:4444 -v /home/docker/mariadb/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=20053140 mariadb:10.2

進入任意一臺容器的mysql

MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.000 sec)

配合外部負載均衡,保證外部使用高可用

添加haproxy用戶用於haproxy監控檢查

MariaDB [(none)]> SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'haproxy'@'192.168.0.104'IDENTIFIED BY '' WITH GRANT OPTION;
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.013 sec)

haproxy配置文件

global  
    chroot /usr/local  
    daemon  
    nbproc 1   
    pidfile /opt/haproxy/logs/haproxy.pid  
    ulimit-n 65536  
    #spread-checks 5m   
    #stats timeout 5m  
    #stats maxconn 100  
  
########默認配置############  
defaults  
    mode tcp              
    retries 3              #兩次鏈接失敗就認爲是服務器不可用,也能夠經過後面設置  
    option redispatch      #當serverId對應的服務器掛掉後,強制定向到其餘健康的服務器  
    option abortonclose    #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的連接  
    maxconn 32000          #默認的最大鏈接數  
    timeout connect 5000ms #鏈接超時  
    timeout client 30000ms #客戶端超時  
    timeout server 30000ms #服務器超時  
    timeout check 2000    #心跳檢測超時  
    log 127.0.0.1 local0 err #[err warning info debug]  

listen stats     #監控
   mode http
   bind 0.0.0.0:8888
   stats enable
   stats uri /stats
   stats realm Global\ statistics
   stats auth admin:admin
   
########MariaDB配置#################  
listen mariadb  
    bind 0.0.0.0:23306  
    mode tcp 
    option mysql-check user haproxy #mysql健康檢查  haproxy爲mysql登陸用戶名
    balance roundrobin  
    server s1 192.168.0.106:3306 weight 1 maxconn 10000 check inter 10s  
    server s2 192.168.0.107:3306 weight 1 maxconn 10000 check inter 10s  
    server s3 192.168.0.108:3306 weight 1 maxconn 10000 check inter 10s

啓動haproxy容器

docker run -d --name haproxy -p 23306:23306 -p 8888:8888 -v /data/syncthing/dongxu/haproxy:/usr/local/etc/haproxy:ro haproxy:1.7

添加測試數據,測試數據同步是否正常,惡意kill掉容器,或關掉一臺機器,測試數據是否正常,恢復機器恢復容器查看數據是否恢復正常,負載均衡是否正常。

image

停掉一臺node3,查看負載均衡健康檢查是否正常

image

2.搭建rancher集羣

建立rancher需使用的數據庫

CREATE DATABASE IF NOT EXISTS cattle COLLATE = 'utf8_general_ci' CHARACTER SET = 'utf8';
GRANT ALL ON cattle.* TO 'cattle'@'%' IDENTIFIED BY 'cattle';
GRANT ALL ON cattle.* TO 'cattle'@'localhost' IDENTIFIED BY 'cattle';

啓動rancher

node1

docker run -d --restart=unless-stopped -p 8080:8080 -p 9345:9345 rancher/server \
     --db-host 192.168.0.104 --db-port 23306 --db-user root --db-pass 20053140 --db-name cattle \
     --advertise-address 192.168.0.106

訪問192.168.0.106:8080,增長訪問控制。

node2

docker run -d --restart=unless-stopped -p 8080:8080 -p 9345:9345 rancher/server \
     --db-host 192.168.0.104 --db-port 23306 --db-user root --db-pass 20053140 --db-name cattle \
     --advertise-address 192.168.0.107

node3

docker run -d --restart=unless-stopped -p 8080:8080 -p 9345:9345 rancher/server \
     --db-host 192.168.0.104 --db-port 23306 --db-user root --db-pass 20053140 --db-name cattle \
     --advertise-address 192.168.0.108

給rancher配置負載均衡,haproxy.cfg增長配置

frontend http-in
    mode http
    bind *:28080
    #bind *:8080 ssl crt /etc/haproxy/certificate.pem
    default_backend rancher_servers

    # Add headers for SSL offloading
    #http-request set-header X-Forwarded-Proto https if { ssl_fc }
    #http-request set-header X-Forwarded-Ssl on if { ssl_fc }

    acl is_websocket hdr(Upgrade) -i WebSocket
    acl is_websocket hdr_beg(Host) -i ws
    use_backend rancher_servers if is_websocket

backend rancher_servers
    mode http
    option httpchk HEAD /login HTTP/1.0
    server websrv106 192.168.0.106:8080 weight 1 maxconn 1024 check
    server websrv107 192.168.0.107:8080 weight 1 maxconn 1024 check
    server websrv108 192.168.0.108:8080 weight 1 maxconn 1024 check

重啓haproxy

docker run -d --restart=unless-stopped --name haproxy -p 23306:23306 -p 8888:8888 -p 28080:28080 -v /data/syncthing/dongxu/haproxy:/usr/local/etc/haproxy:ro haproxy:1.7

訪問192.168.0.104:28080添加主機,添加容器,測試rancher是否正常,惡意kill掉rancher server容器或者關閉機器,查看整個集羣是否正常。

相關文章
相關標籤/搜索