一. 安装Docker
yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
docker -v
二. Docker 应用部署
1. 部署MySQL
1.1 创建容器,设置端口映射、目录映射
mkdir ~/mysql
cd ~/mysql
docker run -id \
-p 3306:3306 \
--name=c_mysql \
-v $PWD/conf:/etc/mysql \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password\
mysql:5.7
1.4 进入容器,操作mysql
docker exec –it c_mysql /bin/bash
2. 部署Tomcat
2.1 创建容器,设置端口映射、目录映射
mkdir ~/tomcat
cd ~/tomcat
docker run -id --name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat
3. 部署Nginx
3.1 创建容器,设置端口映射、目录映射
- 配置
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
vim nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
- 启动
docker run -id --name=c_nginx \
-p 80:80 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
4. 部署Redis
4.1 创建容器,设置端口映射
docker run -id --name=c_redis -p 6379:6379 redis:5.0 --requirepass "password"
5. 部署ES
5.1 创建容器,设置端口映射
- 配置
cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
- 运行
docker run --restart=always -p 9200:9200 -p 9300:9300 -v $PWD/plugins:/usr/share/elasticsearch/plugins -v $PWD/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" --name='c_elasticsearch' --cpuset-cpus="1" -m 2G -d elasticsearch:7.7.0
-e 分配内存 默认是1g内存,虚拟机1G的小的建议给150M就好。
//进入容器安装ik分词器
docker exec -it es容器id /bin/bash
cd /usr/share/elasticsearch/plugins/
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.0/elasticsearch-analysis-ik-7.7.0.zip
//ik 需要和es版本一致。
//解决es跨域请求。
cd /usr/share/elasticsearch/config/
vi elasticsearch.yml
添加以下内容
http.cors.enabled: true
http.cors.allow-origin: "*"
//退出容器
exit
//重启生效
docker restart
- 设置密码
xpack.security.enabled: true
cd bin
elasticsearch-setup-passwords interactive
[apm_system]
[kibana]
[logstash_system]
[beats_system]
[remote_monitoring_user]
[elastic]
6. 部署kibana
6.1 创建容器,设置映射
- 配置
cd ~/kibana
mkdir conf
vim config/kibana.yml
server.name: kibana
server.host: "0.0.0.0"
elasticsearch.hosts: [ "http://xxxxx:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"
- 启动
cd ~/kibana
docker run -d --restart=always --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 --name c_kibana -p 5601:5601 -v $PWD/config/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:7.7.0
- 启动后进行配置的话
docker exec -it kibana bash
vi config/kibana.yml
docker restart kibana
- es设置密码之后需要修改配置
elasticsearch.username: "elastic"
elasticsearch.password: "xxxxxx"
xpack.monitoring.ui.container.elasticsearch.enabled: true
7. 部署Logstash
7.1 创建容器,设置映射
- 配置文件目录
mkdir -p /usr/local/logstash/config
cd /usr/local/logstash/config
touch logstash.yml
vi log4j2.properties
logger.elasticsearchoutput.name = logstash.outputs.elasticsearch
logger.elasticsearchoutput.level = debug
vi pipelines.yml
- pipeline.id: logstash-match
path.config: "/usr/share/logstash/config/*.conf"
pipeline.workers: 3
- 启动
docker run -d -p 5044:5044 -p 9600:9600 -it \
--name logstash \
-v /usr/local/logstash/config/:/usr/share/logstash/config/ \
logstash:7.5.0
8. 部署rabbitmq
docker run -d --name c_rabbitmq -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=password-p 15672:15672 -p 5672:5672 rabbitmq:3-management
三. Docker Compose
1. 安装Docker Compose
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -version
2. 卸载Docker Compose
rm /usr/local/bin/docker-compose
四. Docker 私有仓库
## Docker 私有仓库
1. 私有仓库搭建
docker pull registry
docker run -id --name=registry -p 5000:5000 registry
vim /etc/docker/daemon.json
{
"insecure-registries":["私有仓库服务器ip:5000"]}
systemctl restart docker
docker start registry
2. 将镜像上传至私有仓库
docker tag centos:7 私有仓库服务器IP:5000/centos:7
docker push 私有仓库服务器IP:5000/centos:7
3. 从私有仓库拉取镜像
docker pull 私有仓库服务器ip:5000/centos:7