springboot版本helloworld發佈到k8s示例

操做步驟

  • windows上編寫代碼
  • 遠程linux的docker引擎把代碼打成鏡像
  • 編寫k8s的yaml文件
  • 應用yaml文件,啓動服務

前置任務

一. 基於springboot編寫hello world代碼

1. 代碼結構

2. pom文件

<?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>

複製代碼

3. Dockerfile文件

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"] 
複製代碼

4. controller文件

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

1. 配置遠程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

複製代碼

2. IDEA中設置Docker執行的遠程地址

3. 環境變量中添加DOCKER_HOST

工具名稱:Rapid Environmentnode

4. 開始編譯打包成鏡像

5. 編譯成功後的鏡像

6. linux上驗證正確性

7. 將鏡像拷貝到其餘節點(有私倉則直接push發佈,不須要這一步)

# 保存鏡像
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
複製代碼

三. 編寫k8s的yaml文件

Deployment

  • deployment.yaml
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

複製代碼

Service

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
複製代碼

四. 開始部署到k8s

1. 建立namespace

# 建立命名空間
kubectl create namespace spring-test
# 驗證是否成功
kubectl get ns
複製代碼

2. 建立deployment

# 開始建立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
複製代碼

3. 建立service

# 建立service
kubectl apply -f service.yaml 
 # 驗證是否建立成功
kubectl get svc -n spring-test

複製代碼

4. 外部訪問服務

  • 以下圖:輸入k8s節點ip和30003端口號,就能夠看到服務能夠被訪問到了

相關文章
相關標籤/搜索