使用spring boot的k8s pod服务访问集群内mysql 中的数据。

创建数据库

kubectl run -it -n test --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -p{yourpassword}
# 如果已经创建过mysql-client
执行
kubectl exec -it  mysql-client -n test -- mysql -h mysql -p{yourpassword}

# 创建数据库
create database spring_boot_docker_test;
use spring_boot_docker_test;
# 创建表
create table message(
    env varchar(32),
    message varchar(64)
);
# 添加数据
insert into message values('dev', 'hello dev');
insert into message values('test', 'hello test');
insert into message values('release', 'hello release');

修改工程

# pom.xml的project->dependencies下新增依赖
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

# application.properties添加spring datasource配置
#mybatis
spring.datasource.url=@spring.datasource.url@
spring.datasource.username=@spring.datasource.username@
spring.datasource.password=@spring.datasource.password@
spring.datasource.driver=com.mysql.jdbc.Driver
spring.datasource.initial-size=5
spring.datasource.min-idle=5
spring.datasource.max-idle=20
spring.datasource.max-wait=10000
spring.datasource.validation-query=SELECT 1
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=27800

# 新增MessageMapper.java接口
package com.sunhaojie.sbdit.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface MessageMapper {

    @Select("select message from message where env = #{env}")
    public String getMessage(@Param("env") String env);
}

# 新增业务类MessageBo.java
package com.sunhaojie.sbdit.bo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunhaojie.sbdit.mapper.MessageMapper;

@Service
public class MessageBo {

    @Autowired
    private MessageMapper messageMapper;

    public String getMessage(String env) {
        String message = messageMapper.getMessage(env);
        return env + ":" + message;
    }
}

# 修改HelloWorldController.java
package com.sunhaojie.sbdit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunhaojie.sbdit.bo.MessageBo;

@RequestMapping("hello_world")
@Controller
public class HelloWorldController {

    @Autowired
    private MessageBo messageBo;

    @Value("${env}")
    private String env;

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public String sayHello() {
        return messageBo.getMessage(env);
    }
}

镜像打包

# 工程打包
mvn clean package -Ptest
# 创建镜像
docker build . -t harbor.sunhaojie.com/library/spring_boot_docker_image_test:v0.002
# 推送到私服
docker push harbor.sunhaojie.com/library/spring_boot_docker_image_test:v0.002

更新服务

# 修改版本号v0.001->v0.002

apiVersion: v1
kind: Pod
metadata:
  namespace: test
  name: spring-boot-docker-image-test
  labels:
    app: spring-boot-docker-image-test
spec:
  containers:
    - name: spring-boot-docker-image-test
      image: harbor.sunhaojie.com/library/spring_boot_docker_image_test:v0.002
      imagePullPolicy: Always
  imagePullSecrets:
    - name: harbor.sunhaojie.com

kubectl apply -f spring-boot-docker-image-test-pod.yaml