Example: to achieve running Tomcat in the Web app, ISP page via JDBC direct access to the MySQL database and display the data.
Demand: Web App container vessel MySQL, web ---> mysql
IP addresses need to be injected into the container by way of MySQL environment variables Web App container, at the same time, you need to port 8080. 8080 port mapping host of Web App container in order to access externally.
1.MySQL service creates a definition file RC
# 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
View pod we created
# kubectl get pods | grep mysql
mysql-76999dd7c8-cgcwk 1/1 Running 0 40m
2.MySQL service creates a file SVC
# 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 mysql
mysql ClusterIP 10.0.0.127 <none> 3306/TCP 42m
MySQL Cluster IP address assigned service 10.0.0.127, which is a virtual IP address, then other Pod newly created Kubernetes cluster of connection and can be accessed through the Cluster Service's IP + port number 6379.
The unique name of the Service, the container can be obtained from the environment variable to Cluster IP address and port corresponding Service, thereby initiating TCP / IP connection request.
1.3 Tomcat service creates an RC file
MyWeb rc.yaml-CAT #
apiVersion: Apps / v1beta1
kind: the 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
the ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_HOST
the #VALUE: 'MySQL' # Note, where absence of a name, 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 myweb
myweb-77b4646d58-tcmds 1/1 Running 0 19m
myweb-77b4646d58-wkzx6 1/1 Running 0 19m
1.4 Tomcat service creates a file SVC
# 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 myweb
myweb NodePort 10.0.0.27 <none> 8080:30001/TCP 44m
2. Verify
# kubectl get pods -o wide | grep myweb
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myweb-77b4646d58-tcmds 1/1 Running 0 22m 172.17.30.6 192.168.200.129 <none> <none>
myweb-77b4646d58-wkzx6 1/1 Running 0 22m 172.17.39.6 192.168.200.130 <none> <none>
http://192.168.200.130:30001/demo
Submitted successfully
MySQL query to verify by container
It represents the deployment of OK!