SaltStack 官方提供有REST API格式的 salt-api 项目,将使Salt与第三方系统集成变得尤为简单。
[root@server1 ~]# yum install -y salt-api ##安装salt-api
[root@server1 certs]# cd /etc/pki/tls/
[root@server1 tls]# cd private/
[root@server1 private]# ls
[root@server1 private]# openssl genrsa 1024
[root@server1 private]# openssl genrsa 1024 > localhost.key
[root@server1 private]# cd ..
[root@server1 tls]# cd certs/
[root@server1 certs]# ls
ca-bundle.crt ca-bundle.trust.crt localhost.key make-dummy-cert Makefile renew-dummy-cert
[root@server1 certs]# make testcert ##生成证书
[root@server1 certs]# ll localhost.crt ##生成证书
-rw------- 1 root root 1038 Jan 25 07:05 localhost.crt
[root@server1 certs]# ls
ca-bundle.crt localhost.crt make-dummy-cert renew-dummy-cert
ca-bundle.trust.crt localhost.key Makefile
[root@server1 certs]# cd /etc/salt/master.d/
[root@server1 master.d]# ls
激活rest_cherrypy模块:
[root@server1 master.d]# vim api.conf
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/private/localhost.key
创建用户认证文件:
[root@server1 master.d]# vim auth.conf
重启服务:
[root@server1 master.d]# systemctl restart salt-master
[root@server1 master.d]# systemctl enable --now salt-api
获取认证token:
[root@server1 master.d]# curl -sSk https://localhost:8000/login \
推送任务: token值是上面命令获取的toekn值。
[root@server1 master.d]# curl -sSk https://localhost:8000 \
[root@server1 ~]# vim saltapi.py
[root@server1 ~]# python saltapi.py
[root@server2 ~]# systemctl stop httpd
[root@server2 ~]# netstat -antlp | grep :80
tcp6 0 0 :::80 :::* LISTEN 4360/httpd
[root@server1 yum.repos.d]# vim docker.repo
[root@server1 yum.repos.d]# yum install -y docker-ce
[docker]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
gpgcheck=0
[root@server1 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo ##下载CentOS源
[root@server1 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@server1 yum.repos.d]# vim CentOS-Base.repo
:%s/$releasever/7/g
[root@server1 yum.repos.d]# yum repolist
[root@server1 yum.repos.d]# yum install -y docker-ce
[root@server1 ~]# systemctl start docker
[root@server1 ~]# systemctl enable docker
[root@server1 ~]# docker info
[root@server1 ~]# sysctl -a | grep bridge-nf-call-iptables
[root@server1 ~]# vim /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
[root@server1 ~]# sysctl --system
[root@server1 ~]# docker info
Registry: https://index.docker.io/v1/
[root@server1 ~]# ip addr
[root@server1 ~]# docker search yakexi007
[root@server1 ~]# docker pull yakexi007/game2048
[root@server1 ~]# yum install -y bash-*
[root@server1 ~]# docker image
[root@server1 ~]# docker run -d --name demo -p 80:80 yakexi007/game2048
879a270b347afa2cd7ca3e27e3b28d564563a607582528d77d9df3cf0d3093d8
[root@server1 ~]# docker ps
[root@server1 ~]# docker ps -a
[root@server1 ~]# docker rm -f demo
[root@server1 ~]# docker ps -a
[root@server1 ~]# docker search yakexi007
[root@server1 ~]# docker pull yakexi007/mario
[root@server1 ~]# docker images
[root@server1 ~]# docker history yakexi007/mario:latest
[root@server1 ~]# docker run -d --name demo -p 80:8080 yakexi007/mario
[root@server1 ~]# docker ps
容器层以下所有镜像层都是只读的
docker从上往下依次查找文件
容器层保存镜像变化的部分,并不会对镜像本身进行任何修改
一个镜像最多127层
root@server1 ~]# docker tag yakexi007/game2048:latest game2048:latest
[root@server1 ~]# docker rmi yakexi007/game2048 ##容器改名
[root@server1 ~]# docker pull busybox
[root@server1 ~]# docker run -it busybox
/ # ip addr
/ # uname -r
3.10.0-957.el7.x86_64
/ # exit
[root@server1 ~]# uname -r
3.10.0-957.el7.x86_64 ##共享宿主机内核
[root@server1 ~]# docker rm -f demo
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6565995687cf busybox "sh" 2 hours ago Exited (130) 2 hours ago stupefied_agnesi
[root@server1 ~]# docker start 6565995687cf
6565995687cf
[root@server1 ~]# docker container attach 6565995687cf
/ # ip addr
ctrl+d 退出加删除
[root@server1 ~]# docker ps
[root@server1 ~]# docker start 6565995687cf
6565995687cf
[root@server1 ~]# docker container attach 6565995687cf
/ # ls
bin dev etc home proc root sys tmp usr var
/ # read escape sequence
ctrl+pq ##直接退出
共享宿主机的kernel
base镜像提供的是最小的Linux发行版
同一docker主机支持运行多种Linux发行版
采用分层结构的最大好处是:共享资源
[root@server1 ~]# docker commit -m "add files" demo demo:v1
[root@server1 ~]# docker images
[root@server1 ~]# docker history demo:v1
[root@server1 ~]# docker history busybox:latest
[root@server1 ~]# docker run -it --name demo demo:v1 ##运行容器
[root@server1 ~]# docker history demo:v1
[root@server1 ~]# docker rm demo #删除容器
demo
[root@server1 ~]# docker rmi demo:v1 #删除镜像
[root@server1 ~]# docker history busybox:latest ##查看镜像的分层结构
docker commit 构建新镜像三部曲
运行容器
修改容器
将容器保存为新的镜像
缺点:
效率低、可重复性弱、容易出错
使用者无法对镜像进行审计,存在安全隐患
root@server1 ~]# mkdir docker
[root@server1 ~]# cd docker/
[root@server1 docker]# vim Dockerfile ##镜像的构建
FROM busybox
RUN touch file1
RUN mkdir westos
[root@server1 docker]# docker build -t demo:v1 .
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# docker build -t demo:v2 .
FROM demo:v1
RUN touch file2
RUN mkdir redhat
[root@server1 docker]# docker rmi demo:v1
Untagged: demo:v1
[root@server1 docker]# docker rmi demo:v2
[root@server1 docker]# ls
Dockerfile
[root@server1 docker]# vim index.html
www.westos.org
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# docker build -t demo:v1 .
FROM busybox ##指定base镜像,如果本地不存在会从远程仓库下载
RUN touch file1
COPY index.html / ##把文件从build context复制到镜像
ADD 用法与COPY类似,不同的是src可以是归档压缩文件,文件会被自
动解压到dest,也可以自动下载URL并拷贝到镜像:
ADD test.tar.gz /mnt
ENV 设置环境变量,变量可以被后续的指令使用:
ENV HOSTNAME server1
EXPOSE 如果容器中运行应用服务,可以把服务端口暴露出去:
EXPOSE 80
VOLUME 申明数据卷,通常指定的是应用的数据挂载点:
VOLUME ["/data"]
数据持久化
删除卷
CMD 与 ENTRYPOINT
这两个指令都是用于设置容器启动后执行的命令,但CMD会被
docker run后面的命令行覆盖,而ENTRYPOINT不会被忽略,一定会
被执行
ENTRYPOINT [“echo”, “hello”]
CMD [“world”]
Shell格式底层会调用/bin/sh -c来执行命令,可以解析变量,
[root@server1 docker]# docker images | grep demo | awk '{print $1":"$2}'
[root@server1 docker]# docker rmi `docker images | grep demo | awk '{print $1":"$2}'`
[root@server1 docker]# cd /etc/dockerr
[root@server1 docker]# vim daemon.json
{
"registry-mirrors": ["https://nmcjqb9k.mirror.aliyuncs.com"]
}
[root@server1 docker]# systemctl daemon-reload
[root@server1 docker]# systemctl reload docker
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://nmcjqb9k.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
[root@server1 ~]# cd docker/
[root@server1 docker]# docker pull nginx
[root@server1 docker]# docker image prune ##删除没有id的镜像
[root@server1 docker]# docker images
nginx latest f6d0b4767a6c 13 days ago 133MB
[root@server1 docker]# docker history f6d0b4767a6c
ENV NGINX_VERSION=1.19.6
[root@server1 ~]# docker cp nginx-1.18.0.tar.gz demo:/
[root@server1 docker]# docker pull ubuntu
[root@server1 ~]# docker load -i rhel7.tar
[root@server1 ~]# docker run -it --name demo rhel7 bash
bash-4.2# vi dvd.repo
bash-4.2# rpmdb --rebuilddb
bash-4.2# yum install -y gcc pcre-devel
bash-4.2# mv nginx-1.18.0.tar.gz /mnt/
bash-4.2# tar zxf nginx-1.18.0.tar.gz
bash-4.2# ./configure
bash-4.2# yum install -y zlib-devel
bash-4.2# make
bash-4.2# make install
bash-4.2# whereis nginx
nginx: /usr/local/nginx
bash-4.2# /usr/local/nginx/sbin/nginx -t
bash-4.2# exit
[root@server1 docker]# vim Dockerfile
FROM rhel7
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.18.0.tar.gz /mnt
WORKDIR /mnt/nginx-1.18.0
RUN rpmdb --rebuilddb
RUN yum install -y gcc make pcre-devel zlib-devel
RUN ./configure
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t webserver:v1 .
[root@server1 ~]# docker rm -f demo
demo
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server1 docker]# docker run -d --name webserver webserver:v1
[root@server1 ~]# cd /var/lib/docker/volumes/6efb611344691f79bf36de0c8e3f2cbb1078032c5cb22d5a50195343a8cee0d1/_data
[root@server1 _data]# ls
50x.html index.html
[root@server1 _data]# curl 172.17.0.2
Welcome to nginx!title>
[root@server1 _data]# echo www.westos.org > test.html
[root@server1 _data]# curl 172.17.0.2/test.html
www.westos.org
| 选择最精简的基础镜像
l 减少镜像的层数
l 清理镜像构建的中间产物
l 注意优化网络请求
l 尽量去用构建缓存
l 使用多阶段构建镜像
实例分析
[root@server1 docker]# docker build -t webserver:v1 .
[root@server1 docker]# docker images webserver
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# cat Dockerfile
FROM rhel7 as build
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.18.0.tar.gz /mnt
WORKDIR /mnt/nginx-1.18.0
RUN rpmdb --rebuilddb && yum install -y gcc make pcre-devel zlib-devel &> /dev/null && ./configure &> /dev/null && make &> /dev/null && make install &> /dev/null && rm -fr /mnt/nginx-1.18.0 && yum remove -y gcc make && yum clean all
FROM rhel7
COPY --from=build /usr/local/nginx /usr/local/nginx
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t webserver:v3 .
[root@server1 docker]# docker images webserver
REPOSITORY TAG IMAGE ID CREATED SIZE
webserver v3 9318bb8566e1 37 seconds ago 144MB ##使用多阶段构建镜像 缩减到了144M,但还是比nginx官方镜像133M大很多
webserver v2 ac33a92dfa33 8 minutes ago 233MB ##清理镜像构建的中间产物
webserver v1 3c76174a3370 33 minutes ago 296MB #减少镜像层数
[root@server1 ~]# docker load -i base-debian10.tar
[root@server1 docker]# docker pull nginx:1.18.0
[root@server1 docker]# docker images
[root@server1 ~]# mkdir nginx
[root@server1 ~]# vim Dockerfile
FROM nginx:1.18.0 as base ##下载一个nginx1.18.0
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
ARG TIME_ZONE
RUN mkdir -p /opt/var/cache/nginx && \
cp -a --parents /usr/lib/nginx /opt && \
cp -a --parents /usr/share/nginx /opt && \
cp -a --parents /var/log/nginx /opt && \
cp -aL --parents /var/run /opt && \
cp -a --parents /etc/nginx /opt && \
cp -a --parents /etc/passwd /opt && \
cp -a --parents /etc/group /opt && \
cp -a --parents /usr/sbin/nginx /opt && \
cp -a --parents /usr/sbin/nginx-debug /opt && \
cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpcre.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
FROM gcr.io/distroless/base-debian10
COPY --from=base /opt /
EXPOSE 80 443
ENTRYPOINT ["nginx", "-g", "daemon off;"]
[root@server1 ~]# docker build -t webserver:v4 .
[root@server1 ~]# docker run -d --name webserver webserver:v4
e70b3305f5b84af071beb95c1b936fb874c1ee6d53015d9187be491a1b99064b
[root@server1 ~]# docker ps