如何在kubernetes中将Tomcat与mysql db容器链接

时间:2019-05-08 12:08:50

标签: docker kubernetes

我的tomcat和mysql容器未连接。因此,如何链接它们,以便我的war文件能够成功运行。

我使用docker文件构建了tomcat图像

FROM picoded/tomcat7
COPY data-core-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/data-core-0.0.1-SNAPSHOT.war

mysql.yaml


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
        imagePullPolicy: "IfNotPresent"
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        - name: MYSQL_DATABASE
          value: data-core  
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-initdb-pv-claim

mysqlpersistantvolume.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: "/home/vignesh/stackoverflow/tmp/data"  //this is the path were my 
                                                    sql init script is placed.
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-initdb-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

tomcat.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  type: NodePort     
  ports:
  - name: http
    port: 8080
    targetPort: 8080

  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: suji165475/vignesh:tomcatserver
        name: tomcat
        env:
        - name: DB_PORT_3306_TCP_ADDR
          value: mysql                  #service name of mysql


        - name: DB_ENV_MYSQL_DATABASE
          value: data-core
        - name: DB_ENV_MYSQL_ROOT_PASSWORD
          value: root
        ports:
        - containerPort: 8080
          name: http


        volumeMounts:
        - name: tomcat-persistent-storage
          mountPath: /var/data
      volumes:
      - name: tomcat-persistent-storage
        persistentVolumeClaim:
          claimName: tomcat-pv-claim

tomcatpersistantvolume.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: tomcat-pv
  labels:
    type: local
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/app"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: tomcat-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

当前使用type:Nodeport进行tomcat服务。我也必须为MySQL使用Nodeport吗?如果是这样,那我应该提供相同的节点端口还是不同的??

注意:Iam使用腻子终端在服务器上运行所有这些操作

1 个答案:

答案 0 :(得分:0)

kubetnetes启动服务时,它将为主机,端口等添加环境变量。尝试使用环境变量MYSQL_SERVICE_HOST