1. 程式人生 > 其它 >kubernetes部署一個springboot應用

kubernetes部署一個springboot應用

一、首先製作jdk環境映象

參考:https://www.cnblogs.com/sky-cheng/p/14215332.html

二、製作jar包映象

參考:https://www.cnblogs.com/sky-cheng/p/14218164.html

三、在k8s的master節點上建立專案名稱空間

[root@master69 kubernetes]# mkdir hl95-notary
[root@master69 kubernetes]# vim hl95-notary/hl95-notary-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: hl95
-notary

建立

[root@master69 kubernetes]# kubectl create  -f hl95-notary/hl95-notary-namespace.yaml          
namespace/hl95-notary created
[root@master69 kubernetes]# 

檢視namespace

[root@master69 kubernetes]# kubectl get ns
NAME              STATUS   AGE
default           Active   27h
hl95-notary       Active   36s
kube
-node-lease Active 27h kube-public Active 27h kube-system Active 27h

四、建立deployment

在master節點上編輯hl95-notary-api-deployment.yaml 檔案

[root@master69 kubernetes]# vim hl95-notary/hl95-notary-api-deployment.yaml               
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hl95
-notary-api-deployment namespace: hl95-notary spec: replicas: 2 selector: matchLabels: project: hl95-notary app: hl95-notary-api template: metadata: labels: project: hl95-notary app: hl95-notary-api spec: containers: - name: hl95-notary-api image: skychengp/hl95-blockchain-notary-api:1.0.0 workingDir: /home/blockchain/http-api command: [ "java" ]
args:
- '-jar'
- 'hl95-blockchain-notary-api-0.0.1-SNAPSHOT.jar' ports:
- containerPort: 8079 volumeMounts: - name: logs mountPath: /home/blockchain/http-api/logs #掛載的容器內目錄 volumes: - name: logs #掛載宿主機目錄 hostPath: path: /home/blockchain/http-api/logs

建立

[root@master69 kubernetes]# kubectl create -f hl95-notary/hl95-notary-api-deployment.yaml 
deployment.apps/hl95-notary-api-deployment created

檢視

[root@master69 kubernetes]# kubectl get deployment -n hl95-notary
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
hl95-notary-api-deployment   0/2     2            0           64s

ready是0,檢視pods

[root@master69 kubernetes]# kubectl get pods -n hl95-notary
NAME                                          READY   STATUS             RESTARTS   AGE
hl95-notary-api-deployment-75f86c74cf-2rgxf   0/1     ImagePullBackOff   0          107s
hl95-notary-api-deployment-75f86c74cf-gltxn   0/1     ImagePullBackOff   0          107s

status為ImagePullOff:映象拉取失敗

檢視問題詳情

[root@master69 kubernetes]# kubectl describe pods hl95-notary-api-deployment-75f86c74cf-2rgxf -n  hl95-notary
Error from server (NotFound): namespaces "hl95-notary-api-deployment-75f86c74cf-2rgxf" not found
[root@master69 kubernetes]# kubectl describe pods hl95-notary-api-deployment-75f86c74cf-2rgxf -n  hl95-notary               
Name:         hl95-notary-api-deployment-75f86c74cf-2rgxf
Namespace:    hl95-notary
Priority:     0
Node:         redis-02.hlqxt/172.28.5.124
Start Time:   Tue, 05 Jan 2021 15:49:31 +0800
Labels:       app=hl95-notary-api
              pod-template-hash=75f86c74cf
              project=hl95-notary
Annotations:  <none>
Status:       Pending
IP:           10.244.4.5
IPs:
  IP:           10.244.4.5
