文章目录
alpine基础操作
创建用户和组
//创建用户
adduser
-h #创建家目录
-g #用户的详细信息
-s #登录shell
-G #组
-S #创建系统用户
-D #不分配密码
-H #不创建家目录
-u #UID
-k #创建框架目录
//删除用户
deluser
apk源
//alpine默认为国外源,下载较慢,可改为国内源较快一点
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
apk
apk update
//更新最新镜像源列表
apk update
apk search
//查找
apk search #查找所以可用软件包
-v #查找所以可用软件包及其描述内容
-v 'gcc' #通过软件包名称查找软件包
-v -d 'docker' #通过描述文件查找特定的软件包
apk add
apk add wget #安装一个软件
apk add wget vim #安装多个软件
apk add --no-cache -U apache2 #不使用本地镜像源缓存,相当于先执行update,再执行add
apk del
//删除vim
apk del vim
dockerfile基于alpine制作haproxy镜像
准备两台网站服务
//httpd服务
[root@192 ~]# docker run -d --name web01 httpd
c2947f44ce68e701944f67aed2cfc6cb3ed95d85ab596dcaf98b270bfaf5979e
[root@192 ~]# docker inspect web01
......
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
......
[root@192 ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>
//nginx服务
[root@192 ~]# docker run -d --name web02 nginx
52354f10b03ac1918403601a93d5e43acb6275ca5e7c5ed10f55aa8748f0a454
[root@192 ~]# docker inspect web02
......
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
......
[root@192 ~]# curl 172.17.0.4
<!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>
dcokerfile
//目录结构
[root@192 ~]# tree haproxy/
haproxy/
├── Dockerfile
├── entrypoint.sh
└── files
├── haproxy
├── haproxy-2.4.0.tar.gz
└── install.sh
1 directory, 5 files
//Dockerfile
[root@192 haproxy]# cat Dockerfile
FROM alpine
LABEL MAINTAINER='wawa 1@2.com'
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV version 2.4.0
COPY files /tmp/
COPY entrypoint.sh /
RUN /tmp/install.sh
EXPOSE 80 8189
WORKDIR /usr/local/haproxy
ENTRYPOINT ["/entrypoint.sh"]
//install.sh
[root@192 files]# cat install.sh
#!/bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
adduser -S -H -s /sbin/nologin haproxy
addgroup haproxy
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
cd /tmp/
tar xf haproxy-${version}.tar.gz
cd haproxy-$version
make clean
make -j $(nproc) \
TARGET=linux-musl \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 && \
make install PREFIX=/usr/local/haproxy && \
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
mkdir /usr/local/haproxy/conf
apk del gcc make
rm -rf /var/cache/* /tmp/*
//entrypoint.sh
[root@192 haproxy]# cat 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
count=1
for rs_ip in $(cat /tmp/RSs.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg << EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
//主机文件
[root@192 ~]# mkdir -p /haproxy_host
[root@192 ~]# cd /haproxy_host/
[root@192 haproxy_host.txt]# cat RSs.txt
172.17.0.3
172.17.0.4
172.17.0.5
172.17.0.6
172.17.0.7
//构建镜像
[root@192 ~]# docker build -t soumnswa/haproxy:v2.0 haproxy
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
soumnswa/haproxy v2.0 5a05b658c7b0 4 minutes ago 54.1MB
//使用存储卷开启容器
[root@192 ~]# docker run -d --name haproxy -p 80:80 -p 8189:8189 -v /haproxy_host:/tmp soumnswa/haproxy:v2.0
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
02d22d9695ad soumnswa/haproxy:v2.0 "/entrypoint.sh" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy
52354f10b03a nginx "/docker-entrypoint.…" About an hour ago Up About an hour 80/tcp web02
c2947f44ce68 httpd "httpd-foreground" About an hour ago Up About an hour 80/tcp web01
访问测试