简述:主要分享docker安装步骤,及微服务组件的镜像安装,如:nacos、seata、elasticsearch、rabbitmq、mongodb、minio…等等
一、docker安装,如果不是root用户,记得命令前加上sudo
1、安装yum工具
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2 --skip-broken
2、更新本地镜像源
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
3、安装docker
yum install -y docker-ce
4、先关闭防火墙,再启动
# 关闭防火墙
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
# 查看是否关闭防火墙
systemctl status firewalld
5、启动docker,出现active(running)时,即为启动成功
# 启动docker服务
systemctl start docker
# 停止docker服务
systemctl stop docker
# 重启docker服务
systemctl restart docker
6、查看docker版本
docker -v
7、配置镜像加速
7.1、新建目录
mkdir -p /etc/docker
7.2、/etc/docker下新建文件
touch /etc/docker/daemon.json
7.3、登录阿里云https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors,找到镜像,复制镜像地址
7.4、编辑文件
vim daemon.json
7.5、复制下面内容进去,镜像地址替换成上面复制的
{
"registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
7.6、重新加载文件
systemctl daemon-reload
8、重启docker
systemctl restart docker
二、拉取镜像,并安装运行
1.docker常用命令
# 查找镜像
docker search nacos
# 拉取最新镜像
docker pull redis:latest
# 启动容器
docker run elasticsearch
# 停止容器
docker stop elasticsearch
# 查看端口使用
lsof -i:8080
# 查看进程
docker ps
# 删除容器
docker rm 25b7b405f71c
# 删除镜像
docker rmi 5acf0e8da90b
# 设置容器开机自启
docker update redis --restart=always
2、安装nacos
2.1、查找、拉取镜像
docker search nacos
docker pull nacos/nacos-server:latest
2.2、创建目录、文件
mkdir -p /mydata/nacos/logs/
mkdir -p /mydata/nacos/init.d/
touch /mydata/nacos/init.d/custom.properties
2.3、查看mysql在docker中对应的ip,写在下面配置文件中
# mysql是对应的容器名字
docker inspect mysql | grep IPAddress
2.4、编辑custom.properties,复制下面内容进去,修改数据库连接
erver.contextPath=/nacos
server.servlet.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
db.num=1
# 这里要对应ip,以及对应的数据库
db.url.0=jdbc:mysql://172.17.0.2:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
nacos.security.ignore.urls=/,//*.css,//.js,/**/.html,//*.map,//.svg,/**/.png,//*.ico,/console-fe/public/,/v1/auth/login,/v1/console/health/,/v1/cs/,/v1/ns/,/v1/cmdb/,/actuator/,/v1/console/server/
nacos.naming.distro.taskDispatchThreadCount=1
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
nacos.naming.expireInstance=true
2.5、启动nacos
docker run --name nacos -p 8848:8848 \
--privileged=true \
--restart=always \
-e JVM_XMS=256m \
-e JVM_XMX=256m \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-v /mydata/nacos/logs:/home/nacos/logs \
-v /mydata/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties \
-d nacos/nacos-server
2.6、浏览器访问,账号、密码:nacos
192.168.2.212:8848/nacos
3、安装nginx
3.1、拉取镜像
docker pull nginx:latest
3.2、启动nginx
docker run --name my-nginx -p 80:80 -d nginx
3.3、新建目录
mkdir -p /mydata/nginx/data
mkdir -p /mydata/nginx/config
mkdir -p /mydata/nginx/logs
3.4、停止nginx
docker stop nginx
3.5、删除容器
docker rm nginx
3.6、重新启动nginx
# -v:目录挂载,将data文件夹挂在到指定目录下,将redis.conf挂载到指定目录下
docker run --privileged=true -e TZ="Asia/Shanghai" -it -d --name nginx -p 80:80 \
-v /mydata/nginx/data:/usr/share/nginx/html \
-v /mydata/nginx/config/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/config/default.conf:/etc/nginx/conf.d/default.conf \
-v /mydata/nginx/logs:/var/log/nginx nginx
3.7、在/mydata/nginx/data下创建index.html
touch index.html
3.8、编辑index.html,将下面内容复制进去,测试效果
vim index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.content {
margin-top: 20%;
text-align: center;
}
.detail {
font-size: 40px;
font-weight: bold;
font-family: Cursive;
}
</style>
</head>
<body>
<div class="content">
<p class="detail">享清福不在为官,只要囊有钱,仓有米,腹有诗书,便是山中宰相</p>
<p class="detail">祈寿年无须服药,但愿身无病,心无忧,门无债主,可为地上神仙</p>
</div>
</body>
</html>
3.9、浏览器访问
192.168.2.212
4、安装redis
4.1、拉取最新镜像
docker pull redis:latest
4.2、创建redis目录
mkdir -p /mydata/redis/config
mkdir /mydata/redis/data
4.3、挂载redis
touch /mydata/redis/config/redis.conf
4.4、启动redis
# -v:目录挂载,将data文件夹挂在到指定目录下,将redis.conf挂载到指定目录下,实际目录要进入redis中查看(docker exec -it redis bash)
# -p:端口映射,将docker中的6379端口映射到服务器的6379端口上
docker run -p 6379:6379 -v /mydata/redis/data:/data --name redis redis --requirepass 123456 --appendonly yes
4.5、修改redis密码
#1.进入redis的容器
docker exec -it 容器ID bash
#2.进入redis目录
cd /usr/local/bin
#3.运行命令:
redis-cli
#4.查看现有的redis密码:
config get requirepass
#5.设置redis密码
config set requirepass 密码
5、安装mongodb
5.1、拉取镜像
docker pull mongo:latest
5.2、启动mongo,释放 docker的27017端口映射到服务器的27017,–name 要启动的容器名称
docker run -p 27017:27017 --name mongodb -d mongo
5.3、使用 volume 持久化数据
docker run -it -v mongo_volume:/mydata/mongodb/data -p 27017:27017 --name mongodb -d mongo 47bdb045bd0d
5.4、新增用户
进入mongo容器,依次执行命令
# 进入mongo容器
docker exec -it mongodb bash
# 登录mongodb
mongo admin
# 创建一个名为root,密码为123456 的用户
db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"root",db:"admin"}]
}
)
# 授权root
db.auth("root","123456")
# 进入数据库bbc_dev
use bbc_dev
# 添加用户
db.createUser(
{
user: "root",
pwd: "123456",
roles: [{role:"readWrite", db: "bbc_dev"}]
}
)
5.4、浏览器访问
http://192.168.2.212:27017/
6、安装mysql
6.0、常见命令
# 进入mysql,修改密码
update user set password=password('root') where user='root';
# 进入mysql,查询最大连接数
show variables like '%max_connections%';
# 容器内部拷贝
docker cp mysql:/etc/mysql/conf.d /mydata/mysql/config
6.1、拉取镜像
docker pull mysql:8.0.20
6.2、新建目录
mkdir -p /mydata/mysql/data
mkdir -p /mydata/mysql/config
mkdir -p /mydata/mysql/logs
6.3、新建文件my.cnf,将配置文件挂载到指定目录下(一致挂载失败,后面再更新)
6.4、启动mysql,有个坑:挂载的目录和文件需要提前创建好,不然挂载失败
docker run --restart=always -d --name mysql -v /mydata/mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
6.5、进入mysql容器,退出:exit
docker exec -it mysql bash
6.6、登录mysql
mysql -u root -p
6.7、navicat连接测试
7、安装minio
7.1、查找、拉取镜像
docker search minIO
docker pull minio/minio:latest
7.2、创建目录
mkdir -p /mydata/minio/data
7.3、启动minio
docker run -p 9000:9000 -p 9090:9090 \
--name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minioadmin" \
-e "MINIO_SECRET_KEY=minioadmin" \
-v /mydata/minio/data:/data \
minio/minio server \
/data --console-address ":9090" -address ":9000"
7.4、浏览器访问,账号、密码:7.3设置的minioadmin
192.168.2.212:9090
8、安装elasticsearch
8.1、拉取镜像
docker pull elasticsearch:7.8.0
8.2、创建es目录
mkdir /mydata/elasticsearch/config
mkdir /mydata/elasticsearch/data
8.3、授权es
chmod -R 777 /mydata/elasticsearch/
8.4、启动es,挂载elasticsearch.yml到指定目录下(一致挂载失败,后面再更新)
# 将容器中的目录:/data,挂载到本地:/mydata/elasticsearch/data
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms84m -Xmx512m" \
-v /mydata/elasticsearch/data:/data \
-v /mydata/elasticsearch/plugins:/plugins \
-d elasticsearch:7.8.0
9、安装seata
9.1、查找、拉取seata
docker search seata
docker pull seataio/seata-server:latest
9.2、启动seata
docker run -d --name seata-server -p 8091:8091 seataio/seata-server
9.3、新建目录
mkdir -p /mydata/seata/data
9.4、将seata文件移动到新建目录下
docker cp seata-server:/seata-server /mydata/seata/data
9.5、修改application.yml,在/mydata/seata/data/seata-server/resources下,替换成下面内容
vim /mydata/seata/data/seata-server/resource/application.yml
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
seata:
config:
# support: nacos 、 consul 、 apollo 、 zk 、 etcd3
type: file
nacos:
server-addr: 127.0.0.1:8848
namespace:
group: SEATA_GROUP
username:
password:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
data-id: seataServer.properties
consul:
server-addr: 127.0.0.1:8500
acl-token:
key: seata.properties
apollo:
appId: seata-server
apollo-meta: http://192.168.1.204:8801
apollo-config-service: http://192.168.1.204:8080
namespace: application
apollo-access-key-secret:
cluster: seata
zk:
server-addr: 127.0.0.1:2181
session-timeout: 6000
connect-timeout: 2000
username:
password:
node-path: /seata/seata.properties
etcd3:
server-addr: http://localhost:2379
key: seata.properties
registry:
# support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa
type: file
preferred-networks: 30.240.*
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace:
cluster: default
username:
password:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
eureka:
service-url: http://localhost:8761/eureka
application: default
weight: 1
redis:
server-addr: localhost:6379
db: 0
password:
cluster: default
timeout: 0
zk:
cluster: default
server-addr: 127.0.0.1:2181
session-timeout: 6000
connect-timeout: 2000
username: ""
password: ""
consul:
cluster: default
server-addr: 127.0.0.1:8500
acl-token:
etcd3:
cluster: default
server-addr: http://localhost:2379
sofa:
server-addr: 127.0.0.1:9603
application: default
region: DEFAULT_ZONE
datacenter: DefaultDataCenter
cluster: default
group: SEATA_GROUP
address-wait-time: 3000
server:
service-port: 8091 #If not configured, the default is '${server.port} + 1000'
max-commit-retry-timeout: -1
max-rollback-retry-timeout: -1
rollback-retry-timeout-unlock-enable: false
enable-check-auth: true
enable-parallel-request-handle: true
retry-dead-threshold: 130000
xaer-nota-retry-timeout: 60000
recovery:
committing-retry-period: 1000
async-committing-retry-period: 1000
rollbacking-retry-period: 1000
timeout-retry-period: 1000
undo:
log-save-days: 7
log-delete-period: 86400000
session:
branch-async-queue-size: 5000 #branch async remove queue size
enable-branch-async-remove: false #enable to asynchronous remove branchSession
store:
# support: file 、 db 、 redis
mode: file
session:
mode: file
lock:
mode: file
file:
dir: sessionStore
max-branch-session-size: 16384
max-global-session-size: 512
file-write-buffer-cache-size: 16384
session-reload-read-size: 100
flush-disk-mode: async
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true
user: mysql
password: mysql
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
redis:
mode: single
database: 0
min-conn: 1
max-conn: 10
password:
max-total: 100
query-limit: 100
single:
host: 127.0.0.1
port: 6379
sentinel:
master-name:
sentinel-hosts:
metrics:
enabled: false
registry-type: compact
exporter-list: prometheus
exporter-prometheus-port: 9898
transport:
rpc-tc-request-timeout: 30000
enable-tc-server-batch-send-response: false
shutdown:
wait: 3
thread-factory:
boss-thread-prefix: NettyBoss
worker-thread-prefix: NettyServerNIOWorker
boss-thread-size: 1
9.6、查看运行的seata的id,停止容器,删除容器
docker stop a6efab070fc2
docker rm a6efab070fc2
9.7、再次启动seata,SEATA_IP = “你自己的IP”
docker run -d --restart always --name seata-server -p 8091:8091 \
-v /mydata/seata/data/seata-server -e SEATA_IP=192.168.2.212 \
-e SEATA_PORT=8091 seataio/seata-server
10、安装rabbitMQ
10.1、拉取镜像
docker pull rabbitmq:latest
10.2、运行rabbit,将docker的15672、5672映射到服务器的15672、5673端口
docker run -d --hostname rabbit --name rabbit -p 15672:15672 -p 5673:5672 rabbitmq
10.3、进入rabbit容器,退出:exit
docker exec -it rabbitmq bash
10.4、执行加载配置
rabbitmq-plugins enable rabbitmq_management
10.5、浏览器访问,账号、密码:guest
http://192.168.2.212:15672/
11、未完待续…