alpine系统
常用命令
apk update //更新最新镜像源列表
apk search //查找所以可用软件包
apk search -v //查找所以可用软件包及其描述内容
apk search -v ‘acf*’ //通过软件包名称查找软件包
apk search -v -d ‘docker’ //通过描述文件查找特定的软件包
apk add openssh //安装一个软件
apk add openssh openntp vim //安装多个软件
apk add --no-cache mysql-client //不使用本地镜像源缓存,相当于先执行update,再执行add
apk info //列出所有已安装的软件包
apk info -a zlib //显示完整的软件包信息
apk info --who-owns /sbin/lbu //显示指定文件属于的包
apk upgrade //升级所有软件
apk upgrade openssh //升级指定软件
apk upgrade openssh openntp vim //升级多个软件
apk add --upgrade busybox //指定升级部分软件包
apk del openssh //删除一个软件
服务管理
alpine没有使用fedora的systemctl来进行服务管理,使用的是RC系列命令:
rc-update //主要用于不同运行级增加或者删除服务
rc-status //主要用于运行级的状态管理
rc-service //主用于管理服务的状态
rc-status -a //列出系统所有服务
拉取alpine系统镜像
[root@localhost haproxy_dockerfile]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
安装haproxy
结构
.
├── Dockerfile
├── files
│ ├── 1
│ ├── entrypoint.sh
│ ├── haproxy-2.4.0.tar.gz
│ └── install.sh
└── sysctl.conf
编写Dockerfile
# This dockerfile uses the centos image
# 基础镜像
FROM alpine
# 维护者信息
LABEL MAINTAINER=dzc@163.com
# 设置变量
ENV version 2.4.0
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV RSs ""
# 复制文件
COPY files /tmp/
COPY sysctl.conf /etc/
# 执行安装脚本安装
RUN /tmp/install.sh
ENTRYPOINT ["/entrypoint.sh"]
编写entrypoint.sh脚本
#!/bin/sh
cat > /usr/local/haproxy/conf/haproxy.cfg <<EOF
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
EOF
i=1
for rs_ip in $RSs;do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
server web$i $rs_ip:80 check inter 2000 fall 5
EOF
let i++
done
haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
编写haproxy安装脚本install.sh
#!/bin/sh
sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/" /etc/apk/repositories
apk update
apk add --no-cache -U make gcc pcre-dev bzip2-dev openssl-dev elogind-dev libc-dev dahdi-tools dahdi-tools-dev libexecinfo libexecinfo-dev ncurses-dev zlib-dev zlib
adduser -S -H -s /sbin/nologin haproxy
cd /tmp
tar xf haproxy-${version}.tar.gz
cd haproxy-${version}
make \
TARGET=linux-musl \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 && \
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
mkdir -p /usr/local/haproxy/conf
rm -rf /tmp/haproxy-${version} /tmp/install.sh
apk del gcc make
编写sysctl.conf文件,使用文件替换掉原文件
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
构建镜像
Sending build context to Docker daemon 3.603MB
Step 1/9 : FROM alpine
---> c059bfaa849c
Step 2/9 : LABEL MAINTAINER=dzc@163.com
---> Using cache
---> 5a8193693135
Step 3/9 : ENV version 2.4.0
---> Using cache
---> 15258b39ef5f
Step 4/9 : ENV PATH /usr/local/haproxy/sbin:$PATH
---> Using cache
---> 8f77772110e8
Step 5/9 : ENV RSs ""
---> Using cache
---> 905b3c04f58e
Step 6/9 : COPY files /tmp/
---> b4fd814746d6
Step 7/9 : COPY sysctl.conf /etc/
---> 17f65e96d7d0
Step 8/9 : RUN /tmp/install.sh
---> Running in 45d565f210b7
Removing intermediate container 45d565f210b7
---> d4586e5548fb
Step 9/9 : ENTRYPOINT ["/entrypoint.sh"]
---> Running in f8eeb9856d26
Removing intermediate container f8eeb9856d26
---> 8b701066dc60
Successfully built 8b701066dc60
Successfully tagged haproxy:5.0
使用镜像创建容器
[root@localhost alpine_haproxy]# docker run -d --name haproxy -p 80:80 -e RSs="172.17.0.3 172.17.0.4" haproxy:6.0
2f678cc5e649db05ec76c79de80e9eed81c3ee145e27d31d9ca8988326aa2ded
创建apache与nginx容器
[root@localhost alpine_haproxy]# docker run -d --name httpd httpd
b7ea3654a86f637a14bcb1d7564a558ed7011f0efedf4f8c59ae4466896240ec
[root@localhost alpine_haproxy]# docker run -d --name nginx nginx
11c1e7fbd21cbdfbff46aa7f9fdd62790c2d284efd761003e5f52a380826f027
测试
[root@localhost haproxy_dockerfile]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost haproxy_dockerfile]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
评论区