xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
<relativePath/>
parent>
<groupId>com.kinnyleegroupId>
<artifactId>k8s-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>k8s-demoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<docker.image.prefix>kinnyleedocker.image.prefix>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/dockerdirectory>
<filtering>truefiltering>
<includes>
<include>**/Dockerfileinclude>
includes>
<targetPath>../dockertargetPath>
resource>
resources>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>dockerfile-maven-pluginartifactId>
<version>1.4.0version>
<configuration>
<repository>${project.artifactId}repository>
<tag>${project.version}tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jarJAR_FILE>
buildArgs>
configuration>
plugin>
plugins>
build>
project>
复制代码
FROM openjdk:8-jdk-alpine
ENV TIME_ZONE Asia/Shanghai
RUN apk add --no-cache tzdata && \
echo "${TIME_ZONE}" > /etc/timezone && ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime
VOLUME /data
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
复制代码
package com.kinnylee.k8sdemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class K8sDemoController {
@GetMapping("/hello")
public String hello(){
return "hello k8s demo.";
}
}
复制代码
如果在liunx下安装idea开发,下面的步骤可省略,直接编译打包即可 liunx下安装IDE比较卡,windows下安装docker兼容性不好。于是采用windows编写代码,远程连接linux中docker放开的TCP端口,进行远程镜像打包
vim /lib/systemd/system/docker.service
# ExecStart结尾添加:-H tcp://0.0.0.0:2375
[Service]
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
工具名称:Rapid Environment
# 保存镜像
docker save k8s-demo:0.0.1-SNAPSHOT -o k8s-demo.tar
# 镜像拷贝到其他机器
scp k8s-demo.tar 10.1.69.100:/data/images
scp k8s-demo.tar 10.1.69.102:/data/images
scp k8s-demo.tar 10.1.69.103:/data/images
# 其他节点上分别执行以下命令,恢复镜像
cd /data/images
docker load < k8s-demo.tar
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: k8s-demo
namespace: spring-test
spec:
replicas: 3
template:
metadata:
labels:
app: k8s-demo
spec:
containers:
- name: k8s-demo
image: kinnylee/k8s-demo:0.0.1-SNAPSHOT
ports:
containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: k8s-demo
namespace: spring-test
spec:
type: NodePort
selector:
app: k8s-demo
ports:
- protocol: TCP
port: 8888
targetPort: 8080
nodePort: 30003
# 创建命名空间
kubectl create namespace spring-test
# 验证是否成功
kubectl get ns
# 开始创建deployment
kubectl apply -f deployment.yaml
# 验证是否创建成功
kubectl get deployment -n spring-test
# 验证3个pod是否创建成功
kubectl get pods -n spring-test
# 如果启动不成功,排查原因,pod后面的参数为上一步查询的pod名称
kubectl describe pod k8s-demo-f99976b78-5blxd -n spring-test
# 创建service
kubectl apply -f service.yaml
# 验证是否创建成功
kubectl get svc -n spring-test