部署一个 Java 应用,访问 MySql 数据库。

  • 创建MySQL 应用服务
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
  namespace: yoyi-test
  labels:
    name: mysql
spec:
  replicas: 1
  selector:
    name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: hub.c.163.com/library/mysql:5.5
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
  • MySQL  service 创建
apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  namespace: yoyi-test
  labels:
    name: mysql-svc
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 33306
  selector:
    name: mysql
  • 创建tomcat web 服务
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
  namespace: yoyi-test
spec:
  replicas: 2
  selector:
    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: "10.68.250.179"
        - name: MYSQL_SERVICE_PORT
          value: "3306"

  • 创建myweb  svc
apiVersion: v1
kind: Service
metadata:
  name: myweb
  namespace: yoyi-test
spec:
   ports:
     - port: 8080
       targetPort: 8080
       nodePort: 30001
   selector:
     app: myweb
   type: NodePort

注:若出现MySQL连接错误,请检查Myweb 的MySQL 配置
        - name: MYSQL_SERVICE_HOST
          value: "10.68.250.179"

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