Skip to content

Commit fe44518

Browse files
1
0 parents  commit fe44518

22 files changed

Lines changed: 528 additions & 0 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dockerfile

keepalived/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM alpine:3.7
2+
3+
COPY keepalived.sh /usr/bin/keepalived.sh
4+
5+
RUN apk --update -t --no-cache add keepalived iproute2 grep bash tcpdump sed && \
6+
chmod +x /usr/bin/keepalived.sh && \
7+
rm -f /var/cache/apk/* /tmp/*
8+
9+
COPY keepalived.conf /etc/keepalived/keepalived.conf
10+
11+
ENTRYPOINT ["/usr/bin/keepalived.sh"]

keepalived/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### Keepalived
2+
---
3+
Keepalived是一个基于VRRP协议来实现的服务高可用方案,可以利用其来避免单点故障。
4+
5+
### 变量
6+
---
7+
- VRID VRRP的ID,多节点要设置一致。
8+
- INTERFACE 绑定的网卡
9+
- VIRTUAL_IP VIP地址
10+
- VIRTUAL_MASK VIP的网段
11+
- CHECK_IP 检查的ip地址
12+
- CHECK_PORT 检查的端口号
13+
14+
### 版本
15+
---
16+
- 1.3.9 (docker tags: v1.3.9, latest) : keepalived版本为1.3.9
17+
18+
### 使用
19+
---
20+
```bash
21+
docker run -d --name keepalived --restart=always --net=host --cap-add=NET_ADMIN \
22+
-e VRID=53 \
23+
-e INTERFACE=ens33 \
24+
-e VIRTUAL_IP=192.168.50.10 \
25+
-e VIRTUAL_MASK=24 \
26+
-e CHECK_IP=any \
27+
-e CHECK_PORT=22 \
28+
zhangguanzhang/keepalived:1.3.9
29+
```

keepalived/keepalived.conf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
global_defs {
2+
router_id your_hostname
3+
vrrp_version 2
4+
vrrp_garp_master_delay 1
5+
vrrp_garp_master_refresh 2
6+
#Uncomment the next line if you'd like to use unique multicast groups
7+
#vrrp_mcast_group4 224.0.0.{{VRID}}
8+
}
9+
10+
vrrp_script chk_server {
11+
script "ss -ltn 'src {{CHECK_IP}}' | grep ':{{CHECK_PORT}} '"
12+
timeout 1
13+
interval 1 # check every 1 second
14+
fall 2 # require 2 failures for KO
15+
rise 2 # require 2 successes for OK
16+
}
17+
18+
vrrp_instance lb-vips {
19+
state BACKUP
20+
interface {{INTERFACE}}
21+
virtual_router_id {{VRID}}
22+
priority 100
23+
advert_int 1
24+
nopreempt
25+
track_script {
26+
chk_server
27+
}
28+
authentication {
29+
auth_type PASS
30+
auth_pass blahblah
31+
}
32+
virtual_ipaddress {
33+
{{VIRTUAL_IP}}/{{VIRTUAL_MASK}} dev {{INTERFACE}}
34+
}
35+
}

keepalived/keepalived.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
3+
# Substitute variables in config file.
4+
/bin/sed -i "s/{{VIRTUAL_IP}}/${VIRTUAL_IP}/g" /etc/keepalived/keepalived.conf
5+
/bin/sed -i "s/{{VIRTUAL_MASK}}/${VIRTUAL_MASK}/g" /etc/keepalived/keepalived.conf
6+
/bin/sed -i "s/{{CHECK_IP}}/${CHECK_IP}/g" /etc/keepalived/keepalived.conf
7+
/bin/sed -i "s/{{CHECK_PORT}}/${CHECK_PORT}/g" /etc/keepalived/keepalived.conf
8+
/bin/sed -i "s/{{VRID}}/${VRID}/g" /etc/keepalived/keepalived.conf
9+
/bin/sed -i "s/{{INTERFACE}}/${INTERFACE}/g" /etc/keepalived/keepalived.conf
10+
11+
# Make sure we react to these signals by running stop() when we see them - for clean shutdown
12+
# And then exiting
13+
trap "stop; exit 0;" SIGTERM SIGINT
14+
15+
stop()
16+
{
17+
# We're here because we've seen SIGTERM, likely via a Docker stop command or similar
18+
# Let's shutdown cleanly
19+
echo "SIGTERM caught, terminating keepalived process..."
20+
# Record PIDs
21+
pid=$(pidof keepalived)
22+
# Kill them
23+
kill -TERM $pid > /dev/null 2>&1
24+
# Wait till they have been killed
25+
wait $pid
26+
echo "Terminated."
27+
exit 0
28+
}
29+
30+
# Make sure the variables we need to run are populated and (roughly) valid
31+
32+
if ! [[ $VIRTUAL_IP =~ ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-2][0-3])\.(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[1-4])$ ]]; then
33+
echo "The VIRTUAL_IP environment variable is null or not a valid IP address, exiting..."
34+
exit 1
35+
fi
36+
37+
if ! [[ $VIRTUAL_MASK =~ ^([0-9]|[1-2][0-9]|3[0-2])$ ]]; then
38+
echo "The VIRTUAL_MASK environment variable is null or not a valid subnet mask, exiting..."
39+
exit 1
40+
fi
41+
42+
if ! [[ $VRID =~ ^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then
43+
echo "The VRID environment variable is null or not a number between 1 and 255, exiting..."
44+
exit 1
45+
fi
46+
47+
# Possibly some interfaces are named and don't end in a number so beware of this one
48+
if ! [[ $INTERFACE =~ ^.*[0-9]$ ]]; then
49+
echo "The INTERFACE environment variable is null or doesn't end in a number, exiting..."
50+
exit 1
51+
fi
52+
53+
# Make sure to clean up VIP before start (in case of ungraceful shutdown)
54+
if [[ $(ip a s $INTERFACE | grep $VIRTUAL_IP) ]]
55+
then
56+
ip addr del $VIRTUAL_IP/$VIRTUAL_MASK dev $INTERFACE
57+
fi
58+
59+
# This loop runs till until we've started up successfully
60+
while true; do
61+
62+
# Check if Keepalived is running by recording it's PID (if it's not running $pid will be null):
63+
pid=$(pidof keepalived)
64+
65+
# If $pid is null, do this to start or restart Keepalived:
66+
while [ -z "$pid" ]; do
67+
#Obviously optional:
68+
#echo "Starting Confd population of files..."
69+
#/usr/bin/confd -onetime
70+
echo "Displaying resulting /etc/keepalived/keepalived.conf contents..."
71+
cat /etc/keepalived/keepalived.conf
72+
echo "Starting Keepalived in the background..."
73+
/usr/sbin/keepalived --dont-fork --dump-conf --log-console --log-detail --vrrp &
74+
# Check if Keepalived is now running by recording it's PID (if it's not running $pid will be null):
75+
pid=$(pidof keepalived)
76+
77+
# If $pid is null, startup failed; log the fact and sleep for 2s
78+
# We'll then automatically loop through and try again
79+
if [ -z "$pid" ]; then
80+
echo "Startup of Keepalived failed, sleeping for 2s, then retrying..."
81+
sleep 2
82+
fi
83+
84+
done
85+
86+
# Break this outer loop once we've started up successfully
87+
# Otherwise, we'll silently restart and Rancher won't know
88+
break
89+
90+
done
91+
92+
# Wait until the Keepalived processes stop (for some reason)
93+
wait $pid
94+
echo "The Keepalived process is no longer running, exiting..."
95+
# Exit with an error
96+
exit 1

nodejs/Dockerfile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
FROM buildpack-deps:jessie
2+
3+
RUN groupadd --gid 1000 node \
4+
&& useradd --uid 1000 --gid node --shell /bin/bash --create-home node
5+
6+
# gpg keys listed at https://github.com/nodejs/node#release-team
7+
RUN set -ex \
8+
&& for key in \
9+
94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \
10+
FD3A5288F042B6850C66B31F09FE44734EB7990E \
11+
71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \
12+
DD8F2338BAE7501E3DD5AC78C273792F7D83545D \
13+
C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 \
14+
B9AE9905FFD7803F25714661B63B535A4C206CA9 \
15+
56730D5401028683275BD23C23EFEFE93C4CFFFE \
16+
77984A986EBC2AA786BC0F66B01FBB92821C587A \
17+
; do \
18+
gpg --keyserver pgp.mit.edu --recv-keys "$key" || \
19+
gpg --keyserver keyserver.pgp.com --recv-keys "$key" || \
20+
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key" ; \
21+
done
22+
23+
ENV NODE_VERSION 9.4.0
24+
25+
RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
26+
&& case "${dpkgArch##*-}" in \
27+
amd64) ARCH='x64';; \
28+
ppc64el) ARCH='ppc64le';; \
29+
s390x) ARCH='s390x';; \
30+
arm64) ARCH='arm64';; \
31+
armhf) ARCH='armv7l';; \
32+
i386) ARCH='x86';; \
33+
*) echo "unsupported architecture"; exit 1 ;; \
34+
esac \
35+
&& curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
36+
&& curl -SLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
37+
&& gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
38+
&& grep " node-v$NODE_VERSION-linux-$ARCH.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
39+
&& tar -xJf "node-v$NODE_VERSION-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
40+
&& rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt \
41+
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs
42+
43+
ENV YARN_VERSION 1.3.2
44+
45+
RUN set -ex \
46+
&& for key in \
47+
6A010C5166006599AA17F08146C2130DFD2497F5 \
48+
; do \
49+
gpg --keyserver pgp.mit.edu --recv-keys "$key" || \
50+
gpg --keyserver keyserver.pgp.com --recv-keys "$key" || \
51+
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key" ; \
52+
done \
53+
&& curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
54+
&& curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz.asc" \
55+
&& gpg --batch --verify yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz \
56+
&& mkdir -p /opt/yarn \
57+
&& tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/yarn --strip-components=1 \
58+
&& ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn \
59+
&& ln -s /opt/yarn/bin/yarn /usr/local/bin/yarnpkg \
60+
&& rm yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz
61+
62+
63+
WORKDIR /home/node
64+
COPY entrypoint.sh /usr/local/bin/
65+
RUN chmod u+x /usr/local/bin/entrypoint.sh
66+
ENTRYPOINT ["entrypoint.sh"]
67+
68+
CMD [ "node" ]

nodejs/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# about this
2+
3+
I'm trying to run a nodejs project with docker, but the project needs to first npm install the dependencies in the package.json file while the node is running, and the package.json file needs to be provided by the user ,so I'm going to make a generic image and I reference The official dockerfile for node image adds the entrypoint section to automate npm install and npm run start for common
4+
5+
我尝试用docker运行一个nodejs项目,但是在项目中node运行起来之前需要先npm install文件package.json里的依赖,而package.json文件需要用户提供,我打算制作一个通用型镜像,所以我参照了node镜像的官方dockerfile增加了entrypoint部分来自动执行npm install和npm run start从而做到通用
6+
7+
## what did I do
8+
9+
> * I added the following to node's official dockerfile(我在node官方的dockerfile里增加了下面内容)
10+
```
11+
WORKDIR /home/node
12+
COPY entrypoint.sh /usr/local/bin/
13+
ENTRYPOINT ["entrypoint.sh"]
14+
```
15+
> * and I wrote bash script as entrypoint and the docker-compose.yml
16+
17+
## how to start and use
18+
19+
-------------------------------------------------------------------------
20+
**whatever,the app folder just for reference only,you should use yours**.
21+
-------------------------------------------------------------------------
22+
You can use either part of it or all the part (你可以使用其中的一部分,也可以整个部分)
23+
if you use the part of this(just only dockerfile):
24+
>1. only download the node folder
25+
>2. use the docker to build it,After building you will get a image, such as nodejs
26+
>3. Put your node project into a folder, such as app
27+
>4. cd the app directory,you must mount the all file into the /home/node/ when you run the docker command to run a container.for example:
28+
>
29+
```
30+
docker run -d -v $PWD:/home/node/ nodejs npm run start
31+
```
32+
33+
if you use the docker-compose to run a node project(You must use docker-compose like me):
34+
>1. change the docker-compose.yml file and the entrypoint.sh if you want to change it
35+
>2. run the command :
36+
```
37+
docker-compose up -d
38+
```
39+
>3. just wait a few secends,enjoy it

nodejs/entrypoint.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
set -e
3+
4+
function package_check(){
5+
[ ! -f '/home/node/package.json' ] \
6+
&& { ehco 'package.json not exist!';exit 6; }
7+
:
8+
}
9+
function npm_base(){
10+
[[ "$(npm list nodemon)" =~ 'empty' ]] && npm install nodemon -g
11+
[[ "$(npm list request)" =~ 'empty' ]] && npm install request
12+
:
13+
}
14+
function node_base_run(){
15+
package_check
16+
npm install
17+
npm_base
18+
exec "$@"
19+
}
20+
21+
[ "$1" = 'node' -a "$#" -gt 1 ] && node_base_run
22+
# edit your's run style
23+
[ "$*" = 'npm run start' ] && node_base_run
24+
#----------------------
25+
26+
exec "$@"

uwsgi/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3
2+
WORKDIR /code
3+
COPY . .
4+
RUN pip install -r requirements.txt \
5+
&& pip install uwsgi
6+
EXPOSE 8000 8090
7+
CMD ["uwsgi","uwsgi/uwsgi.ini"]
8+

uwsgi/mysite/app.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError:
10+
# The above import may fail for some other reason. Ensure that the
11+
# issue is really that Django is missing to avoid masking other
12+
# exceptions on Python 2.
13+
try:
14+
import django
15+
except ImportError:
16+
raise ImportError(
17+
"Couldn't import Django. Are you sure it's installed and "
18+
"available on your PYTHONPATH environment variable? Did you "
19+
"forget to activate a virtual environment?"
20+
)
21+
raise
22+
execute_from_command_line(sys.argv)

0 commit comments

Comments
 (0)