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

时间:2019-05-08 01:40:47

标签: docker kubernetes docker-compose

如何将我在webapps文件夹中具有war文件的tomcat容器链接到另一个具有mysql数据库的容器。当前,如果我在tomcat Manager应用中启动我的war文件,我得到错误提示无法在contextpath /data-core-0.0处启动应用程序。 1-SNAPSHOT卡特琳娜生命周期异常。

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

statefulset.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

persistantvolumeclaim.yaml

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

1 个答案:

答案 0 :(得分:0)

您必须创建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