Kubernetes cluster deployment SpringBoot project process

Make a mirror image and upload the project to the Alibaba Cloud warehouse

https://www.yuque.com/docs/share/69500bda-8be1-4a8b-9794-6c27fb66eef7?# "Docker uploads the image to the Alibaba Cloud image warehouse, and then pulls it locally"

Deployment deployment image creates a pod

kubectl create deployment [deployment name]–image=[mirror address] --dry-run -o yaml >[yaml file name].yaml

[root@zjj101 k8s]# kubectl create  deployment javademo1 --image=registry.cn-beijing.aliyuncs.com/zjjmkmkksjm/zjj101:1.0 --dry-run -o yaml >javademo1.yaml
W0228 16:41:04.046934   11821 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
[root@zjj101 k8s]#

--Dry-run -o yaml >javademo1.yaml means to export a yaml file in the current directory, for convenience

View created files

[root@zjj101 k8s]#  cat javademo1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: javademo1
  name: javademo1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: javademo1
  strategy: {
    
    }
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: javademo1
    spec:
      containers:
      - image: registry.cn-beijing.aliyuncs.com/zjjmkmkksjm/zjj101:1.0
        name: zjj101
        resources: {
    
    }
status: {
    
    }
[root@zjj101 k8s]#

Execute yaml file

kubectl apply -f [yaml file name].yaml

[root@zjj101 k8s]# kubectl apply -f javademo1.yaml
deployment.apps/javademo1 created

View pod

kubectl get pods

Found that there is already. Need to wait patiently, generally need to wait ten minutes for the status to become running

[root@zjj101 k8s]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
javademo1-69f7678847-qf5gf   1/1     Running   0          6m59s

You can use kubectl describe pod [pod name] to see if an error is reported

[root@zjj101 k8s]#  kubectl describe pod javademo1-69f7678847-qf5gf
Name:           javademo1-69f7678847-qf5gf
Namespace:      default
Priority:       0
Node:           zjj103/172.16.10.103
Start Time:     Sun, 28 Feb 2021 17:22:23 +0800
Labels:         app=javademo1
                pod-template-hash=69f7678847
Annotations:    <none>
Status:         Pending
IP:
IPs:            <none>
Controlled By:  ReplicaSet/javademo1-69f7678847
Containers:
  zjj101:
    Container ID:
    Image:          registry.cn-beijing.aliyuncs.com/zjjmkmkksjm/zjj101:                                                                               1.0
    Image ID:
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-z                                                                               plfm (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-zplfm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zplfm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  5m20s  default-scheduler  Successfully assigned def                                                                               ault/javademo1-69f7678847-qf5gf to zjj103
  Normal  Pulling    5m19s  kubelet, zjj103    Pulling image "registry.c                                                                               n-beijing.aliyuncs.com/zjjmkmkksjm/zjj101:1.0"

Then I checked and found that it was all right.

[root@zjj101 k8s]# kubectl get pod -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP             NODE     NOMINATED NODE   READINESS GATES
javademo1-69f7678847-qf5gf   1/1     Running   0          9m52s   10.244.3.141   zjj103   <none>           <none>

The above ip 10.244.3.141 is used internally by the kubernetes cluster, and the external browser cannot access it, so the port needs to be exposed externally.

Add three copies to make one expansion

kubectl scale deployment [deployment名字] --replicas=3

[root@zjj101 k8s]# kubectl  scale deployment javademo1 --replicas=3
deployment.apps/javademo1 scaled

k8s will expand on the node node

Found that the capacity has been expanded.

[root@zjj101 k8s]# kubectl  get deployment javademo1
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
javademo1   3/3     3            3           14m
[root@zjj101 k8s]# kubectl get pod -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP             NODE     NOMINATED NODE   READINESS GATES
javademo1-69f7678847-68z2w   1/1     Running   0          3m12s   10.244.3.143   zjj103   <none>           <none>
javademo1-69f7678847-nsnbc   1/1     Running   0          3m12s   10.244.3.142   zjj103   <none>           <none>
javademo1-69f7678847-qf5gf   1/1     Running   0          17m     10.244.3.141   zjj103   <none>           <none>

Service exposes applications

When Service exposes ports to the outside world, it actually has a load balancing effect.

kubectl expose deployment [deployment name]–port=[port provided externally] --target-port=[internal port] --type=NodePort

Description:

– Port is the port provided externally – target
-port is the internal port,
–type=NodePort is exposed

Note that the port should not be scribbled, it must be matched with your project port, otherwise it will not be accessible. For example, if your original project port is 8080, then this must write port 8080, otherwise, you will not be able to access it.

[root@zjj101 k8s]# kubectl  expose deployment javademo1 --port=8080  --target-port=8080 --type=NodePort
service/javademo1 exposed

View the exposed ports

kubectl get svc

[root@zjj101 k8s]# kubectl  get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
javademo1    NodePort    10.105.149.218   <none>        8080:31420/TCP   5s
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          46h
[root@zjj101 k8s]#

You can visit

Check that javademo1 is port 31420 externally, then you can use port 31420 of any machine in the cluster to access it

During the visit, you will be equally divided among multiple copies of this pod

image-20210228175354234

View docker log

kubectl get pod -o wide javademo1

[root@zjj101 k8s]# kubectl get pod -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP             NODE     NOMINATED NODE   READINESS GATES
javademo1-69f7678847-68z2w   1/1     Running   0          23m   10.244.3.143   zjj103   <none>           <none>
javademo1-69f7678847-nsnbc   1/1     Running   0          23m   10.244.3.142   zjj103   <none>           <none>
javademo1-69f7678847-qf5gf   1/1     Running   0          37m   10.244.3.141   zjj103   <none>           <none>

Found that the nodes are all on the zjj103 machine

Using docker ps -a on the zjj103 machine, I found that three docker machines were deployed on this machine

[root@zjj103 ~]# docker ps -a
CONTAINER ID        IMAGE                                                 COMMAND                  CREATED             STATUS                        PORTS               NAMES
ea2786f7e715        3e63062a76cc                                          "java -Djava.securit…"   16 minutes ago      Up 15 minutes                                     k8s_zjj101_javademo1-69f7678847-68z2w_default_eec5b2d6-93d0-4cbd-9002-269c989b4010_0
e191795d4c3b        3e63062a76cc                                          "java -Djava.securit…"   16 minutes ago      Up 15 minutes                                     k8s_zjj101_javademo1-69f7678847-nsnbc_default_a3370b98-6db4-4693-9350-832e2490175b_0
7cd16dd0c99a        registry.cn-beijing.aliyuncs.com/zjjmkmkksjm/zjj101   "java -Djava.securit…"   23 minutes ago      Up 23 minutes                                     k8s_zjj101_javademo1-69f7678847-qf5gf_default_4fb122ba-ae81-4153-8f5e-a22012e58a30_0

Use docker logs ea2786f7e715 and docker logs 7cd16dd0c99a and docker logs e191795d4c3b to view the running logs of tomcat

Guess you like

Origin blog.csdn.net/qq_41489540/article/details/114252763