search
HomeJavajavaTutorialHow to deploy springboot project to k8s

How to deploy springboot project to k8s

May 15, 2023 am 10:04 AM
springbootk8s

Steps to deploy springboot to k8s

  • The springboot project packages the image and deploys it to the image warehouse

  • Log in to the private image warehouse and pull the image

  • Create deployment

  • Expose service access port

Create secret

Log in private The warehouse needs to create a secret to store the authentication information of the docker registry

Create the secret

~$ kubectl create secret docker-registry fdf-docker-secret --docker-server=registry.cn-chengdu.aliyuncs.com --docker-username=17602117026 --docker-password=用户密码
secret/fdf-docker-secret created
~$
~$
~$ kubectl get secret
NAME                TYPE                             DATA   AGE
fdf-docker-secret   kubernetes.io/dockerconfigjson   1      15s

After it is created, it needs to be mounted to the pod later

Create the deployment yaml file

Quickly create a deployment and export the yaml file

~$ kubectl create deployment k8sdemo --image=registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0 --dry-run=client -o yaml > k8sdemo.yaml
~$ cat k8sdemo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: k8sdemo
  name: k8sdemo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8sdemo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: k8sdemo
    spec:
      containers:
      - image: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
        name: k8s-test
        resources: {}
status: {}
~$

Modify the k8sdemo.yaml file

#1.修改副本数量为2 
#2.挂在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: fdf-docker-secret
      containers:
      - image: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
        name: k8s-test
        resources: {}
status: {}

Create a deployment

~$ kubectl apply -f k8sdemo.yaml
deployment.apps/k8sdemo created
~$ kubectl get deploy
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
k8sdemo   0/2     2            0           12s
~$ kubectl get deploy
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
k8sdemo   2/2     2            2           24s
~$ kubectl get pod
NAME                       READY   STATUS    RESTARTS   AGE
k8sdemo-65d45fb49f-l4kx5   1/1     Running   0          28s
k8sdemo-65d45fb49f-pqsjw   1/1     Running   0          28s

Create service, nodePort

#port 服务端口    target-port pod端口
~$ kubectl expose deploy k8sdemo --port=9001 --target-port=9001 --type=NodePort
service/k8sdemo exposed
~$
~$ kubectl get svc k8sdemo
NAME      TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
k8sdemo   NodePort   10.96.10.79   <none>        9001:31921/TCP   12s
~$ curl http://127.0.0.1:31921/api/v1/user/name
zhangsan

You can see that the IP of serviced is 10.96.10.79 and the external port is 31921. The security group needs to open this port to access it.

Even if our service deployment is completed here, take a look at all the nodes

~$ kubectl get secret,pod,svc,deploy
NAME                       TYPE                             DATA   AGE
secret/fdf-docker-secret   kubernetes.io/dockerconfigjson   1      52m
NAME                           READY   STATUS    RESTARTS   AGE
pod/k8sdemo-65d45fb49f-l4kx5   1/1     Running   0          39m
pod/k8sdemo-65d45fb49f-pqsjw   1/1     Running   0          39m
NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
service/k8sdemo      NodePort    10.96.10.79   <none>        9001:31921/TCP   5m51s
service/kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP          301d
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/k8sdemo   2/2     2            2           39m
~$

The above is the detailed content of How to deploy springboot project to k8s. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)