Python社区  »  DATABASE

如何在kubernetes clusture上链接和运行mysql数据库和tomcat服务器?

Swetha Swaminathan • 2 周前 • 34 次点击  

如何将webapps文件夹中包含war文件的tomcat容器链接到另一个包含mysql数据库的容器。当前,如果在tomcat管理器应用程序中启动war文件,则会出现错误,说明无法在contextpath/data-core-0.0.1-SNAPSHOT catalina生命周期异常处启动应用程序。

我想我的tomcat容器无法访问mysql数据库容器!!

状态集.yaml

serviceName: mysql
 template:
  metadata:
   labels:
    name: mysql
  spec:
   containers:
    - name: mysql
      image: suji165475/vignesh:latest
      ports:
       - containerPort: 3306
      volumeMounts:
       - name: mysql-initdb
         mountPath: /docker-entrypoint-initdb.d
   volumes:
    - name: mysql-initdb
      persistentVolumeClaim:
       claimName: mysql-initdb-pv-claim

持久体积

kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-initdb-pv-volume
  labels:
    type: local
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/tmp/data"

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-initdb-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

tomcatdeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  labels:
    app: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: suji165475/vignesh:tomcatserver
        ports:
        - containerPort: 8080
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49249
 
34 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 2 周前
Harsh Manvar
Reply   •   1 楼
Harsh Manvar    6 月前

您必须为mysql创建服务,并在tomcat服务中使用该服务名,这样tomcat pod才能与mysql服务器进行通信。

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: <value>
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

现在部署tomcat:

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443
  selector:
    app: tomcat
    tier: frontend
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  selector:
    matchLabels:
      app: tomcat
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: tomcat
        tier: frontend
    spec:
      containers:
      - image: <image location>
        name: tomcat
        env:
        - name: DB_PORT_3306_TCP_ADDR
          value: mysql                  #service name of mysql
        - name: DB_ENV_MYSQL_PASSWORD
          value: <Mysql password>
        - name: DB_ENV_MYSQL_USER
          value: <mysql user>
        - name: DB_ENV_MYSQL_DATABASE
          value: <database name>
        - name: DB_ENV_MYSQL_PASSWORD
          value: <Mysql password>
        ports:
        - containerPort: 80
          name: http
        - containerPort: 443
          name: https
        volumeMounts:
        - name: tomcat-persistent-storage
          mountPath: /var/data
      volumes:
      - name: tomcat-persistent-storage
        persistentVolumeClaim:
          claimName: tomcat-pv-claim