This is a simple example of a blue/green deployment. It depends on springboot2, builds by maven, deploys by kubernetes.
Change the project version in pom.xml
as follow:
<groupId>com.shf.example</groupId>
<artifactId>springboot-kube-sample</artifactId>
<version>Blue</version>
It will be read in VersionController
:
@Slf4j
@RestController
public class VersionController {
@Autowired(required = false)
private BuildProperties buildProperties;
@GetMapping("/version")
public String version() {
if (null == buildProperties) {
return "UnKnow";
}
return buildProperties.getVersion();
}
}
Dockerfile is in ./docker folder, just input mvn clean install
command. Then we could get two images:
- local-dtr.com/springboot-kube-sample:Blue
- local-dtr.com/springboot-kube-sample:Green
In ./kubernetes folder, you will find all yaml-files for kubernetes.
- kubectl create namespace blue-green
- kubectl apply -f deployment-blue.yaml
- kubectl apply -f deployment-green.yaml
- kubectl apply -f service-blue.yaml
- kubectl apply -f ingress.yaml -n blue-green
Add hosts configuration into your **/etc/hosts file, the domains which defined in ingress.yaml
127.0.0.1 shf.sample.com
INPUT
curl -i http://shf.sample.com/sample-service/version
OUTPUT
Blue
- kubectl apply -f service-green.yaml
INPUT
curl -i http://shf.sample.com/sample-service/version
OUTPUT
Green