Docker单机编排工具
Docker-Compose介绍
Compose是用于定义和运行多容器 Docker应用工具
通过Compose 可以使用YML文件来配置应用程序所需要的所有服务
Docker Compose使用的三步:
1.使用docker file 定义应用程序的环境
2.docker-compose定义构成应用程序的服务
3.启动Compose,就相当于启动应用
版本号对应
yaml使用compose的版本:version 2-3的大版本
docker版本:docker version
docker-compose版本:docker-compose version
安装指定版本的Docker-compose
# 官方下载
curl -L "https://github.com/docker/compose/releases/download/v1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# yum安装docker-Compose
yum install -y docker-compose
# 手动传输docker-compose安装包,需要进行以下操作
[root@docker02 ~]# mv docker-compose-linux-x86_64 /usr/bin/docker-compose
[root@docker02 ~]# chmod +x /usr/bin/docker-compose
[root@docker02 ~]# docker-compose version
Docker Compose version v2.21.0
Docker-compose模板
version: '版本号'
services:
服务名称1:
image: 容器镜像
container_name: 容器名称
environment:
环境变量1=值1(环境变量1: 值1)
环境变量2=值2(环境变量2: 值2)
user: 宿主机用户:容器用户#(如果是相同用户只需要填写一个)
volumes:
- 宿主机数据目录:容器内数据目录
ports:
- 宿主机端口:容器内映射端口
networks:
- 自定义网络名称
links:
- namenode
command:
- 数据库使用字符集变量时可以用
restart: always
服务名称2:
image: 容器镜像
container_name: 容器名称
environment:
环境变量1=值1(环境变量1: 值1)
环境变量2=值2(环境变量2: 值2)
user: 宿主机用户:容器用户#(如果是相同用户只需要填写一个)
volumes:
- 宿主机数据目录:容器内数据目录
ports:
- 宿主机端口:容器内映射端口
networks:
- 自定义网络名称
links:
- namenode
command:
- 数据库使用字符集变量时可以用
depends_on:
- 依赖服务
restart: always
networks:
default:
externnal: true
name: 自定义网络名称
Docker-compose部署zabbix实战
Docker run 语法
# 1.启动zabbix数据库
docker run \
--name zabbix-db \
-v /data/zabbix:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE='zabbix' \
-e MYSQL_USER='zabbix' \
-e MYSQL_PASSWORD='123' \
-p 33060:3306 \
-d mysql:5.7 \
--character-set-server=utf8 \
--collation-server=utf8_bin
# 2.启动zabbix-server
docker run \
--name zabbix-server \
--link zabbix-db \
-e DB_SERVER_HOST="zabbix-db" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123" \
-p 10051:10051 \
-d zabbix/zabbix-server-mysql:5.0.37-alpine
# 3.启动zabbix前端
docker run \
--link zabbix-db \
--link zabbix-server \
--name zabbix-web \
-e DB_SERVER_HOST="zabbix-db" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123" \
-e ZBX_SERVER_HOST="zabbix-server" \
-e PHP_TZ="Asia/Shanghai" \
-p 8080:8080 \
-d zabbix/zabbix-web-nginx-mysql:5.0.37-alpine
Docker-compose
[root@docker01 zabbix]# cat docker-compose.yaml
version: '2.2'
services:
mysql57:
image: mysql:5.7
container_name: mysql57
environment:
MYSQL_ROOT_PASSWORD: '123'
MYSQL_DATABASE: 'zabbix'
MYSQL_USER: 'zabbix'
MYSQL_PASSWORD: '123'
volumes:
- /data/mysql57:/var/lib/mysql
ports:
- 3306:3306
command:
- --character-set-server=utf8
- --collation-server=utf8_bin
restart: always
zabbix-server:
image: zabbix/zabbix-server-mysql:5.0.37-alpine
container_name: zabbix-server
environment:
DB_SERVER_HOST: 'mysql57'
MYSQL_USER: 'zabbix'
MYSQL_PASSWORD: '123'
ports:
- 10051:10051
links:
- mysql57
depends_on:
- mysql57
restart: always
zabbix-web:
image: zabbix/zabbix-web-nginx-mysql:5.0.37-alpine
container_name: zabbix-web
environment:
DB_SERVER_HOST: 'mysql57'
MYSQL_USER: 'zabbix'
MYSQL_PASSWORD: '123'
ZBX_SERVER_HOST: 'zabbix-server'
PHP_TZ: 'Asia/Shanghai'
ports:
- 8080:8080
links:
- mysql57
- zabbix-server
depends_on:
- mysql57
- zabbix-server
restart: always
Dockercompose管理命令
# 启动docker-compose(指定配置文件)
[root@docker01 zabbix]# docker-compose -f zabbix-compose.yaml up
-f 指定配置文件
# 启动docker-compose
[root@docker01 zabbix]# docker-compose up
如不指定配置文件,则文件名需要改为docker-compose
# 后台启动
[root@docker01 zabbix]# docker-compose up -d
-d 后台启动
# 查看进程
[root@docker01 zabbix]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------------------------
mysql57 docker-entrypoint.sh --cha ... Up 0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp
zabbix-server /sbin/tini -- /usr/bin/doc ... Up 0.0.0.0:10051->10051/tcp,:::10051->10051/tcp
zabbix-web docker-entrypoint.sh Up 0.0.0.0:8080->8080/tcp,:::8080->8080/tcp, 8443/tcp
# 重启(启动\停止)docker-compose
[root@docker01 zabbix]# docker-compose restart(start\stop)
# 指定容器重启(启动\停止)
[root@docker01 zabbix]# docker-compose restart(start\stop) 容器名
# 实时查看docker-compose日志
[root@docker01 zabbix]# docker-compose logs -f
# 指定容器查看docker-compose日志
[root@docker01 zabbix]# docker-compose logs 容器名
# docker-compose中删除指定容器
[root@docker01 zabbix]# docker-compose down 容器名
Docker-compose部署gitlab jenkins
[root@docker02 ~]# vim docker-compose.yml
version: '3.6'
services:
gitlab:
image: 'gitlab/gitlab-ce:latest'
restart: always
hostname: '10.0.0.102'
environment:
GITLAB_OMNIBUS_CONFIG: |
gitlab_rails['gitlab_shell_ssh_port'] = '2222'
prometheus['enable'] = false
prometheus['monitor_kubernetes'] = false
prometheus_monitoring['enable'] = false
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
grafana['enable'] = false
ports:
- '8888:80'
- '4443:443'
- '2222:22'
volumes:
- '/data/gitlab/config:/etc/gitlab'
- '/data/gitlab/logs:/var/log/gitlab'
- '/data/gitlab/data:/var/opt/gitlab'
shm_size: '256m'
docker:
image: docker:dind
privileged: true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
jenkins:
image: jenkins/jenkins
restart: always
user: root
depends_on:
- docker
ports:
- "8080:8080"
- "50000:50000"
volumes:
- /root/.ssh:/root/.ssh
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /home/jenkins_home:/var/jenkins_home
- /root/.docker/config.json:/root/.docker/config.json
# 启动gitlab jenkins
[root@docker02 ~]# docker-compose up -d
# 查看gitlab密码
[root@docker02 ~]# cat /data/gitlab/config/initial_root_password
Password: kMcfmyceuxs/kNgQ6oRlPw1ssNCW6dRaQL4aCURHzmU=
# 查看jenkins密码
[root@docker02 ~]# cat /home/jenkins_home/secrets/initialAdminPassword
8183e9b8ec56498ebdb83b1bc7cc2515
如何解决jenkins中无法使用docker命令
在Jenkins容器中,无法直接使用privileged选项来设置特权模式。相反,你可以通过其他方法解决这个问题。
一种常见的做法是在Docker Compose文件中添加一个特权容器来处理与Docker相关的操作。
1.创建一个额外的容器服务,用于运行Docker命令,并设置为使用特权模式。
docker:
image: docker:dind
privileged: true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
2.将Jenkins容器和新创建的docker容器连接起来。
services:
jenkins:
...
depends_on:
- docker
gitlab成品展示
jenkins成品展示
Docker-compose部署wordpress
[root@docker02 ~]# vim wordpress.yml
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8081:80
environment:
WORDPRESS_DB_HOST: mysql57
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: 123
WORDPRESS_DB_NAME: wp
volumes:
- /data/wordpress:/var/www/html
mysql57:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: wp
MYSQL_USER: wp_user
MYSQL_PASSWORD: 123
MYSQL_RANDOM_ROOT_PASSWORD: '123'
volumes:
- /data/mysql57:/var/lib/mysql
volumes:
wordpress:
db:
# 启动wordpress
[root@docker02 ~]# docker-compose -f wordpress.yml up -d
Docker-compose部署es集群(包含elk)
[root@docker02 elk]# cat logstash.conf
input {
beats {
port => 5044
}
}
filter {
# 在这里添加任何你需要的过滤器,根据数据的需求
# 例如,你可以添加 grok 过滤器来解析日志消息
# grok {
# match => { "message" => "%{TIMESTAMP_ISO8601:timestamp} %{LOGLEVEL:loglevel} %{GREEDYDATA:message}" }
# }
}
output {
elasticsearch {
hosts => ["elasticsearch:9200"]
index => "your-custom-index-%{+YYYY.MM.dd}"
}
}
[root@docker02 elk]# cat pipeline.yml
- pipeline.id: main
path.config: "/usr/share/logstash/pipeline/logstash.conf"
# 编写集群的docker-compose
[root@docker02 elk]# cat docker-compose.yaml
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
container_name: elasticsearch
environment:
- node.name=node1
- cluster.name=mycluster
- discovery.seed_hosts=elasticsearch2,elasticsearch3
- cluster.initial_master_nodes=node1,node2,node3
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
container_name: elasticsearch2
environment:
- node.name=node2
- cluster.name=mycluster
- discovery.seed_hosts=elasticsearch,elasticsearch3
- cluster.initial_master_nodes=node1,node2,node3
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata2:/usr/share/elasticsearch/data
ports:
- 9201:9200
- 9301:9300
elasticsearch3:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
container_name: elasticsearch3
environment:
- node.name=node3
- cluster.name=mycluster
- discovery.seed_hosts=elasticsearch,elasticsearch2
- cluster.initial_master_nodes=node1,node2,node3
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata3:/usr/share/elasticsearch/data
ports:
- 9202:9200
- 9302:9300
kibana:
image: docker.elastic.co/kibana/kibana:7.14.0
container_name: kibana
environment:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
ports:
- 5601:5601
logstash:
image: docker.elastic.co/logstash/logstash:7.14.0
container_name: logstash
volumes:
- ./logstash/config:/usr/share/logstash/config
- ./logstash/pipeline:/usr/share/logstash/pipeline
ports:
- 5044:5044
volumes:
esdata1:
esdata2:
esdata3:
[root@docker02 ~]# docker-compose up -d
解决报错信息
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: sysctl "vm.max_map_count" is not in a separate kernel namespace: unknown
# 修改内存映射区域数量配置
[root@docker02 ~]# vim /etc/sysctl.conf
vm.max_map_count=262144
# 使新的配置生效
[root@docker02 ~]# sysctl -p /etc/sysctl.conf
vm.max_map_count = 262144
# 重新启动 Docker 服务
[root@docker02 ~]# systemctl restart docker
页面解析
"name":节点名称,这里是 "node1/2/3"。
"cluster_name":集群名称,这里是 "mycluster"。
"cluster_uuid":集群的唯一标识符,这里是 "B7VgHnXlRHinkMBryeIb1A"。
"version":Elasticsearch 的版本信息。
"number":版本号,这里是 "7.14.0"。
"build_flavor":构建版本的类型,这里是 "default"。
"build_type":构建类型,这里是 "docker"。
"build_hash":构建的哈希值。
"build_date":构建日期。
"build_snapshot":是否是快照版本。
"lucene_version":所使用的 Lucene 版本。
"minimum_wire_compatibility_version":与旧版本 Elasticsearch 之间的最低兼容性版本。
"minimum_index_compatibility_version":与旧索引之间的最低兼容性版本。
"tagline":Elasticsearch 的标语,这里是 "You Know, for Search"。
##这段信息主要用来描述 Elasticsearch 节点及其相关的版本和集群信息。
Docker图形化界面
# 下载镜像包
docker pull portainer/portainer
portainer/.portainer.tar.gz
# 启动容器
docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock --restart=always --name portainer portainer/portainer
修改名称
添加新docker
# docker 修改远程连接
[root@docker02 ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock
# 重启
[root@docker02 ~]# systemctl daemon-reload
[root@docker02 ~]# systemctl restart docker
docker主机查看