1.docker 容器化部署nginx
- 创建 在docker host暴露80端口
```shell
创建docker nginx
docker run -d -p 80:80 —name nginx-server-port -v /opt/nginx-server-port:/usr/share/nginx/html:ro nginx
2. 挂载配置文件 创建nginx
```shell
## 创建挂载文件的目录
mkdir -p /opt/docker/nginx/conf
## 拷贝docker 容器中的nginx 配置文件
docker cp nginx-server-port:/etc/nginx/nginx.conf /opt/docker/nginx/conf
## 创建启动docker容器
docker run -d -p 82:80 --name nginx-server-conf -v /opt/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /opt/docker/nginx/html:/usr/share/nginx/html:ro nginx
## 创建index.html
echo "nginx is running" > /opt/docker/nginx/html/index.html
浏览器访问http://10.0.0.10 :80 显示 nginx is runing 表示容器启动成功
2. docker 容器化部署tomcat
创建docker 容器
## 创建tomcat docker应用
docker run -d -p 8081:8080 -v /opt/docker/tomcat/:/usr/local/tomcat/webapps/ROOT tomcat:9.0
## 创建tomcat文件
echo "tomcat running" > /opt/docker/tomcat/index.html
浏览器访问http://10.0.0.10 :8081 显示 tomcat is runing 表示容器启动成功
3. docker容器化部署mysql
3.1docker容器化部署单节点mysql
## 创建docker 容器
docker run -p 3306:3306 --name mysql -v /opt/docker/mysql/log:/var/log/mysql -v /opt/docker/mysql/data:/var/lib/mysql -v /opt/docker/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=ro
ot -d mysql:5.7
## 访问mysql数据库
docker exec -it mysql mysql -uroot -proot
3.2.docker 容器化部署mysql 主从复制
MySQL主节点部署
docker run -p 3307:3306 --name mysql-master -v /opt/docker/mysql-master/log:/var/log/mysql -v /opt/docker/mysql-master/data:/var/lib/mysql -v /opt/docker/mysql-master/conf:/etc/mys
ql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
MySQL主节点配置
```shell vim /opt/docker/mysql-master/conf/my.cnf
加入如下配置
[client] default-character-set=utf8
[mysql] default-character-set=utf8
[mysqld] init_connect=’SET collation_connection = utf8_unicode_ci’ init_connect=’SET NAMES utf8’ character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve
server_id=1 log-bin=mysql-bin read-only=0 binlog-do-db=kubemsb_test
replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema
重启 docker mysql-master
docker restart mysql-master
<a name="cpQfK"></a>
#### mysql从节点部署
```shell
docker run -p 3308:3306 --name mysql-slave -v /opt/docker/mysql-slave/log:/var/log/mysql -v /opt/docker/mysql-slave/data:/var/lib/mysql -v /opt/docker/mysql-slave/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d --link mysql-master:mysql-master mysql:5.7
mysql从节点配置
vim /opt/docker/mysql-slave/conf/my.cnf
## 配置如下内容
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=kubemsb_test
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
## 重启 docker mysql-master
docker restart mysql-slave
master节点配置
## 进入mysql master 容器 并进入mysql数据库
docker exec -it mysql-master mysql -uroot -proot -P 3307
## 执行授权配置
MySQL [(none)]> grant replication slave on *.* to 'backup'@'%' identified by '123456';
## 退出容器 重启容器
docker restart mysql-master
#查看配置是否生效
show master status\G
slave节点配置
## 进入mysql master 容器 并进入mysql数据库
docker exec -it mysql-slave mysql -uroot -proot -P 3308
## 执行授权配置
MySQL [(none)]> change master to master_host='mysql-master', master_user='backup', master_password='123456', master_log_file='mysql-bin.000002', master_log_pos=154, master_port=3306;
## 退出容器 重启容器
docker restart mysql-master
#查看配置是否生效
show master status\G
验证MySQL集群可用性
#在MySQL Master节点添加kubemsb_test数据库
docker exec -it mysql-master mysql -uroot -proot -P 3307
MySQL [(none)]> create database kubemsb_test;
MySQL [(none)]> show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kubemsb_test |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
#在MySQL Slave节点查看同步情况
docker exec -it mysql-slave mysql -uroot -proot -P 3308
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kubemsb_test |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
## master 和slave 同时出现kubemsb_test 数据库 说明 主从数据库配置成功
4. docker 容器化部署oracle
## 下载docker 容器
docker pull oracleinanutshell/oracle-xe-11g
## 启动docker 氢气
docker run -h oracle --name oracle -d -p 49160:22 -p 49161:1521 -p 49162:8080 oracleinanutshell/oracle-xe-11g
## 说明
49160 为ssh端口
49161 为sqlplus端口
49162 为oem端口
oracle数据库连接信息
port:49161
sid:xe
username:system
password:oracle
SYS用户密码为:oracle
下载 sqldeveloper 测试连接oracle
5. docker 容器化部署elasticeSearch和kibana
5.1 docker 部署 es
## 下载es容器
docker pull elasticsearch:7.17.0
##
mkdir -p /opt/docker/es/config
mkdir -p /opt/docker/es/data
echo "http.host: 0.0.0.0" >> /opt/docker/es/config/elasticsearch.yml
chmod -R 777 /opt/docker/es/
## 启动docker容器
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /opt/docker/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /opt/docker/es/data:/usr/share/elasticsearch/data \
-v /opt/docker/es/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.17.0
5.2 docker 部署kibana
## 下载 kibana
docker pull kibana:7.17.0
#启动docker 容器
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://10.0.0.10:9200 -p 5601:5601 \
-d kibana:7.17.0
浏览器访问 http://10.0.0.10:5601
6. docker 容器化部署Redis
6.1 docker容器化部署单节点Redis
mkdir -p /opt/docker/redis/conf
touch /opt/docker/redis/conf/redis.conf
docker run -p 6379:6379 --name redis -v /opt/docker/redis/data:/data \
-v /opt/docker/redis/conf:/etc/redis \
-d redis redis-server /etc/redis/redis.conf
验证redis 安装是否成功
[root@localhost ~]# docker exec -it redis redis-cli
127.0.0.1:6379> set key1 abc
OK
127.0.0.1:6379> get key1 abc
"abc"
6.2 docker 容器化部署Redis集群
安装redis-cluster;3主3从方式,从为了同步备份,主进行slot数据分片
vim redis-cluster.sh
## 脚本内容如下
for port in $(seq 8001 8006); \
do \
mkdir -p /opt/docker/redis-cluster/redis/node-${port}/conf
touch /opt/docker/redis-cluster/redis/node-${port}/conf/redis.conf
cat << EOF >/opt/docker/redis-cluster/redis/node-${port}/conf/redis.conf
port ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 10.0.0.10
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
EOF
docker run -p ${port}:${port} -p 1${port}:1${port} --name redis-${port} \
-v /opt/docker/redis-cluster/redis/node-${port}/data:/data \
-v /opt/docker/redis-cluster/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d redis:5.0.7 redis-server /etc/redis/redis.conf; \
done
## 执行脚本
sh redis-cluster.sh
登录容器进行配置
##登录redis容器
docker exec -it redis-8001 bash
## 创建redis-cluster
root@d47c3843ed2c:/data# redis-cli --cluster create 10.0.0.10:8001 10.0.0.10:8002 10.0.0.10:8003 10.0.0.10:8004 10.0.0.10:8005 10.0.0.10:8006 --cluster-replicas 1
## 输出如下
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.10:8005 to 10.0.0.10:8001
Adding replica 10.0.0.10:8006 to 10.0.0.10:8002
Adding replica 10.0.0.10:8004 to 10.0.0.10:8003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 996456e419e8510e308395b42764bc8b6f9cc2fa 10.0.0.10:8001
slots:[0-5460] (5461 slots) master
M: 5ad906a397cbf4d6a7ed75701579080101da46f0 10.0.0.10:8002
slots:[5461-10922] (5462 slots) master
M: 85ae27fe8fea16019b47b62cdcf2688f8e4ce6cc 10.0.0.10:8003
slots:[10923-16383] (5461 slots) master
S: ffaa41fc81a8afca768c5e8982bf3d26106edf78 10.0.0.10:8004
replicates 85ae27fe8fea16019b47b62cdcf2688f8e4ce6cc
S: dcb74d0322a8cc1cac645259f9e3c425db8ad4a6 10.0.0.10:8005
replicates 996456e419e8510e308395b42764bc8b6f9cc2fa
S: 2d52bc15414aab82b8da79b1fdf4d1e51bf0ce9e 10.0.0.10:8006
replicates 5ad906a397cbf4d6a7ed75701579080101da46f0
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 10.0.0.10:8001)
M: 996456e419e8510e308395b42764bc8b6f9cc2fa 10.0.0.10:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: ffaa41fc81a8afca768c5e8982bf3d26106edf78 10.0.0.10:8004
slots: (0 slots) slave
replicates 85ae27fe8fea16019b47b62cdcf2688f8e4ce6cc
S: 2d52bc15414aab82b8da79b1fdf4d1e51bf0ce9e 10.0.0.10:8006
slots: (0 slots) slave
replicates 5ad906a397cbf4d6a7ed75701579080101da46f0
M: 5ad906a397cbf4d6a7ed75701579080101da46f0 10.0.0.10:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 85ae27fe8fea16019b47b62cdcf2688f8e4ce6cc 10.0.0.10:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: dcb74d0322a8cc1cac645259f9e3c425db8ad4a6 10.0.0.10:8005
slots: (0 slots) slave
replicates 996456e419e8510e308395b42764bc8b6f9cc2fa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
验证集群
## 进入容器执行命令
docker exec -it redis-8001 redis-cli -c -p 8001
127.0.0.1:8001> set key1 abc
-> Redirected to slot [9189] located at 10.0.0.10:8002
OK
10.0.0.10:8002>
7. docker容器化部署RabbitMQ
docker run -d --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672 -v /opt/docker/rabbitmq:/var/lib/rabbitmq rabbitmq:management
##
端口说明:
4369, 25672 (Erlang发现&集群端口)
5672, 5671 (AMQP端口)
15672 (web管理后台端口)
61613, 61614 (STOMP协议端口)
1883, 8883 (MQTT协议端口)
浏览器访问http://10.0.0.10:15672 ; 用户名/密码 guest/guest