目录
1、架构图
2、部署一个java网站项目实践
2.1 编译java
2.2、构建与上传镜像
2.3、k8s中部署项目
访问
3、资源配置
3.2、加入配置文件挂载
4、部署数据库并连接
恢复数据
项目连接数据库
5、更新
执行
6、ingress暴露(前提需要运行ingress服务)
7、配置外网负载均衡
git clone https://github.com/lizhenliang/tomcat-java-demo
yum install java-1.8.0-openjdk maven git -y
cd tomcat-java-demo-master
vi /etc/maven/settings.xml
aliyunmaven * 阿里云公共仓库 https://maven.aliyun.com/repository/public
mvn clean package -Dmaven.test.skip=ture
ls target/
unzip target/ly-simple-tomcat-0.0.1-SNAPSHOT.war -d ROOT
vi Dockerfile
FROM lizhenliang/tomcatLABEL maintainer afCOPY target/ROOT /usr/local/tomcat/webapps/ROOT
docker build -t java-demo:v1 .
docker tag java-demo:v1 192.168.209.100/demo/java-demo:v1
vi /etc/docker/daemon.json
{"insecure-registries": ["192.168.209.100"]}
vi /etc/hosts
192.168.209.100 reg.ctnrs.com
docker login 192.168.209.100
docker push 192.168.209.100/demo/java-demo:v1
kubectl create deployment java-demo --image=192.168.209.100/demo/java-demo:v1 --dry-run=client -o yaml > deployment.yaml
kubectl create secret docker-registry harbor-auth --docker-username=admin --docker-password=Harbor12345 --docker-server=192.168.209.100
kubectl get secret
vi deployment
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: java-demoname: java-demospec:replicas: 1selector:matchLabels:app: java-demostrategy: {}template:metadata:creationTimestamp: nulllabels:app: java-demospec:imagePullSecrets:- name: harbor-authcontainers:- image: 192.168.209.100/demo/java-demo:v1name: java-demoresources: {}
kubectl apply -f deployment.yaml
kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort
vi deployment
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: java-demoname: java-demospec:replicas: 1selector:matchLabels:app: java-demostrategy: {}template:metadata:creationTimestamp: nulllabels:app: java-demospec:imagePullSecrets:- name: harbor-authcontainers:- image: 192.168.209.100/demo/java-demo:v1name: java-demoresources:limits:cpu: 1memory: 1Girequests:cpu: 0.7memory: 700MilivenessProbe:httpGet:path: /port: 8080initialDelaySeconds: 50periodSeconds: 30readinessProbe:httpGet:path: /port: 8080initialDelaySeconds: 50periodSeconds: 30
vi configmap.yaml
apiVersion: v1kind: ConfigMapmetadata:name: java-demo-configdata:application.yaml: |server:port: 8080spring:datasource:url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8username: rootpassword: 12345driver-class-name: com.mysql.jdbc.Driverfreemarker:allow-request-override: falsecache: truecheck-template-location: truecharset: UTF-8content-type: text/html; charset=utf-8expose-request-attributes: falseexpose-session-attributes: falseexpose-spring-macro-helpers: falsesuffix: .ftltemplate-loader-path:- classpath:/templates/
kubectl apply -f configmap.yaml
kubectl get configmap
vi deployment
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: java-demoname: java-demospec:replicas: 1selector:matchLabels:app: java-demostrategy: {}template:metadata:creationTimestamp: nulllabels:app: java-demospec:imagePullSecrets:- name: harbor-authcontainers:- image: 192.168.209.100/demo/java-demo:v1name: java-demoresources:limits:cpu: 1memory: 1Girequests:cpu: 0.7memory: 700MilivenessProbe:httpGet:path: /port: 8080initialDelaySeconds: 50periodSeconds: 30readinessProbe:httpGet:path: /port: 8080initialDelaySeconds: 50periodSeconds: 30volumeMounts:- name: configmountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml"subPath: "application.yml"volumes:- name: configconfigMap:name: java-demo-configitems:- key: "application.yaml"path: "application.yml"
kubectl applyment -f deployment
vi mysql.yaml
apiVersion: v1kind: Secretmetadata:name: java-demo-dbtype: Opaquedata:mysql-root-password: "MTIzNDU2"mysql-password: "MTIzNDU2"---apiVersion: apps/v1kind: Deploymentmetadata:name: java-demo-dbspec:selector:matchLabels:project: wwwapp: mysqltemplate:metadata:labels:project: wwwapp: mysqlspec:containers:- name: dbimage: mysql:5.7.30resources:requests:cpu: 500mmemory: 512Milimits:cpu: 500mmemory: 512Mienv:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: java-demo-dbkey: mysql-root-password- name: MYSQL_PASSWORDvalueFrom:secretKeyRef:name: java-demo-dbkey: mysql-password- name: MYSQL_USERvalue: "aliang"- name: MYSQL_DATABASEvalue: "k8s"ports:- name: mysqlcontainerPort: 3306livenessProbe:exec:command:- sh- -c- "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"initialDelaySeconds: 30periodSeconds: 10readinessProbe:exec:command:- sh- -c- "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"initialDelaySeconds: 5periodSeconds: 10volumeMounts:- name: datamountPath: /var/lib/mysqlvolumes:- name: datapersistentVolumeClaim:claimName: java-demo-db---apiVersion: v1kind: PersistentVolumeClaimmetadata:name: java-demo-dbspec:storageClassName: "managed-nfs-storage"accessModes:- "ReadWriteOnce"resources:requests:storage: "8Gi"---apiVersion: v1kind: Servicemetadata:name: java-demo-dbspec:type: ClusterIPports:- name: mysqlport: 3306targetPort: mysqlselector:project: wwwapp: mysql
kubectl apply -f mysql.yaml
cd /root/tomcat-java-demo-master/db
kubectl cp tables_ly_tomcat.sql java-demo-db-76d66777b6-bcwx4:/
kubectl exec -it java-demo-db-76d66777b6-bcwx4 -- bash
create database test;
use test;
source /tables_ly_tomcat.sql;
show tables;
vi configmap.yaml
apiVersion: v1kind: ConfigMapmetadata:name: java-demo-configdata:application.yaml: |server:port: 8080spring:datasource:url: jdbc:mysql://10.108.140.66:3306/test?characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverfreemarker:allow-request-override: falsecache: truecheck-template-location: truecharset: UTF-8content-type: text/html; charset=utf-8expose-request-attributes: falseexpose-session-attributes: falseexpose-spring-macro-helpers: falsesuffix: .ftltemplate-loader-path:- classpath:/templates/
kubectl apply -f configmap.yaml
cd /root/tomcat-java-demo-master
vi src/main/resources/templates/index.ftl
mvn clean package -Dmaven.test.skip=ture
cd target/
unzip ly-simple-tomcat-0.0.1-SNAPSHOT.war -d ROOT
cd ..
docker build -t java-demo:v2 .
docker tag java-demo:v2 192.168.209.100/demo/java-demo:v2
docker push 192.168.209.100/demo/java-demo:v2
vi deployment.yaml
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: java-demoname: java-demospec:replicas: 1selector:matchLabels:app: java-demostrategy: {}template:metadata:creationTimestamp: nulllabels:app: java-demospec:imagePullSecrets:- name: harbor-authcontainers:- image: 192.168.209.100/demo/java-demo:v2name: java-demoresources:limits:cpu: 1memory: 1Girequests:cpu: 0.7memory: 700MilivenessProbe:httpGet:path: /port: 8080initialDelaySeconds: 50periodSeconds: 30readinessProbe:httpGet:path: /port: 8080initialDelaySeconds: 50periodSeconds: 30volumeMounts:- name: configmountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml"subPath: "application.yml"volumes:- name: configconfigMap:name: java-demo-configitems:- key: "application.yaml"path: "application.yml"
kubectl apply -f deployment.yaml
vi ingress.yaml
apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: java-demospec:ingressClassName: nginxrules:- host: java.aliangedu.cnhttp:paths:- path: /pathType: Prefixbackend:service:name: java-demoport:number: 80
kubectl apply -f ingress.yaml
yum install epel-release
yum install nginx
vi /etc/nginx/nginx.conf
upstream ingress-controller {server 192.168.209.111:30761;server 192.168.209.112:30761;}server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;location / {proxy_pass http://ingress-controller;proxy_set_header Host $Host;}
systemctl start nginx