Docker部署nginx集群

1. 利用Dockerfile文件生成nginx镜像

[root@localhost ~]# docker build -t nginx_centos https://pan.shuaiguoer.com/Dockerfile/nginx

2.  以后台运行nginx,并指定名称,随机端口

[root@localhost ~]# docker run -itd --name nginx1 -p 2021:22 -p 8081:80 nginx_centos
ff166d27d1735a401551fb767dc59f16f798c78dd2830704abed9903f6b0a207

[root@localhost ~]# docker run -itd --name nginx2 -p 2022:22 -p 8082:80 nginx_centos
247c34d8a5140077e5ff74b2649b713893246b83a36c0e6a707b5280f1113e71

[root@localhost ~]# docker run -itd --name nginx3 -p 2023:22 -p 8083:80 nginx_centos
4120b14b17cca7462f0b8185f1ec6d07ff833bd80233ba0537fb4cbd104739ed

3. 分别进入每个nginx容器,修改其html文件

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
cac14b2bcd68        nginx_centos        "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes        0.0.0.0:32779->80/tcp   nginx3
3aa71721cbd0        nginx_centos        "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes        0.0.0.0:32778->80/tcp   nginx2
fa8e5a72ba8e        nginx_centos        "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes        0.0.0.0:32777->80/tcp   nginx1

[root@localhost ~]# docker exec -it nginx1 /bin/bash
[root@4762a1cdd5f9 /]# cd /usr/local/nginx/html/
[root@4762a1cdd5f9 html]# vi index.html
111111111111111111111111

[root@localhost ~]# docker exec -it nginx2 /bin/bash
[root@cac14b2bcd68 /]# cd /usr/local/nginx/html/
[root@cac14b2bcd68 html]# vi index.html
2222222222222222

[root@localhost ~]# docker exec -it nginx3 /bin/bash
[root@d5a5d58eee5c /]# cd /usr/local/nginx/html/
[root@d5a5d58eee5c html]# vi index.html
33333333333333333

4. 本地安装nginx

安装步骤见上图Dockerfile

5. 修改本地nginx配置文件,实现轮询

[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf

 

6. 重载nginx

nginx -s reload

7.访问本地测试是否成功

[root@localhost ~]# curl 127.0.0.1
11111111111111

[root@localhost ~]# curl 127.0.0.1
22222222222222222222

[root@localhost ~]# curl 127.0.0.1
3333333333333333333

安装ssh管理docker

8. 分别进入容器执行下面的操作

[root@localhost ~]# docker exec -it nginx1 /bin/bash
[root@localhost ~]# docker exec -it nginx2 /bin/bash
[root@localhost ~]# docker exec -it nginx3 /bin/bash

9. 安装服务

[root@4762a1cdd5f9 /]# yum -y install passwd openssl openssh-server openssh-clients

10. 启动sshd

[root@4762a1cdd5f9 /]# /usr/sbin/sshd -D &
这时报以下错误:
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
执行以下命令解决:
[root@4762a1cdd5f9 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@4762a1cdd5f9 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@4762a1cdd5f9 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N ''

11. 修改 /etc/ssh/sshd_config 配置信息:

[root@4762a1cdd5f9 /]# vi /etc/ssh/sshd_config 

UsePAM yes 改为 UsePAM no(直接注释掉!)
UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no

12. 设置root密码

[root@4762a1cdd5f9 /]# passwd root
输入两次确认密码

13. 启动ssh服务

[root@4762a1cdd5f9 /]# /usr/sbin/sshd –D &

14. 测试是否可以用本地+端口访问(-p:指定端口)

[root@localhost ~]# ssh 127.0.0.1 -p 2021
root@127.0.0.1's password: 
Last login: Mon Apr 22 06:36:52 2019 from gateway
[root@4762a1cdd5f9 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 272  bytes 35090 (34.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 203  bytes 33662 (32.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Last modification:July 23rd, 2020 at 11:32 pm
如果觉得我的文章对你有用,请随意赞赏