kubernates之部署springboot

    最近開始接觸kubernates,在上面部署了下springboot,記錄下。java

1、建立一個Springboot應用

    List-1.1node

import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @RequestMapping(value = "/")
        public String hello(){
            System.out.println("hello方法");
            return "Hello, k8s!";
        }
    }

    以後mvn clean package,打包,獲得app.jar,和Dockerfile放在同一個目錄下web

    List-1.2spring

mjduan@mjduan-ubuntu:/opt/software/docker/springboot-k8s$ ll
    total 676796
    drwxr-xr-x 2 mjduan mjduan      4096 2月  15 15:43 ./
    drwxr-xr-x 4 mjduan mjduan      4096 2月  15 15:30 ../
    -rw-r--r-- 1 mjduan mjduan  16674428 2月  15 15:31 app.jar
    -rw-r--r-- 1 mjduan mjduan       362 2月  15 15:37 Dockerfile

2、製做鏡像

 Dockerfile的內容爲docker

    List-2.1ubuntu

FROM openjdk:8
    
    ADD app.jar /opt/app/app.jar
    RUN bash -c 'touch /opt/app/app.jar'
    
    # 開放8080端口
    EXPOSE 8080
    
    ENV TZ=Asia/Shanghai
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    # 配置容器啓動後執行的命令
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/app/app.jar"]

以後執行命令來獲得鏡像api

    List-2.2springboot

mjduan@mjduan-ubuntu:/opt/software/docker/springboot-k8s$ docker build -t hello:1.0 .

三、將該鏡像放到全部的節點上

    以後將這個鏡像放到k8s的master和倆個node上,這裏我是先用bash

docker save hello:1.0>hello.tar

    命令獲得文件hello.tar,以後將hello.tar用scp命令傳到k8s的master和倆個node上。markdown

    以後在k8s的master和倆個node上,執行以下命令獲得鏡像hello:1.0

docker load -i hello.tar

四、kubernates上部署

    好了,上述步驟完成後,在k8s的master上建立yaml文件

[root@localhost mjduan]# vi /data/mjduan/hello.yaml

    hello.yaml的內容以下

apiVersion: apps/v1beta2
    kind: Deployment
    metadata:
      name: example-demo3
      labels:
        app: example-demo3
    spec:
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          app: example-demo3
      template:
        metadata:
          labels:
            app: example-demo3
        spec:
          containers:
          - name: example-demo3
            #image名稱是咱們的hello:1.0
            image: hello:1.0
            ports:
            - containerPort: 8080
              protocol: TCP
            livenessProbe:
              httpGet:
                path: /
                port: 8080
              initialDelaySeconds: 30
              timeoutSeconds: 30
            imagePullPolicy: Never
          # Comment the following tolerations if Dashboard must not be deployed on master
          tolerations:
          - key: node-role.kubernetes.io/master
            effect: NoSchedule

    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: example-demo3
      labels:
        app: example-demo3
    spec:
      ports:
        - port: 8080
          targetPort: 8080
      selector:
        app: example-demo3
      type: NodePort

    部署Docker image,命令以下

[root@localhost mjduan]# kubectl create -f hello.yaml 
    deployment.apps/example-demo3 created
    service/example-demo3 created

    查看pod信息,以下

[root@localhost mjduan]# kubectl get pods
    NAME                             READY     STATUS              RESTARTS   AGE
    example-demo3-746fc684df-h4dpq   1/1       Running             0          10s

    查看service,以下,NAME爲example-demo3的那個就是咱們剛剛部署的,端口映射是宿主機的32300對應到容器的8080

[root@localhost mjduan]# kubectl get services
    NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    example-demo3   NodePort    10.107.189.127   <none>        8080:32300/TCP   1m
    kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP          2h

    訪問服務

[root@localhost mjduan]# curl http://k8s.master:32300
    Hello, k8s!
相關文章
相關標籤/搜索