(10) Server K8S cluster deployment SpringBoot project actual combat

1. Prepare the springboot project

You can prepare a project on the https://start.spring.io/ website, here as k8s learning, so prepare a simple access interface in the springboot project.
insert image description here

2. Server environment preparation

Install Jdk

1. Update the system package:

sudo yum update

2. Install OpenJDK 11:

sudo yum install java-11-openjdk-devel

3. Verify Java installation:

java -version

insert image description here
4. Configure environment variables

vim /etc/profile
#JAVA_HOME 的内容根据具体安装jdk的路径替换
JAVA_HOME=/usr/lib/jvm/java-11-openjdk
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

5. Make the environment variable take effect

source /etc/profile

Install Maven

1. You can download version 3.5.4 at https://dlcdn.apache.org/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.zip website (or https://maven .apache.org/download.cgi to download the version you need), upload it to the server /usr/local/software/directory
2. Install the unzip command

yum install unzip -y

3. Unzip

unzip apache-maven-3.5.4-bin.zip

modify name

mv apache-maven-3.5.4 maven3.5

4. Configure environment variables

vim /etc/profile
JAVA_HOME=/usr/lib/jvm/java-11-openjdk
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
MAVEN_HOME=/usr/local/software/maven3.5
PATH=$PATH:$MAVEN_HOME/bin
export PATH JAVA_HOME CLASSPATH MAVEN_HOME

5. Make the environment variable take effect

source /etc/profile

3. The springboot project builds and packages docker

1. Set the package file name to demo and
set finalName in the pom file in the springboot project

insert image description here
2. Add the Dockerfile file, the file is placed in the project and directory under
insert image description here
the Dockerfile file

FROM adoptopenjdk/openjdk11:jre11u-nightly

ADD target/demo.jar demo.jar

ENTRYPOINT ["java","-jar","/demo.jar"]

3. Upload the springboot project to the server and enter the root directory for packaging

Excuting an order

mvn clean install

Build the image (don't ignore the last dot)

docker build -t demo:1.0 .

View mirror list

docker images

start up

docker run -d -it -p 8080:8080 --name=k8sdemo demo:1.0

4. Image push (Alibaba Cloud example)

Aliyun account access address
https://cr.console.aliyun.com/
The creation of the warehouse will not be described here, and Baidu (personal version does not charge)

Push private mirror warehouse

#仓库地址根据自己的地址替换
docker login --username=gq570566 registry.cn-shenzhen.aliyuncs.com
#打tag
#docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/fqfff/demo:[镜像版本号]
docker tag ba0652a92214 registry.cn-hangzhou.aliyuncs.com/fqfff/demo:1.0

#推送
#docker push registry.cn-hangzhou.aliyuncs.com/fqfff/demo:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/fqfff/demo:1.0
#拉取
docker pull registry.cn-hangzhou.aliyuncs.com/fqfff/demo:[镜像版本号]

5.K8s deployment SpringBoot project

create secert

#kubectl create secret docker-registry demo-docker-secret --docker-server=registry.cn-hangzhou.aliyuncs.com --docker-username=用户名 --docker-password=登录密码

kubectl create secret docker-registry demo-docker-secret --docker-server=registry.cn-hangzhou.aliyuncs.com --docker-username=gq570566--docker-password=*****

Create the yaml file of k8sdemo-deployment

How to quickly generate yaml files

kubectl create deployment k8sdemo --image=registry.cn-hangzhou.aliyuncs.com/fqfff/demo:1.0 --dry-run=client -o yaml > demo-k8s.yaml

Modify the number of copies to 2, mount the secret

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: k8sdemo
  name: k8sdemo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: k8sdemo
  strategy: {
    
    }
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: k8sdemo
    spec:
      imagePullSecrets:
        - name: demo-docker-secret
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/fqfff/demo:1.0
        name: demo-k8s
        resources: {
    
    }
status: {
    
    }

create controller

kubectl apply -f demo-k8s.yaml

Create service, nodePort type

kubectl expose deploy k8sdemo --port=8080 --target-port=8080 --type=NodePort

test access

view port

kubectl get svc

insert image description here
Access node ip+port+interface address
insert image description here

success!

Guess you like

Origin blog.csdn.net/csdn570566705/article/details/131040836