Docker集群安装和配置
一、修改主机名
修改主机名
[root@xianchaomaster1~]# hostnamectl set-hostname xianchaomaster1 && bash
关闭防火墙
[root@xianchaomaster1 ~]# systemctl stop firewalld && systemctl disable firewalld
关闭selinux配置文件
[root@xianchaomaster1 ~]# setenforce 0
[root@xianchaomaster1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
注意:修改selinux配置文件之后,重启机器,selinux才能永久生效
配置时间同步
[root@xianchaomaster1 ~]# yum install -y ntp ntpdate
[root@xianchaomaster1 ~]# ntpdate cn.pool.ntp.org
编写计划任务
配置时间同步
[root@xianchaomaster1 ~]# yum install -y ntp ntpdate
[root@xianchaomaster1 ~]# ntpdate cn.pool.ntp.org
编写计划任务
[root@xianchaomaster1 ~]# crontab -e * */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
重启crond服务使配置生效:
[root@xianchaomaster1 ~]# systemctl restart crond
查看docker版本
[root@cka_master ~]# docker version
Client: Docker Engine - Community
Version: 24.0.7
API version: 1.43
Go version: go1.20.10
Git commit: afdd53b
Built: Thu Oct 26 09:11:35 2023
OS/Arch: linux/amd64
Context: default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
文件追加输入内容
EOF是END Of File的缩写,表示自定义终止符。
既然自定义,那么EOF就不是固定的,可以随意设置别名。
在linux按ctrl-d就代表EOF。
EOF一般会配合cat能够多行文本输出
<<EOF //开始
....
EOF //结束
[root@cka_master ~]# cat <<EFO > test.2
> 123
> 1234
> 12345
> EFO
[root@cka_master ~]# cat test.2
123
1234
12345
[root@cka_master ~]#
理解sysctl -p、sysctl -a、sysctl -w
[-p] //指定文件加载系统参数
[-a] //显示所有系统参数
[-w] //临时改变某个指定参数的,如(sysctl -w net.ipv4.ip_forward=1)
lsmod //查看已经载入的模块
[root@cka_master ~]# lsmod | grep br_net
br_netfilter 22256 0
bridge 146976 1 br_netfilter
[root@cka_master ~]#
阿里云镜像加速
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
docker常规命令
docker search [镜像名称] //查询镜像
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建
解析不到DNS服务,docker拉取不了镜像,可以配置8.8.8.8公共DNS
[root@cka_master ~]# docker pull centos //拉取镜像
[root@cka_master ~]# docker images //查看镜像
[root@cka_master ~]# docker rmi [镜像名]/[镜像ID] //删除镜像
[root@cka_master ~]# docker save -o centos.tar.gz centos //离线封装镜像
[root@cka_master ~]# docker load -i centos.tar.gz //离线解压
[pull] //拉取
[images] //镜像
[rmi] //删除
[-o] //导出
[-i] //导入
save命令
docker save [options] images [images...]
示例 :
docker save -o nginx.tar nginx:latest
或
docker save > nginx.tar nginx:latest
其中-o和>表示输出到文件,nginx.tar为目标文件,nginx:latest是源镜像名(name:tag)
//
load命令
docker load [options]
示例:
docker load -i nginx.tar
或
docker load < nginx.tar
其中-i和<表示从文件输入。会成功导入镜像及相关元数据,包括tag信息
//容器内部命令
[root@cka_master ~]# docker ps -a //查询所有容器
[root@cka_master ~]# docker rm [container_name] //删除容器
[root@cka_master ~]# docker rename [old_container_name] [new_container_name] //修改容器名
[root@cka_master ~]# docker attach [容器ID]/[名称] //进入容器
[root@cka_master ~]# docker exec -it [名称] /bin/bash //进入容器
[root@cka_master ~]# docker logs [容器ID]/[名称] //查看容器日志
//以守护进程启动一个容器
[root@cka_master ~]# docker run --name=yang -it centos /bin/bash //创建一个名yang的容器
[root@cka_master ~]# docker run --name=test1 -td centos //一个容器在后台运行
[root@xianchaomaster1 ~]# docker ps |grep [容器名称] //容器内查找镜像
[docker run] //创建并运行一个容器,处于运行状态
[docker pause] //让一个运行的容器暂停
[docker unpause] //让一个容器从暂停状态恢复运行
[docker stop] //停止一个运行的容器
[docker start] //让一个停止的容器再次运行
[docker rm] //删除一个容器
docker run运行并创建容器
[--name] 容器的名字
[-i] 交互式
[-t] 分配伪终端
[centos]: 启动docker需要的镜像
/bin/bash说明你的shell类型为bash,bash shell是最常用的一种shell, 是大多数Linux发行版默认的shell。 此外还有C shell等其它shell。
[-d]在后台运行docker
处理邮件提示
[root@cka_master mail]# cat /dev/null > /var/spool/mail/root //删除邮件
[root@cka_master ~]# echo "unset MAILCHECK" >> /etc/profile source /etc/profile
//禁止系统启动邮件检查
ls常用命令
ls
[-lh] //以长格式列出文件大小
[-lrt] //升序
[-lt] //降序
[-S] //文件大小排序
[-t] //修改时间排序