Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Springboot or Nginx using Kubernetes

2025-03-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you how to use Kubernetes to deploy Springboot or Nginx. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1 preface

It's very simple, just a yaml file.

2 one-click deployment of springboot

2.1 prepare the yaml file

When the image file is ready, it is very easy to deploy to kubernetes. All you need is a file in yaml format that describes the components you need, such as deployment, service, ingress, etc. The definition is as follows:

Apiversion: apps/v1kind: deploymentmetadata: name: pkslow-springboot-deploymentspec: selector: matchlabels: app: springboot replicas: 2 template: metadata: labels: app: springboot spec: containers:-name: springboot image: pkslow/springboot-mongo:0.0.6 ports:-containerport: 8080---apiversion: v1kind: servicemetadata: labels: app: springboot name: pkslow-springboot-servicespec: ports:-port: 8080 name: springboot-service protocol: tcp targetport: 8080 nodeport: 30080 selector: app: springboot type: nodeport

Kind: types, including deployment, service, pod, ingress, etc., which are very rich

Metadata: used to define some component information, such as names, labels, etc.

Labels: tag function, very useful for selecting associations, but label does not provide uniqueness, you can use combinations to select

Nodeport: for services that need to be exposed externally, there are three ways: nodeports, loadbalancer, and ingress. Note that when using nodeports;, the default port range is [3000-32767]. If you need other ranges, you need to modify the relevant parameters.

2.2 deploy through the kubectl command

When the yaml file is ready, you can deploy it with the following command:

$kubectl create-f pksow-springboot.yaml deployment.apps/pkslow-springboot-deployment createdservice/pkslow-springboot-service created

Looking at the console log shows that deployment and service were created successfully. View the dashboard as follows:

Access the web service:

Check through the command line:

$kubectl get deploymentname ready up-to-date available agepkslow-springboot-deployment 2 8m15spkslow-springboot-deployment-68dffc6795 2 2 28 m2s $kubectl get servicename type cluster-ip external-ip port (s) agekubernetes clusterip 10.96.0.1 443/tcp 10mpkslow-springboot-service nodeport 10.102.218.119 8080:30080/tcp 8m7s $kubectl get podname ready status restarts agepkslow-springboot-deployment-68dffc6795-874tp 1 running 0 8m15spkslow-springboot-deployment-68dffc6795-89xww 1 running 0 8m15s

So far, we have successfully released springboot to kubernetes.

2.3To try killing a pod?

The minimum administrative element of kubernetes is not a container, but a pod.

Let's try to delete a pod and see what happens.

$kubectl delete pod pkslow-springboot-deployment-68dffc6795- 89xwwpod "pkslow-springboot-deployment-68dffc6795- 89xww" deleted$ kubectl get podname ready status restarts agepkslow-springboot-deployment-68dffc6795-874tp 1 running 0 13mpkslow-springboot-deployment-68dffc6795-gpw67 1 running 0 46s

You can find that when another pod is deleted, a new pod is automatically generated for us, which increases the high availability of the entire service.

2.4 try killing a container?

Let's explore what happens if you kill a container instance.

$docker ps$ docker rm-f 57869688a22657869688a226 $docker ps

After the experiment, after killing a container, it will automatically regenerate a container instance for us. Pod will not change, nor will it be regenerated.

2.5 Rapid expansion of pod

The number of pod needs to be increased because of the sudden increase in user requests and the service will not hold up. Just modify the replicas of the yaml configuration file and update it to replicas: 4. Then execute the following command:

$kubectl apply-f pksow-springboot.yaml

Check out the dashboard, which adds two to the original two pod.

3 one-click deployment of nginx

If you do not have an springboot image, you can use the official nginx image. The yaml file is as follows:

Apiversion: apps/v1kind: deploymentmetadata: name: nginx-deploymentspec: selector: matchlabels: app: nginx replicas: 3 template: metadata: labels: app: nginx spec: containers:-name: nginx image: nginx:1.19.0 ports:-containerport: 80---apiversion: v1kind: servicemetadata: labels: app: nginx name: nginx-servicespec: ports:-port: 80 name: nginx-service1 protocol: tcp targetport: 80 nodeport: 30000-port: 81 name: nginx-service2 protocol: 80 tcp targetport: 30001 nodeport: selector: selector: selector

Execute the deployment command:

$kubectl apply-f nginx-deployment-scale.yaml deployment.apps/nginx-deployment createdservice/nginx-service created

View the dashboard as follows:

Access the service: or. Because we set up two.

These are all the contents of the article "how to deploy Springboot or Nginx using Kubernetes". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report