本文是《kubernetes下的Nginx加Tomcat三部曲》的第二章,在《kubernetes下的Nginx加Tomcat三部曲之一:极速体验》一文我们快速部署了Nginx和Tomcat,达到以下效果:
本文我会详细说明在kubernetes部署上述网络服务时,需要做哪些具体的工作;
需要以下操做才能在kubernetes上部署Nginx加Tomcat的服务:
本次体验所需的deployment和service资源是通过脚本创建的,这个脚本可以通过以下两种方式中的任意一种下载:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
下到的k8stomcatcluster20180201.tar是个压缩包,复制到可以执行kubectl命令的ubuntu电脑上,然后解压开,是个名为k8stomcatcluster的文件夹;
GitHub下载,地址和链接信息如下表所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
接下来我们开始实战开发吧;
本次实战开发环境的具体信息如下:
web工程用来提供http服务,返回当前机器的IP地址给浏览器,完整源码请参照前面下载的k8stomcatdemo工程,这里我们还是重新创建一次;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.bolingcavalrygroupId>
<artifactId>k8stomcatdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>k8stomcatdemoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>bolingcavalry/${project.artifactId}imageName>
<imageTags>
<imageTag>${project.version}imageTag>
imageTags>
<baseImage>java:8u111-jdkbaseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
plugins>
build>
project>
可以看到这是个普通的springboot的web工程,唯一不同的是多了一个maven插件:docker-maven-plugin,使用该插件我们可以将maven工程制作成docker镜像;
2. 整个工程只有一个Controller,开通一个http接口,将当前服务器IP地址返回,源码如下:
@RestController
public class ServerInfo {
@RequestMapping(value = "/getserverinfo", method = RequestMethod.GET)
public String getUserInfoWithRequestParam(){
return String.format("server : %s, time : %s", getIPAddr(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
/**
* 获取本机IP地址
* @return
*/
private static String getIPAddr(){
String hostAddress = null;
try{
InetAddress address = InetAddress.getLocalHost();
hostAddress = address.getHostAddress();
}catch (UnknownHostException e){
e.printStackTrace();
}
return hostAddress;
}
}
以上就是工程的关键源码;
3. 在pom.xml所在目录执行mvn clean package -DskipTests docker:build,会编译构建工程,并且在本地制作好镜像文件,如下:
root@maven:/usr/local/work/github/blog_demos# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bolingcavalry/k8stomcatdemo latest 1d41d9980a0b 43 hours ago 658 MB
bolingcavalry/k8stomcatdemo 0.0.1-SNAPSHOT c91bc368a729 46 hours ago 658 MB
现在的镜像只存在于开发和构建web工程的电脑上,为了让kubernetes的node机器能用上这个镜像,可以用以下几种方式实现:
用yaml文件将详情配置好,再用kubectl命令执行这个配置就能创建pod,这个web应用镜像的配置文件名为tomcat.yaml,内容如下:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tomcathost
spec:
replicas: 3
template:
metadata:
labels:
name: tomcathost
spec:
containers:
- name: tomcathost
image: bolingcavalry/k8stomcatdemo:0.0.1-SNAPSHOT
tty: true
ports:
- containerPort: 8080
将上述脚本的几个关键点列举一下:
创建了tomcat的pod之后,为了能在kubernetes环境中给其他service使用,需要将这些pod包装为service,这里是通过tomcat-svc.yaml文件来配置的,内容如下:
apiVersion: v1
kind: Service
metadata:
name: tomcathost
spec:
type: ClusterIP
ports:
- port: 8080
selector:
name: tomcathost
将上述脚本的几个关键点列举一下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
upstream tomcat_client {
server tomcathost:8080;
}
server {
server_name "";
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
location / {
proxy_pass http://tomcat_client;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
以上配置中,新增的upstream对应的IP地址是tomcathost,这是tomcat的service名称,在Nginx运行的时候,通过tomcathost就能访问到tomcat的Pod;
# First docker file from bolingcavalry
# VERSION 0.0.1
# Author: bolingcavalry
#基础镜像
FROM nginx:stable
#作者
MAINTAINER BolingCavalry <[email protected]>
#定义工作目录
ENV WORK_PATH /etc/nginx
#定义conf文件名
ENV CONF_FILE_NAME nginx.conf
#删除原有配置文件
RUN rm $WORK_PATH/$CONF_FILE_NAME
#复制新的配置文件
COPY ./$CONF_FILE_NAME $WORK_PATH/
#给shell文件赋读权限
RUN chmod a+r $WORK_PATH/$CONF_FILE_NAME
root@maven:/usr/local/work/nginx# docker build -t bolingcavalry/nginx-with-tomcat-host:0.0.1 .
Sending build context to Docker daemon 14.51 MB
Step 1/7 : FROM nginx:stable
---> dfe062ee1dc8
Step 2/7 : MAINTAINER BolingCavalry <[email protected]>
---> Using cache
---> 93f4bf154c55
Step 3/7 : ENV WORK_PATH /etc/nginx
---> Using cache
---> d0158757fc9c
Step 4/7 : ENV CONF_FILE_NAME nginx.conf
---> Using cache
---> 7a18a8b417d6
Step 5/7 : RUN rm $WORK_PATH/$CONF_FILE_NAME
---> Using cache
---> f6f27d25539d
Step 6/7 : COPY ./$CONF_FILE_NAME $WORK_PATH/
---> Using cache
---> 33075a2b0379
Step 7/7 : RUN chmod a+r $WORK_PATH/$CONF_FILE_NAME
---> Using cache
---> 58ce530e160b
Successfully built 58ce530e160b
这一步和之前的web工程的镜像放到kubernetes一样,有多种方式,我用的还是通过docker push推送到hub.docker.com网站,再在kubernetes上pull下来;
用yaml文件将详情配置好,再用kubectl命令执行这个配置就能创建pod,这个web应用镜像的配置文件名为nginx.yaml,内容如下:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: ng
spec:
replicas: 1
template:
metadata:
labels:
name: ng
spec:
containers:
- name: ng
image: bolingcavalry/nginx-with-tomcat-host:0.0.1
ports:
- containerPort: 80
以上配置中有几点要注意:
通过service定义的yaml文件nginx-svc.yam,将前面创建的nginx的pod包装为service:
apiVersion: v1
kind: Service
metadata:
name: ng
spec:
type: NodePort
ports:
- port: 80
nodePort: 30006
selector:
name: ng
以上配置中有几点要注意:
接下来我们创建一个shell脚本start_all.sh,将上述的tomcat和nginx的pod以及service全部创建和启动:
kubectl create -f tomcat.yaml
kubectl create -f tomcat-svc.yaml
kubectl create -f nginx.yaml
kubectl create -f nginx-svc.yaml
echo ""
echo "nginx and tomcat running now"
如上所示,通过kubectl create -f加文件名,就能创建好yaml文件中定义的pod和service;
创建一个shell脚本stop_all.sh,能够将上述的tomcat和nginx的pod以及service全部清除掉:
kubectl delete service tomcathost
kubectl delete deployment tomcathost
kubectl delete service ng
kubectl delete deployment ng
echo "nginx and tomcat stop now"
如上所示,其实就是通过kubectl delete命令将指定的pod和service资源删除;
以上就是在kubernetes搭建整个Nginx加Tomcat环境的所有资源,您就可以用这些像《kubernetes下的Nginx加Tomcat三部曲之一:极速体验》文中那样去搭建和体验kubernetes下的Nginx加Tomcat;
下一章,我们会在此环境的基础上实战Tomcat服务的扩容,并修改web工程的代码,再创建新的镜像,并且将kubernetes环境下在线升级新的web工程服务;