zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    alpha冲刺3
    alpha冲刺2
    alpha冲刺1
    软工第七次作业
    软工第八次作业
    软工第六次作业
    软工第五次作业
    软工第四次作业
    Alpha冲刺一 (2/10)
    Alpha冲刺一(1/10)
  • 原文地址:https://www.cnblogs.com/sky-cheng/p/14236895.html
Copyright © 2011-2022 走看看