<?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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kinnylee</groupId>
<artifactId>k8s-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>k8s-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>kinnylee</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--編譯打包成鏡像-->
<build>
<resources>
<resource>
<directory>src/main/docker</directory>
<filtering>true</filtering>
<includes>
<include>**/Dockerfile</include>
</includes>
<targetPath>../docker</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!-- <repository>${docker.image.prefix}/${project.artifactId}</repository>-->
<repository>${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_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開發,下面的步驟可省略,直接編譯打包便可php
liunx下安裝IDE比較卡,windows下安裝docker兼容性很差。因而採用windows編寫代碼,遠程鏈接linux中docker放開的TCP端口,進行遠程鏡像打包java
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 Environmentnode
# 保存鏡像
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
複製代碼