kubernete部署发布web应用
1 制作web容器镜像 1.1 准备好项目包cc,修改里面的配置文件mongodb.properties、quartz.properties mongodb.properties:mongo.repset=mongo_url1mongo.user=mongo_user1mongo.password=mongo_password1mongo.database=mongo_db1m...
1 制作web容器镜像
1.1 准备好项目包cc,修改里面的配置文件mongodb.properties、quartz.properties
mongodb.properties:
mongo.repset=mongo_url1
mongo.user=mongo_user1
mongo.password=mongo_password1
mongo.database=mongo_db1
mongo.connectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=5
mongo.connectTimeout=10000
mongo.maxWaitTime=15000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=15000
mongo.slaveOk=true
quartz.properties:
# Use the MongoDB store
org.quartz.jobStore.class=com.novemberain.quartz.mongodb.MongoDBJobStore
# MongoDB URI (optional if 'org.quartz.jobStore.addresses' is set)
# comma separated list of mongodb hosts/replica set seeds (optional if 'org.quartz.jobStore.mongoUri' is set)
org.quartz.jobStore.addresses=mongo_url1
org.quartz.jobStore.dbName=mongo_db1
org.quartz.jobStore.username=mongo_user1
org.quartz.jobStore.password=mongo_password1
# Will be used to create collections like mycol_jobs, mycol_triggers, mycol_calendars, mycol_locks
org.quartz.jobStore.collectionPrefix=quartz
# thread count setting is ignored by the MongoDB store but Quartz requries it
org.quartz.threadPool.threadCount=1
# Time in millis after which a trigger can be considered as expired.
# Defaults to 10 minutes:
org.quartz.scheduler.triggerTimeoutMillis=120000
# Time in millis after which a job can be considered as expired.
# Defaults to 10 minutes:
org.quartz.scheduler.jobTimeoutMillis=120000
org.quartz.jobStore.misfireThreshold =5000
1.2编写Dockerfile文件,需要联网下载的资源可以提前下载好如:jdk-7u79-linux-x64.gz、apache-tomcat-7.0.57.tar.gz
Dockerfile:
# Pull base image
FROM ubuntu:13.10
MAINTAINER chen "chen@cdvcloud.com"
# update source
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
RUN apt-get update
# Install curl
RUN apt-get -y install curl
# Install JDK 7
#RUN cd /tmp && curl -L 'http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz' -H 'Cookie: oraclelicense=accept-securebackup-cookie; gpw_e24=Dockerfile' | tar -xz
RUN mkdir -p /usr/lib/jvm
#RUN mv /tmp/jdk1.7.0_65/ /usr/lib/jvm/java-7-oracle/
ADD jdk-7u79-linux-x64.gz /tmp/
RUN mv /tmp/jdk1.7.0_79/ /usr/lib/jvm/java-7-oracle/
# Set Oracle JDK 7 as default Java
RUN update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/bin/java 300
RUN update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 300
ENV JAVA_HOME /usr/lib/jvm/java-7-oracle/
# Install tomcat7
# RUN cd /tmp && curl -L 'http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz' | tar -xz
# RUN mv /tmp/apache-tomcat-7.0.8/ /opt/tomcat7/
ADD apache-tomcat-7.0.57.tar.gz /tmp/
RUN mv /tmp/apache-tomcat-7.0.57/ /opt/tomcat7/
ENV CATALINA_HOME /opt/tomcat7
ENV PATH $PATH:$CATALINA_HOME/bin
ADD tomcat7.sh /etc/init.d/tomcat7
RUN chmod 755 /etc/init.d/tomcat7
# Expose ports.
EXPOSE 8080
# add app
ENV MONGO_URL localhost:27017
ENV MONGO_USER onair
ENV MONGO_PASSWORD onair123
ENV MONGO_DB cc
RUN mkdir -p /opt/tomcat7/webapps/cc/
ADD cc /opt/tomcat7/webapps/cc/
#RUN jar -xvf /opt/tomcat7/webapps/cc/cc.war
#ADD configapp.sh /etc/init.d/configapp
#RUN chmod 755 /etc/init.d/configapp
ADD configapp.sh /configapp.sh
RUN chmod 755 /configapp.sh
#RUN ln -s usr/local/bin/configapp.sh /configapp.sh # backwards compat
# Define default command.
ENTRYPOINT /configapp.sh && service tomcat7 start && tail -f /opt/tomcat7/logs/catalina.out
1.3 编写修改配置的脚本文件configapp.sh 和tomcat启动脚本tomcat7.sh
configapp.sh:
#!/bin/bash
sed -i "s/mongo_url1/$MONGO_URL/" /opt/tomcat7/webapps/cc/WEB-INF/classes/mongodb.properties
sed -i "s/mongo_user1/$MONGO_USER/" /opt/tomcat7/webapps/cc/WEB-INF/classes/mongodb.properties
sed -i "s/mongo_password1/$MONGO_PASSWORD/" /opt/tomcat7/webapps/cc/WEB-INF/classes/mongodb.properties
sed -i "s/mongo_db1/$MONGO_DB/" /opt/tomcat7/webapps/cc/WEB-INF/classes/mongodb.properties
sed -i "s/mongo_url1/$MONGO_URL/" /opt/tomcat7/webapps/cc/WEB-INF/classes/quartz.properties
sed -i "s/mongo_user1/$MONGO_USER/" /opt/tomcat7/webapps/cc/WEB-INF/classes/quartz.properties
sed -i "s/mongo_password1/$MONGO_PASSWORD/" /opt/tomcat7/webapps/cc/WEB-INF/classes/quartz.properties
sed -i "s/mongo_db1/$MONGO_DB/" /opt/tomcat7/webapps/cc/WEB-INF/classes/quartz.properties
tomcat7.sh:
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/
export JAVA_OPTS="-Dfile.encoding=UTF-8 \
-Dcatalina.logbase=/var/log/tomcatProd \
-Dnet.sf.ehcache.skipUpdateCheck=true \
-XX:+UseConcMarkSweepGC \
-XX:+CMSClassUnloadingEnabled \
-XX:+UseParNewGC \
-XX:MaxPermSize=128m \
-Xms512m -Xmx512m"
export PATH=$JAVA_HOME/bin:$PATH
TOMCAT_HOME=/opt/tomcat7
SHUTDOWN_WAIT=20
export CATALINA_OPTS="-Xmx512m"
export CATALINA_BASE=/opt/tomcat7
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep $CATALINA_BASE | grep -v grep | awk '{ print $2 }'`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat"
#ulimit -n 100000
umask 007
$TOMCAT_HOME/bin/startup.sh
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stoping Tomcat"
if [ "$USER" == "$TOMCAT_USER" ]
then
$TOMCAT_HOME/bin/shutdown.sh
else
/bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/shutdown.sh
fi
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\nwaiting for processes to exit";
sleep 1
let count=$count+1;
done
if [ $count -gt $kwait ]; then
echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0
1.4 把相关的包和文件放到同一目录下,如下图:
1.5 在Dockerfile目录下执行docker build -t chen/cc:v2 . 命令构造镜像如下图:
查看生成的镜像
1.6 验证下拉起的镜像是否可用
可以直接通过docker run拉起一个容器
[root@k8s-node-2 ~]# docker run -d --name cc2.0 --env MONGO_URL=192.168.0.36:27017 --env MONGO_USER=cc --env MONGO_PASSWORD=cc123 -p 8090:8080 chen/cc:v2
083728e009f9275b761f593569ebf945eb9eac959ae40a37cc37056183519e7f
[root@k8s-node-2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
083728e009f9 chen/cc:v2 "/bin/sh -c '/conf..." 4 seconds ago Up 2 seconds 0.0.0.0:8090->8080/tcp cc2.0
f2b21941e110 f6f363e6e98e "/usr/local/bin/ku..." 16 hours ago Up 16 hours k8s_kube-proxy_kube-proxy-bhr8p_kube-system_8f06fa62-4481-11e8-b213-000c298e4460_2
057a0f95d421 gcr.io/google_containers/pause-amd64:3.0 "/pause" 16 hours ago Up 16 hours k8s_POD_kube-proxy-bhr8p_kube-system_8f06fa62-4481-11e8-b213-000c298e4460_2
348544aa10f1 2b736d06ca4c "/opt/bin/flanneld..." 16 hours ago Up 16 hours k8s_kube-flannel_kube-flannel-ds-89hqs_kube-system_b46306d2-4482-11e8-8f6c-000c298e4460_2
51a8cba07f40 gcr.io/google_containers/pause-amd64:3.0 "/pause" 16 hours ago Up 16 hours k8s_POD_kube-flannel-ds-89hqs_kube-system_b46306d2-4482-11e8-8f6c-000c298e4460_2
进入容器里面查看配置文件是否改变,发现相关参数已经改成传递过来的变量值如下
[root@k8s-node-2 ~]# docker exec -it 083728e009f9 /bin/bash
root@083728e009f9:/# cd /opt/tomcat7/webapps/cc/WEB-INF/classes/
root@083728e009f9:/opt/tomcat7/webapps/cc/WEB-INF/classes# cat mongodb.properties
mongo.repset=192.168.0.36:27017
mongo.user=cc
mongo.password=cc123
mongo.database=cc
mongo.connectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=5
mongo.connectTimeout=10000
mongo.maxWaitTime=15000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=15000
mongo.slaveOk=true
通过http://192.168.0.42:8090/cc访问一下项目,如下图,说明镜像制作成功
2 部署发布应用
2.1创建发布应用对应的YAML文件 web-test-deployment.yaml 、web-test-service.yaml
web-test-deployment.yaml:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web-test-deployment
labels:
app: web-test
spec:
replicas: 2
selector:
matchLabels:
app: web-test
template:
metadata:
labels:
app: web-test
spec:
containers:
- name: web-test
env:
- name: MONGO_URL
value: "192.168.0.36:27017"
- name: MONGO_USER
value: "cc"
- name: MONGO_PASSWORD
value: "cc123"
- name: MONGO_DB
value: "cc"
image: chen/cc:v2
ports:
- containerPort: 8080
protocol: TCP
web-test-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: web-test
labels:
app: web-test
spec:
ports:
- name: myport
nodePort: 32001
port: 80
protocol: TCP
targetPort: 8080
selector:
app: web-test
sessionAffinity: None
type: NodePort
2.2 通过Deployment创建对应POD、并通过service对外提供服务
执行命令:kubectl create -f web-test-deployment.yaml
kubectl create -f web-test-service.yaml
2.3 通过http://192.168.0.42:32001/cc 访问发布的web系统 ,由于创建的是nodePort类型的service,所以IP可以是任意node主机的IP,如下
更多推荐
所有评论(0)