Controlled By:  ReplicaSet/hl95-notary-api-deployment-75f86c74cf
Containers:
  hl95-notary-api:
    Container ID:  
    Image:         skychengp/hl95-blockchain-notary-api:1.0.0
    Image ID:      
    Port:          8079/TCP
    Host Port:     0/TCP
    Command:
      java -jar hl95-blockchain-notary-api-0.0.1-SNAPSHOT.jar
    State:          Waiting
      Reason:       ImagePullBackOff
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /home/blockchain/http-api/logs from logs (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-td92j (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  logs:
    Type:          HostPath (bare host directory volume)
    Path:          /home/blockchain/http-api/logs
    HostPathType:  
  default-token-td92j:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-td92j
    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  4m27s                 default-scheduler        Successfully assigned hl95-notary/hl95-notary-api-deployment-75f86c74cf-2rgxf to redis-02.hlqxt
  Normal   Pulling    106s (x4 over 4m25s)  kubelet, redis-02.hlqxt  Pulling image "skychengp/hl95-blockchain-notary-api:1.0.0"
  Warning  Failed     87s (x4 over 4m6s)    kubelet, redis-02.hlqxt  Failed to pull image "skychengp/hl95-blockchain-notary-api:1.0.0": rpc error: code = Unknown desc = Error response from daemon: pull access denied for skychengp/hl95-blockchain-notary-api, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  Warning  Failed     87s (x4 over 4m6s)    kubelet, redis-02.hlqxt  Error: ErrImagePull
  Warning  Failed     62s (x6 over 4m6s)    kubelet, redis-02.hlqxt  Error: ImagePullBackOff
  Normal   BackOff    51s (x7 over 4m6s)    kubelet, redis-02.hlqxt  Back-off pulling image "skychengp/hl95-blockchain-notary-api:1.0.0"

從Events裡可以看到詳細的事件資訊,提示映象不存在,需要將master節點生成的映象檔案複製到node節點上,或者從私有映象倉庫中拉取,前提是先推送到倉庫中

將master節點映象匯出tar包

[root@master69 kubernetes]# docker save -o hl95-notary-api.tar skychengp/hl95-blockchain-notary-api:1.0.0

將tar包複製到node節點上

[root@master69 kubernetes]# scp -P25601 hl95-notary-api.tar root@172.28.5.120:/etc/kubernetes/

在node節點匯入映象

[root@redis-01 kubernetes]# docker load -i hl95-notary-api.tar 

檢視映象

[root@redis-01 kubernetes]# docker images|grep hl95            
skychengp/hl95-blockchain-notary-api                                                                                                      1.0.0               2c07e234fc34        2 hours ago         660MB

刪除剛才失敗的deployment

[root@master69 kubernetes]# kubectl delete deployment hl95-notary-api-deployment -n hl95-notary
deployment.apps "hl95-notary-api-deployment" deleted

再次檢視deployment和pods已經刪除

[root@master69 kubernetes]# kubectl get deployment -n hl95-notary               
No resources found in hl95-notary namespace.
[root@master69 kubernetes]# kubectl get pods -n hl95-notary
No resources found in hl95-notary namespace.

再次建立

[root@master69 kubernetes]# kubectl create -f hl95-notary/hl95-notary-api-deployment.yaml 
deployment.apps/hl95-notary-api-deployment created

檢視pods

[root@master69 kubernetes]# kubectl get pods -n hl95-notary
NAME                                          READY   STATUS    RESTARTS   AGE
hl95-notary-api-deployment-79f9559498-bxchq   1/1     Running   0          12s
hl95-notary-api-deployment-79f9559498-p869k   1/1     Running   0          12s
[root@master69 kubernetes]# 

running成功,分別在兩個node節點上建立了pod

五、建立service

[root@master69 kubernetes]# vim hl95-notary/hl95-notary-api-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: hl95-notary-api-service
  namespace: hl95-notary
spec:
  selector:
    project: hl95-notary
    app: hl95-notary-api
  ports:
    - name: http-api
      protocol: TCP
      port: 8079
      targetPort: 8079
      nodePort: 8079
  type: NodePort

建立

[root@master69 kubernetes]# kubectl create -f hl95-notary/hl95-notary-api-service.yaml 
The Service "hl95-notary-api-service" is invalid: spec.ports[0].nodePort: Invalid value: 8079: provided port is not in the valid range. The range of valid ports is 30000-32767

報錯無效埠,預設是30000-32767,需要修改kube-apiserver.yaml檔案

[root@master69 kubernetes]# vim /etc/kubernetes/manifests/kube-apiserver.yaml 

在--service-cluster-ip-range=10.96.0.0/12下面加一行

    - --service-node-port-range=1-65535

儲存退出,並重啟kubelet服務

[root@master69 kubernetes]# systemctl restart kubelet

再次建立service

[root@master69 kubernetes]# kubectl create -f hl95-notary/hl95-notary-api-service.yaml 
service/hl95-notary-api-service created

檢視

[root@master69 kubernetes]# kubectl get svc -n hl95-notary
NAME                      TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)         AGE
hl95-notary-api-service   NodePort   10.110.180.240   <none>        8079:8079/TCP   25s

訪問http://masterip:8079

六、檢視pod日誌

[root@master69 kubernetes]# kubectl get pods -n hl95-notary -o wide                    
NAME                                          READY   STATUS    RESTARTS   AGE     IP            NODE             NOMINATED NODE   READINESS GATES
hl95-notary-api-deployment-79f9559498-gmrr8   1/1     Running   0          3m29s   10.244.3.12   redis-03.hlqxt   <none>           <none>
hl95-notary-api-deployment-79f9559498-j8qrg   1/1     Running   0          3m29s   10.244.3.13   redis-03.hlqxt   <none>           <none>[root@master69 kubernetes]# kubectl  logs -f hl95-notary-api-deployment-79f9559498-gmrr8 -n hl95-notary