Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

K8s build and deploy Tomcat+MySQL method

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following is mainly to bring you k8s build deployment Tomcat+MySQL method, I hope these contents can bring you practical use, this is also the main purpose of my editing k8s build deployment Tomcat+MySQL method this article. Okay, no more nonsense, let's go straight to the following.

Example: Implement a Web app running in Tomcat, JSP pages directly access MySQL databases through JDBC and display data.

Requirements: Web App container MySQL container, web--->mysql

The MySQL container IP address needs to be injected into the Web App container through environment variables. At the same time, port 8080 of the Web App container needs to be mapped to port 8080 of the host for external access.

1. YAML writing

MySQL service creates an RC file

# cat mysql-rc.yaml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: mysql

spec:

replicas: 1

selector:

matchLabels:

app: mysql

template:

metadata:

labels:

app: mysql

spec:

containers:

- name: mysql

image: mysql:5.7

ports:

- containerPort: 3306

env:

- name: MYSQL_ROOT_PASSWORD

value: "123456"

# kubectl create -f mysql-rc.yaml replicationcontroller/mysql created

Check out the pods we created.

# kubectl get pods | grep mysqlmysql-76999dd7c8-cgcwk 1/1 Running 0 40m

1.2 MySQL service creates an SVC file

# cat mysql-svc.yaml

apiVersion: v1

kind: Service

metadata:

name: mysql

spec:

ports:

- port: 3306

selector:

app: mysql

# kubectl create -f mysql-svc.yaml service/mysql created# kubectl get svc | grep mysqlmysql ClusterIP 10.0.0.127 3306/TCP 42m

MySQL service assigns the Cluster IP address of 10.0.0.127, which is a virtual IP address, and other newly created pods in the Kubernetes cluster can then connect to and access it via the Cluster IP of Service + port number 6379.

According to the unique name of the Service, the container can obtain the Cluster IP address and port corresponding to the Service from the environment variable, and thus initiate a TCP/IP connection request.

1.3 Tomcat service creates an RC file

# cat myweb-rc.yaml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: myweb

spec:

replicas: 2

selector:

matchLabels:

app: myweb

template:

metadata:

labels:

app: myweb

spec:

containers:

- name: myweb

image: kubeguide/tomcat-app:v1

ports:

- containerPort: 8080

env:

- name: MYSQL_SERVICE_HOST

#value: 'mysql' #Note, there is no domain name resolution here, it is best to configure MySQL cluster address

value: '10.0.0.127' # kubectl get svc available

- name: MYSQL_SERVICE_PORT

value: '3306'

# kubectl create -f myweb-rc.yaml replicationcontroller/myweb created# kubectl get pods | grep mywebmyweb-77b4646d58-tcmds 1/1 Running 0 19mmyweb-77b4646d58-wkzx6 1/1 Running 0 19m

1.4 Tomcat service creates an SVC file

# cat myweb-svc.yaml

apiVersion: v1

kind: Service

metadata:

name: myweb

spec:

type: NodePort

ports:

- port: 8080

nodePort: 30001

selector:

app: myweb

# kubectl create -f myweb-svc.yaml service/myweb created# kubectl get svc | grep mywebmyweb NodePort 10.0.0.27 8080:30001/TCP 44m

2. be verified

# kubectl get pods -o wide | grep mywebNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmyweb-77b4646d58-tcmds 1/1 Running 0 22m 172.17.30.6 192.168.200.129 myweb-77b4646d58-wkzx6 1/1 Running 0 22m 172.17.39.6 192.168.200.130

http://192.168.200.130:30001/demo

submitted successfully

Validating queries with MySQL containers

For the above method about k8s building and deploying Tomcat+MySQL, do you think it is very helpful? If you need to know more, please continue to pay attention to our industry information, I believe you will like these contents.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report