1.安装docker:
setenforce 0 ##临时禁用selinux
getenforce ##查看是否已经临时禁用
systemctl stop firewalld ##关闭防火墙
systemctl disable firewalld
yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-selinux
docker-engine-selinux
docker-engine ##删除以前docker
yum install -y yum-utils device-mapper-persistent-data lvm2 ##安装docker所需依赖
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo ##添加docker源
yum makecache fast ##更新缓存
Yum -y install docker-ce ##安装docker-ce
Systemctl start dokcer ##启动docker
Systemctl enable docker ##开启启动
[root@localhost ~]# cat /etc/docker/daemon.json ##添加docker下载镜像加速
{
“registry-mirrors”: [“https://docker.mirrors.ustc.edu.cn”]
}
[root@localhost ~]# systemctl daemon-reload 重新加载配置
[root@localhost ~]# systemct restart docker 重启docker
2.为了方便管理docker容器我们搭建portainer
docker pull protainer ##下载portainer镜像
docker run -d -p 9000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --name prtainer portainer/portainer ##–restart=always 如果docker重启容器也重启 -v挂在主机目录,-p指定端口号,–name 指定容器名称
浏览器访问(第一次登录需要设置密码,然后选择local(本地)集群选择remote)
3.下载tomcat与nginx镜像,并创建volume卷
[root@localhost ~]# docekr pull nginx
[root@localhost ~]# docekr pull tomcat
[root@localhost ~]# docker images |grep -Ee ^nginx -e ^tomcat
[root@localhost ~]# docker volume list |grep -e nginx -e tomcat_webapps ##如果没有就创建
[root@localhost ~]# docker volume create nginx_conf
[root@localhost ~]# docker volume create nginx_log
[root@localhost ~]# docker volume create nginx_html
[root@localhost ~]# docker volume create tomcat_webapps
[root@localhost ~]# docker volume create tomcat_webapps2
[root@localhost ~]# docker volume list ##查看所有volume
Portainer查看:
4.运行tomcat容器,因为要负载均衡所以运行俩次
[root@localhost ~]# docker run -d -p 8080:8080 --name tomcat_server -v tomcat_webapps:/usr/local/tomcat/webapps tomcat ##运行第一个tomcat
[root@localhost ~]# docker run -d -p 8082:8080 --name tomcat_server_2 -v tomcat_webapps2:/usr/local/tomcat/webapps tomcat ##运行第二个tomcat
[root@localhost ~]# docker ps |grep tomcat
访问测试:
设置负载均衡界面:
[root@localhost ~]# cd /var/lib/docker/volumes/tomcat_webapps/_data/
[root@localhost _data]# mkdir test
[root@localhost _data]# cd test/
[root@localhost test]# cat index.jsp
[root@localhost ~]# cd /var/lib/docker/volumes/tomcat_webapps2/_data/
[root@localhost _data]# mkdir test
[root@localhost _data]# cd test/
[root@localhost test]# cat index.jsp
访问测试页面:
5.运行nginx容器,并修改配置文件设置负载均衡。
[root@localhost ~]# docker run -d -p 80:80 --name web_server -v nginx_conf:/etc/ -v nginx_html:/usr/share/nginx/html nginx
[root@localhost ~]# cd /var/lib/docker/volumes/nginx_conf/_data/nginx
[root@localhost nginx]# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr -
r
e
m
o
t
e
u
s
e
r
[
remote_user [
remoteuser[time_local] “KaTeX parse error: Double superscript at position 34: … '̲status
b
o
d
y
b
y
t
e
s
s
e
n
t
"
body_bytes_sent "
bodybytessent"http_referer” ’
‘“
h
t
t
p
u
s
e
r
a
g
e
n
t
"
"
http_user_agent" "
httpuseragent""http_x_forwarded_for”’;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream tomcat {
server 172.17.0.6:8080;
server 172.17.0.7:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
location ~* \.(jsp|do)$ {
proxy_pass http://tomcat;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
注意upstream是tomcat的地址,可以通过portainer查看:
[root@localhost nginx]# docker exec -it web_server /bin/bash ##进入nginx容器
root@394bf9d711c9:/# nginx -t ##检查配置文件有没有问题
root@394bf9d711c9:/# nginx -s reload ##重新加载
root@394bf9d711c9:/# nginx ##启动
Ctrl+p,ctrl+q保存配置退出容器
[root@localhost _data]# echo “haha” > /var/lib/docker/volumes/nginx_html/_data/index.html ##修改nginx首页
测试:
测试负载均衡(f5刷新):
在portainer上查看实时docker日志:
关于更多portainer相关的操作,需要自己去点击