|
1 | | -# 12.3 应用部署 |
2 | | -程序开发完毕之后,我们现在要部署Web应用程序了,但是我们如何来部署这些应用程序呢?因为Go程序编译之后是一个可执行文件,编写过C程序的读者一定知道使用demon就可以完美的实现程序后台运行,但是目前Go还无法完美的实现demon,因此,针对Go的应用程序部署,我们可以利用第三方工具来管理,第三方的工具有很多,例如Supervisord、upstart、daemontools等,这小节我介绍目前自己系统中采用的工具Supervisord。 |
3 | | -## deamon |
4 | | -目前Go程序如果要实现deamon还不行,详细的见这个Go语言的bug:http://code.google.com/p/go/issues/detail?id=227,大概的意思说很难从现有的使用的线程中fork一个出来,因为没有一种简单的方法来确保所有已经使用的线程的状态一致性问题。 |
5 | | - |
6 | | -但是我们可以看到很多网上的一些实现deamon的方法,例如下面两种方式: |
7 | | - |
8 | | -- MarGo的一个实现思路,使用Commond来执行自身的应用,如果真想实现,那么推荐这种方案 |
9 | | - |
10 | | - d := flag.Bool("d", false, "Whether or not to launch in the background(like a daemon)") |
11 | | - if *d { |
12 | | - cmd := exec.Command(os.Args[0], |
13 | | - "-close-fds", |
14 | | - "-addr", *addr, |
15 | | - "-call", *call, |
16 | | - ) |
17 | | - serr, err := cmd.StderrPipe() |
18 | | - if err != nil { |
19 | | - log.Fatalln(err) |
20 | | - } |
21 | | - err = cmd.Start() |
22 | | - if err != nil { |
23 | | - log.Fatalln(err) |
24 | | - } |
25 | | - s, err := ioutil.ReadAll(serr) |
26 | | - s = bytes.TrimSpace(s) |
27 | | - if bytes.HasPrefix(s, []byte("addr: ")) { |
28 | | - fmt.Println(string(s)) |
29 | | - cmd.Process.Release() |
30 | | - } else { |
31 | | - log.Printf("unexpected response from MarGo: `%s` error: `%v`\n", s, err) |
32 | | - cmd.Process.Kill() |
33 | | - } |
34 | | - } |
35 | | - |
36 | | -- 另一种是利用syscall的方案,但是这个方案并不完善: |
37 | | - |
38 | | - package main |
39 | | - |
40 | | - import ( |
41 | | - "log" |
42 | | - "os" |
43 | | - "syscall" |
44 | | - ) |
45 | | - |
46 | | - func daemon(nochdir, noclose int) int { |
47 | | - var ret, ret2 uintptr |
48 | | - var err uintptr |
49 | | - |
50 | | - darwin := syscall.OS == "darwin" |
51 | | - |
52 | | - // already a daemon |
53 | | - if syscall.Getppid() == 1 { |
54 | | - return 0 |
55 | | - } |
56 | | - |
57 | | - // fork off the parent process |
58 | | - ret, ret2, err = syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0) |
59 | | - if err != 0 { |
60 | | - return -1 |
61 | | - } |
62 | | - |
63 | | - // failure |
64 | | - if ret2 < 0 { |
65 | | - os.Exit(-1) |
66 | | - } |
67 | | - |
68 | | - // handle exception for darwin |
69 | | - if darwin && ret2 == 1 { |
70 | | - ret = 0 |
71 | | - } |
72 | | - |
73 | | - // if we got a good PID, then we call exit the parent process. |
74 | | - if ret > 0 { |
75 | | - os.Exit(0) |
76 | | - } |
77 | | - |
78 | | - /* Change the file mode mask */ |
79 | | - _ = syscall.Umask(0) |
80 | | - |
81 | | - // create a new SID for the child process |
82 | | - s_ret, s_errno := syscall.Setsid() |
83 | | - if s_errno != 0 { |
84 | | - log.Printf("Error: syscall.Setsid errno: %d", s_errno) |
85 | | - } |
86 | | - if s_ret < 0 { |
87 | | - return -1 |
88 | | - } |
89 | | - |
90 | | - if nochdir == 0 { |
91 | | - os.Chdir("/") |
92 | | - } |
93 | | - |
94 | | - if noclose == 0 { |
95 | | - f, e := os.OpenFile("/dev/null", os.O_RDWR, 0) |
96 | | - if e == nil { |
97 | | - fd := f.Fd() |
98 | | - syscall.Dup2(fd, os.Stdin.Fd()) |
99 | | - syscall.Dup2(fd, os.Stdout.Fd()) |
100 | | - syscall.Dup2(fd, os.Stderr.Fd()) |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - return 0 |
105 | | - } |
106 | | - |
107 | | -上面提出了两种实现Go的deamon方案,但是我还是不推荐大家这样去实现,因为官方还没有正式的宣布支持deamon,当然第一种方案目前来看是比较可行的,而且目前开源库skynet也在采用这个方案做deamon。 |
108 | | - |
109 | | -## Supervisord |
110 | | -上面已经介绍了Go目前是有两种方案来实现他的deamon,但是官方本身还不支持这一块,所以还是建议大家采用第三方成熟工具来管理我们的应用程序,这里我给大家介绍一款目前使用比较广泛的进程管理软件:Supervisord。Supervisord是用Python实现的一款非常实用的进程管理工具。supervisord会帮你把管理的应用程序转成daemon程序,而且可以方便的通过命令开启、关闭、重启等操作,而且它管理的进程一旦奔溃会自动再次重新开启,这样就可以保证程序中断的情况下有自我修复功能。 |
111 | | - |
112 | | ->我前面在应用中踩过一个坑,就是因为所有的应用程序都是由Supervisord父进程生出来的,那么当你修改了操作系统的文件描述符之后,别忘记重启Supervisord,光重启下面的应用程序没用。当初我就是系统安装好之后就先装了Supervisord,然后开始部署程序,修改文件描述符,重启程序,以为文件描述符已经是100000了,其实Supervisord这个时候还是默认的1024个,导致他管理的进程所有的描述符也是1024.开放之后压力一上来里面狂暴文件描述符用光了,查了很久才找到这个坑。 |
113 | | -
|
114 | | -### Supervisord安装 |
115 | | -Supervisord可以通过`sudo easy_install supervisor`安装,当然也可以通过Supervisord官网下载后`setup.py install`安装。 |
116 | | - |
117 | | -- 使用easy_install必须安装setuptools |
118 | | - |
119 | | - 打开http://pypi.python.org/pypi/setuptools#files,根据你系统的python的版本下载相应的文件,然后执行`sh setuptoolsxxxx.egg`,这样就可以使用easy_install命令来安装Supervisord。 |
120 | | - |
121 | | -### Supervisord配置 |
122 | | -Supervisord默认的配置文件路径为/etc/supervisord.conf,通过文本编辑器修改这个文件,下面是一个示例的配置文件: |
123 | | - |
124 | | - ;/etc/supervisord.conf |
125 | | - [unix_http_server] |
126 | | - file = /var/run/supervisor.sock |
127 | | - chmod = 0777 |
128 | | - chown= root:root |
129 | | - |
130 | | - [inet_http_server] |
131 | | - # Web管理界面设定 |
132 | | - port=9001 |
133 | | - username = admin |
134 | | - password = yourpassword |
135 | | - |
136 | | - [supervisorctl] |
137 | | - ; 必须和'unix_http_server'里面的设定匹配 |
138 | | - serverurl = unix:///var/run/supervisord.sock |
139 | | - |
140 | | - [supervisord] |
141 | | - logfile=/var/log/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log) |
142 | | - logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) |
143 | | - logfile_backups=10 ; (num of main logfile rotation backups;default 10) |
144 | | - loglevel=info ; (log level;default info; others: debug,warn,trace) |
145 | | - pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) |
146 | | - nodaemon=true ; (start in foreground if true;default false) |
147 | | - minfds=1024 ; (min. avail startup file descriptors;default 1024) |
148 | | - minprocs=200 ; (min. avail process descriptors;default 200) |
149 | | - user=root ; (default is current user, required if root) |
150 | | - childlogdir=/var/log/supervisord/ ; ('AUTO' child log dir, default $TEMP) |
151 | | - |
152 | | - [rpcinterface:supervisor] |
153 | | - supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface |
154 | | - |
155 | | - ; 管理的单个进程的配置,可以添加多个program |
156 | | - [program:blogdemon] |
157 | | - command=/data/blog/blogdemon |
158 | | - autostart = true |
159 | | - startsecs = 5 |
160 | | - user = root |
161 | | - redirect_stderr = true |
162 | | - stdout_logfile = /var/log/supervisord/blogdemon.log |
163 | | - |
164 | | -### Supervisord管理 |
165 | | -Supervisord安装完成后有两个可用的命令行supervisor和supervisorctl,命令使用解释如下: |
166 | | - |
167 | | -- supervisord,初始启动Supervisord,启动、管理配置中设置的进程。 |
168 | | -- supervisorctl stop programxxx,停止某一个进程(programxxx),programxxx为[program:blogdemon]里配置的值,这个示例就是blogdemon。 |
169 | | -- supervisorctl start programxxx,启动某个进程 |
170 | | -- supervisorctl restart programxxx,重启某个进程 |
171 | | -- supervisorctl stop all,停止全部进程,注:start、restart、stop都不会载入最新的配置文件。 |
172 | | -- supervisorctl reload,载入最新的配置文件,并按新的配置启动、管理所有进程。 |
173 | | - |
174 | | -## 小结 |
175 | | -这小节我们介绍了如何实现Go的deamon,但是我们知道由于目前Go的deamon实现不是很完美,我们需要依靠第三方工具来实现应用程序的deamon管理,这里介绍了一个python写的进程管理工具Supervisord,通过Supervisord可以很方便的把我们的Go应用程序管理起来。 |
176 | | - |
177 | | - |
178 | | -## links |
179 | | - * [目录](<preface.md>) |
180 | | - * 上一章: [网站错误处理](<12.2.md>) |
| 1 | +# 12.3 应用部署 |
| 2 | +程序开发完毕之后,我们现在要部署Web应用程序了,但是我们如何来部署这些应用程序呢?因为Go程序编译之后是一个可执行文件,编写过C程序的读者一定知道使用daemon就可以完美的实现程序后台运行,但是目前Go还无法完美的实现daemon,因此,针对Go的应用程序部署,我们可以利用第三方工具来管理,第三方的工具有很多,例如Supervisord、upstart、daemontools等,这小节我介绍目前自己系统中采用的工具Supervisord。 |
| 3 | +## deamon |
| 4 | +目前Go程序如果要实现daemon还不行,详细的见这个Go语言的bug:http://code.google.com/p/go/issues/detail?id=227,大概的意思说很难从现有的使用的线程中fork一个出来,因为没有一种简单的方法来确保所有已经使用的线程的状态一致性问题。 |
| 5 | + |
| 6 | +但是我们可以看到很多网上的一些实现deamon的方法,例如下面两种方式: |
| 7 | + |
| 8 | +- MarGo的一个实现思路,使用Commond来执行自身的应用,如果真想实现,那么推荐这种方案 |
| 9 | + |
| 10 | + d := flag.Bool("d", false, "Whether or not to launch in the background(like a daemon)") |
| 11 | + if *d { |
| 12 | + cmd := exec.Command(os.Args[0], |
| 13 | + "-close-fds", |
| 14 | + "-addr", *addr, |
| 15 | + "-call", *call, |
| 16 | + ) |
| 17 | + serr, err := cmd.StderrPipe() |
| 18 | + if err != nil { |
| 19 | + log.Fatalln(err) |
| 20 | + } |
| 21 | + err = cmd.Start() |
| 22 | + if err != nil { |
| 23 | + log.Fatalln(err) |
| 24 | + } |
| 25 | + s, err := ioutil.ReadAll(serr) |
| 26 | + s = bytes.TrimSpace(s) |
| 27 | + if bytes.HasPrefix(s, []byte("addr: ")) { |
| 28 | + fmt.Println(string(s)) |
| 29 | + cmd.Process.Release() |
| 30 | + } else { |
| 31 | + log.Printf("unexpected response from MarGo: `%s` error: `%v`\n", s, err) |
| 32 | + cmd.Process.Kill() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | +- 另一种是利用syscall的方案,但是这个方案并不完善: |
| 37 | + |
| 38 | + package main |
| 39 | + |
| 40 | + import ( |
| 41 | + "log" |
| 42 | + "os" |
| 43 | + "syscall" |
| 44 | + ) |
| 45 | + |
| 46 | + func daemon(nochdir, noclose int) int { |
| 47 | + var ret, ret2 uintptr |
| 48 | + var err uintptr |
| 49 | + |
| 50 | + darwin := syscall.OS == "darwin" |
| 51 | + |
| 52 | + // already a daemon |
| 53 | + if syscall.Getppid() == 1 { |
| 54 | + return 0 |
| 55 | + } |
| 56 | + |
| 57 | + // fork off the parent process |
| 58 | + ret, ret2, err = syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0) |
| 59 | + if err != 0 { |
| 60 | + return -1 |
| 61 | + } |
| 62 | + |
| 63 | + // failure |
| 64 | + if ret2 < 0 { |
| 65 | + os.Exit(-1) |
| 66 | + } |
| 67 | + |
| 68 | + // handle exception for darwin |
| 69 | + if darwin && ret2 == 1 { |
| 70 | + ret = 0 |
| 71 | + } |
| 72 | + |
| 73 | + // if we got a good PID, then we call exit the parent process. |
| 74 | + if ret > 0 { |
| 75 | + os.Exit(0) |
| 76 | + } |
| 77 | + |
| 78 | + /* Change the file mode mask */ |
| 79 | + _ = syscall.Umask(0) |
| 80 | + |
| 81 | + // create a new SID for the child process |
| 82 | + s_ret, s_errno := syscall.Setsid() |
| 83 | + if s_errno != 0 { |
| 84 | + log.Printf("Error: syscall.Setsid errno: %d", s_errno) |
| 85 | + } |
| 86 | + if s_ret < 0 { |
| 87 | + return -1 |
| 88 | + } |
| 89 | + |
| 90 | + if nochdir == 0 { |
| 91 | + os.Chdir("/") |
| 92 | + } |
| 93 | + |
| 94 | + if noclose == 0 { |
| 95 | + f, e := os.OpenFile("/dev/null", os.O_RDWR, 0) |
| 96 | + if e == nil { |
| 97 | + fd := f.Fd() |
| 98 | + syscall.Dup2(fd, os.Stdin.Fd()) |
| 99 | + syscall.Dup2(fd, os.Stdout.Fd()) |
| 100 | + syscall.Dup2(fd, os.Stderr.Fd()) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return 0 |
| 105 | + } |
| 106 | + |
| 107 | +上面提出了两种实现Go的daemon方案,但是我还是不推荐大家这样去实现,因为官方还没有正式的宣布支持daemon,当然第一种方案目前来看是比较可行的,而且目前开源库skynet也在采用这个方案做daemon。 |
| 108 | + |
| 109 | +## Supervisord |
| 110 | +上面已经介绍了Go目前是有两种方案来实现他的daemon,但是官方本身还不支持这一块,所以还是建议大家采用第三方成熟工具来管理我们的应用程序,这里我给大家介绍一款目前使用比较广泛的进程管理软件:Supervisord。Supervisord是用Python实现的一款非常实用的进程管理工具。supervisord会帮你把管理的应用程序转成daemon程序,而且可以方便的通过命令开启、关闭、重启等操作,而且它管理的进程一旦奔溃会自动再次重新开启,这样就可以保证程序中断的情况下有自我修复功能。 |
| 111 | + |
| 112 | +>我前面在应用中踩过一个坑,就是因为所有的应用程序都是由Supervisord父进程生出来的,那么当你修改了操作系统的文件描述符之后,别忘记重启Supervisord,光重启下面的应用程序没用。当初我就是系统安装好之后就先装了Supervisord,然后开始部署程序,修改文件描述符,重启程序,以为文件描述符已经是100000了,其实Supervisord这个时候还是默认的1024个,导致他管理的进程所有的描述符也是1024.开放之后压力一上来系统就开发报文件描述符用光了,查了很久才找到这个坑。 |
| 113 | +
|
| 114 | +### Supervisord安装 |
| 115 | +Supervisord可以通过`sudo easy_install supervisor`安装,当然也可以通过Supervisord官网下载后`setup.py install`安装。 |
| 116 | + |
| 117 | +- 使用easy_install必须安装setuptools |
| 118 | + |
| 119 | + 打开http://pypi.python.org/pypi/setuptools#files,根据你系统的python的版本下载相应的文件,然后执行`sh setuptoolsxxxx.egg`,这样就可以使用easy_install命令来安装Supervisord。 |
| 120 | + |
| 121 | +### Supervisord配置 |
| 122 | +Supervisord默认的配置文件路径为/etc/supervisord.conf,通过文本编辑器修改这个文件,下面是一个示例的配置文件: |
| 123 | + |
| 124 | + ;/etc/supervisord.conf |
| 125 | + [unix_http_server] |
| 126 | + file = /var/run/supervisor.sock |
| 127 | + chmod = 0777 |
| 128 | + chown= root:root |
| 129 | + |
| 130 | + [inet_http_server] |
| 131 | + # Web管理界面设定 |
| 132 | + port=9001 |
| 133 | + username = admin |
| 134 | + password = yourpassword |
| 135 | + |
| 136 | + [supervisorctl] |
| 137 | + ; 必须和'unix_http_server'里面的设定匹配 |
| 138 | + serverurl = unix:///var/run/supervisord.sock |
| 139 | + |
| 140 | + [supervisord] |
| 141 | + logfile=/var/log/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log) |
| 142 | + logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) |
| 143 | + logfile_backups=10 ; (num of main logfile rotation backups;default 10) |
| 144 | + loglevel=info ; (log level;default info; others: debug,warn,trace) |
| 145 | + pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) |
| 146 | + nodaemon=true ; (start in foreground if true;default false) |
| 147 | + minfds=1024 ; (min. avail startup file descriptors;default 1024) |
| 148 | + minprocs=200 ; (min. avail process descriptors;default 200) |
| 149 | + user=root ; (default is current user, required if root) |
| 150 | + childlogdir=/var/log/supervisord/ ; ('AUTO' child log dir, default $TEMP) |
| 151 | + |
| 152 | + [rpcinterface:supervisor] |
| 153 | + supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface |
| 154 | + |
| 155 | + ; 管理的单个进程的配置,可以添加多个program |
| 156 | + [program:blogdemon] |
| 157 | + command=/data/blog/blogdemon |
| 158 | + autostart = true |
| 159 | + startsecs = 5 |
| 160 | + user = root |
| 161 | + redirect_stderr = true |
| 162 | + stdout_logfile = /var/log/supervisord/blogdemon.log |
| 163 | + |
| 164 | +### Supervisord管理 |
| 165 | +Supervisord安装完成后有两个可用的命令行supervisor和supervisorctl,命令使用解释如下: |
| 166 | + |
| 167 | +- supervisord,初始启动Supervisord,启动、管理配置中设置的进程。 |
| 168 | +- supervisorctl stop programxxx,停止某一个进程(programxxx),programxxx为[program:blogdemon]里配置的值,这个示例就是blogdemon。 |
| 169 | +- supervisorctl start programxxx,启动某个进程 |
| 170 | +- supervisorctl restart programxxx,重启某个进程 |
| 171 | +- supervisorctl stop all,停止全部进程,注:start、restart、stop都不会载入最新的配置文件。 |
| 172 | +- supervisorctl reload,载入最新的配置文件,并按新的配置启动、管理所有进程。 |
| 173 | + |
| 174 | +## 小结 |
| 175 | +这小节我们介绍了如何实现Go的daemon,但是我们知道由于目前Go的daemon实现不是很完美,我们需要依靠第三方工具来实现应用程序的daemon管理,这里介绍了一个python写的进程管理工具Supervisord,通过Supervisord可以很方便的把我们的Go应用程序管理起来。 |
| 176 | + |
| 177 | + |
| 178 | +## links |
| 179 | + * [目录](<preface.md>) |
| 180 | + * 上一章: [网站错误处理](<12.2.md>) |
181 | 181 | * 下一节: [备份和恢复](<12.4.md>) |
0 commit comments