1. 程式人生 > >K8s部署docker化Java應用

K8s部署docker化Java應用

Docker

下面以SpringBoot工程為例

  • 首先建立一個SpringBoot工程,工程目錄結構如下:
> mt-myservice
    > .idea
    > .mvn
    > src
        > main
            > docker
                  Dockerfile         // Dockerfile(名字是固定的)為工程docker的配置
        > java
            > com.my
                > controller         // web應用入口,controller控制器
                > service            // 業務邏輯類             
                  MyserviceApplication   // springBoot應用啟動類
    > resources
          application.properties     // springBoot配置類,tomcat預設埠號可在這裡修改
  • 上述新增docker配置檔案Dockerfile,目錄結構設定為src/main/docker,內容如下
#基礎映象環境
FROM java:8  
VOLUME /tmp  
#把工程生成的jar包新增到容器並改名為app.jar
ADD myservice-0.0.1-SNAPSHOT.jar app.jar 
#命令入口,其中-Djava.security.egd=file:/dev/./urando是生成隨機數,urandom節約構建熵池時間
#設定應用最大記憶體為1000M
ENTRYPOINT ["java","-Xmx1000m","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  • pom中配置maven外掛,配置如下
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.2.3</version>
                <configuration>
                    <imageName>demo/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 指定tomcat埠號,以springboot為例,在classpath路徑下修改application.properties檔案,內容:
    server.port=<要修改的埠號>
  • 啟動docker(不啟動下面構建會失敗)
  • 執行maven命令:package docker:build(執行成功後會生成docker映象在本地倉庫)
  • 在docker命令列執行命令
    • docker images(檢視本地映象)
    • docker login(登入自己的docker賬號,沒有可以註冊下)
    • docker tag [ImageId]  demo/myservice:[映象版本號]   //修改版本號
    • docker push <username>/<tag>(推送映象到遠端倉庫)

常見問題:

  1. docker啟動失敗,可能是計算機沒有設定允許裝VM選項,可到bios進行更改
  2. 建立VM失敗,可能是VirtualBox版本問題,可以解除安裝VirtualBox重新裝

k8s部署service,yaml檔案配置:

配置service資訊:
api:/api/v1/namespaces/{mynamespace}/services/
請求方式:post
{
    "metadata":{
        "name":"myservice",
        "namespace":"mynamespace",
        "labels":{
            "app":"myservice"
        }
    },
    "apiVersion":"v1",
    "kind":"Service",
    "spec":{
        "selector":{
            "app":"myservice"
        },
        "ports":[
            {
                "protocol":"TCP",
                "port":80,
                "targetPort":8080
            }
        ],
        "clusterIP":"None"
    }
}
注意:targetPort一定要和專案啟動的埠號一致。


配置Ingress資訊(相當於nginx):
api:/apis/extensions/v1beta1/namespaces/{mynamesapce}/ingresses/
請求方式:post
{
    "metadata":{
        "name":"myservice",
        "namespace":"mynamesapce"
    },
    "apiVersion":"extensions/v1beta1",
    "kind":"Ingress",
    "spec":{
        "rules":[
            {
                "host":"myservice.mtdata.bach.com",
                "http":{
                    "paths":[
                        {
                            "path":"/",
                            "backend":{
                                "servicePort":80,
                                "serviceName":"myservice"
                            }
                        }
                    ]
                }
            }
        ]
    }
}


配置deployment,定義Deployment來建立Pod和ReplicaSet
api:/apis/extensions/v1beta1/namespaces/{mynamespace}/deployments/
請求方式:post
{
    "metadata":{
        "name":"myservice",
        "namespace":"mynamespace"
    },
    "apiVersion":"extensions/v1beta1",
    "kind":"Deployment",
    "spec":{
        "template":{
            "metadata":{
                "labels":{
                    "app":"myservice"
                }
            },
            "spec":{
                "hostNetwork":false,
                "containers":[
                    {
                        "image":"docker映象路徑:版本號",
                        "imagePullPolicy":"Always",
                        "name":"subtitle",
                        "resources":{
                            "requests":{
                                "memory":"1024Mi",
                                "cpu":4
                            },
                            "limits":{  
                                "memory":"2048Mi",         //記憶體最大限制
                                "cpu":4                    //cpu核心數
                            }
                        },
                        "readinessProbe":{
                            "tcpSocket":{
                                "port":8080
                            },
                            "periodSeconds":10,
                            "initialDelaySeconds":20
                        }
                    }
                ],
                "affinity":{
                    "nodeAffinity":{
                        "requiredDuringSchedulingIgnoredDuringExecution":{
                            "nodeSelectorTerms":[
                                {
                                    "matchExpressions":[
                                        {
                                            "key":"gpu",
                                            "operator":"DoesNotExist"
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        },
        "replicas":1
    }
}

image升級時候,需要修改deployment,請求方式為patch

api:/apis/extensions/v1beta1/namespaces/mynamespace/deployments/myservice

Content-Type:application/strategic-merge-patch+json


{
    "spec":{
        "template":{
            "spec":{
                "containers":[
                    {
                        "image":"docker映象路徑:版本號",
                        "name":"myservice"
                    }
                ]
            }
        }
    }
}

k8s api文件:http://kubernetes.kansea.com/docs/api-reference/v1/operations/
k8s 引數說明文件:http://kubernetes.kansea.com/docs/api-reference/v1/definitions/#_v1_event