本篇是基于k8s-v1.15.0版本,在现有集群上部署一个java项目。
java-deamon的源码下载地址详见百度云
链接:https://pan.baidu.com/s/1mK5HPrPWKUTn1Wf3AG3p3w
提取码:gum7
tomcat下载地址
http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.46/bin/apache-tomcat-8.0.46.tar.gz
cat Dockerfile-tomcatv1
FROM www.my.com/sys/centos:7.7.1908
LABEL maintainer wangkaixuan
ADD jdk-8u112-linux-x64.tar.gz /usr/local
ENV JAVA_HOME=/usr/local/jdk1.8.0_112
ENV PATH=$JAVA_HOME/bin/:$PATH
ENV CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPAT
ADD apache-tomcat-8.0.46.tar.gz /usr/local
WORKDIR /usr/local/apache-tomcat-8.0.46
EXPOSE 8080
ENTRYPOINT ["./bin/catalina.sh", "run"]
docker build -t tomcat:v1 -f Dockerfile-tomcatv1 .
docker tag tomcat:v1 www.my.com/web/tomcat:v1
docker push www.my.com/web/tomcat:v1
├── db(数据库sql文件)
├── pom.xml(maven工程会用)
└── src(源代码)
docker pull www.my.com/sys/mysql:5.7
docker run -itd --name mysql -p 3306:3306 -v /var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 www.my.com/sys/mysql:5.7
docker inspect mysql
mysql -uroot -p -h172.17.0.2
CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 ;
USE `test`;
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL COMMENT '名字',
`age` INT(3) NOT NULL COMMENT '年龄',
`sex` CHAR(1) DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
grant all on *.* to 'root'@'%' identified by '123456';
mysql -uroot -p -h 10.10.0.120
cd /root/java-daemon/
unzip tomcat-java-demo-master.zip
cd
cat /root/java-daemon/tomcat-java-demo-master/src/main/resources/application.yml
...
url: jdbc:mysql://10.10.0.120:3306/test?characterEncoding=utf-8
username: root
password: 123456
...
mvn clean package -Dmaven.test.skip=true #在当前目录下,编译后会生成一个target目录,里面有编译好的war包
cat Dockerfile
FROM www.my.com/web/tomcat:v1
LABEL maintainer wangkaixuan
RUN rm -rf /usr/local/apache-tomcat-8.0.46/webapps/ROOT
ADD target/*.war /usr/local/apache-tomcat-8.0.46/webapps/ROOT.war
docker build -t java-deamon:v1 .
docker tag java-deamon:v1 www.my.com/web/java-deamon:v1
docker push www.my.com/web/java-deamon:v1
kubectl create deployment java-deamon --image=www.my.com/web/java-deamon:v1 --dry-run -o yaml > java.yaml #生成deployment的yaml模板
编辑yaml模板,删除metadata下的creationTimestamp;删除spec下的strategy;删除spec下的spec下的resources;删除status;修改replicas为2
cat << EOF > java.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: java-deamon
name: java-deamon
spec:
replicas: 2
selector:
matchLabels:
app: java-deamon
template:
metadata:
labels:
app: java-deamon
spec:
containers:
- image: www.my.com/web/java-deamon:v1
name: java-deamon
EOF
kubeclt apply -f java.yaml #执行yaml模板,生成pod
kubectl get pod -o wide #查看pod
kubectl expose deployment java-deamon --port=80 --target-port=8080 --type=NodePort -o yaml --dry-run >svc.yaml #生成svc的yaml模板,越详细越好
编辑yaml模板
删除metadata下的creationTimestamp;删除status
cat svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: java-deamon
name: java-deamon
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: java-deamon
type: NodePort
kubeclt apply -f svc.yaml #执行yaml模板,生成scv
kubectl get pod,svc #查看svc和pod以及端口
http://nodeip:nodeport #nodeip值得是任意k8s节点的ip;nodeport值得是k8s分配给svc的随机端口号
cat <<EOF > ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: java-deamon
namespace: default
spec:
rules:
- host: www.java-tomcat.com
http:
paths:
- path: /
backend:
serviceName: java-deamon
servicePort: 8080
EOF
kubectl apply -f ingress.yaml #执行yaml模板,生成对应的ingress
kubectl get pod,svc,ingress #查看pod,svc,ingress
dns服务器部署参考轻量级域名解析服务器之dnsmasq
echo '10.10.0.111 www.java-tomcat.com' >> /etc/hosts #在DNS服务器中的hosts配置
systemctl restart dnsmasq.service #重启dns服务器