互联网科技开发技巧Java 杂谈

我把SpringBoot应用部署到了K8S上,怎么感觉用起来像D

2021-01-28  本文已影响0人  程序员知识圈

想要把一个复杂的微服务项目部署到K8S上去,首先我们得学会把单个SpringBoot应用部署上去。今天我们来讲下如何把SpringBoot应用部署到K8S上去,和使用Docker Compose部署非常类似,希望对大家有所帮助!

推送镜像到Docker Hub

之前我们都是自建的镜像仓库,这次我们换种方式,把镜像上传到Docker Hub中去。

<configuration>
    <!-- Docker 远程管理地址-->
    <dockerHost>http://192.168.5.94:2375</dockerHost>
    <!-- 添加认证信息-->
    <authConfig>
        <push>
            <!--Docker Hub 用户名-->
            <username>macrodocker</username>
            <!--Docker Hub 密码-->
            <password>xxx</password>
        </push>
    </authConfig>
    <images>
        <image>
            <!--修改镜像前缀为Docker Hub 用户名-->
            <name>macrodocker/${project.name}:${project.version}</name>
        </image>
     </images>
 </configuration>

应用部署

接下来我们将把应用部署到K8S上去,包含SpringBoot应用的部署和MySQL的部署。

部署MySQL

apiVersion: apps/v1
kind: Deployment
metadata:
  # 指定Deployment的名称
  name: mysql-deployment
  # 指定Deployment的标签 
  labels:
    app: mysql
spec:
  # 指定创建的Pod副本数量 
  replicas: 1
  # 定义如何查找要管理的Pod
  selector:
    # 管理标签app为mysql的Pod
    matchLabels:
      app: mysql
  # 指定创建Pod的模板
  template:
    metadata:
      # 给Pod打上app:mysql标签
      labels:
        app: mysql
    # Pod的模板规约
    spec:
      containers:
        - name: mysql
          # 指定容器镜像
          image: mysql:5.7
          # 指定开放的端口
          ports:
            - containerPort: 3306
          # 设置环境变量
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
          # 使用存储卷
          volumeMounts:
            # 将存储卷挂载到容器内部路径
            - mountPath: /var/log/mysql
              name: log-volume
            - mountPath: /var/lib/mysql
              name: data-volume
            - mountPath: /etc/mysql
              name: conf-volume
      # 定义存储卷
      volumes:
        - name: log-volume
          # hostPath类型存储卷在宿主机上的路径
          hostPath:
            path: /home/docker/mydata/mysql/log
            # 当目录不存在时创建
            type: DirectoryOrCreate
        - name: data-volume
          hostPath:
            path: /home/docker/mydata/mysql/data
            type: DirectoryOrCreate
        - name: conf-volume
          hostPath:
            path: /home/docker/mydata/mysql/conf
            type: DirectoryOrCreate
kubectl apply -f mysql-deployment.yaml
[macro@linux-local k8s]$ kubectl get deployments
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
mysql-deployment          1/1     1            1           38s
nginx-volume-deployment   2/2     2            2           6d5h
apiVersion: v1
kind: Service
metadata:
  # 定义服务名称,其他Pod可以通过服务名称作为域名进行访问
  name: mysql-service
spec:
  # 指定服务类型,通过Node上的静态端口暴露服务
  type: NodePort
  # 管理标签app为mysql的Pod
  selector:
    app: mysql
  ports:
    - name: http
      protocol: TCP
      port: 3306
      targetPort: 3306
      # Node上的静态端口
      nodePort: 30306
kubectl apply -f mysql-service.yaml 
[macro@linux-local k8s]$ kubectl get services
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP          7d23h
mysql-service   NodePort    10.107.189.51    <none>        3306:30306/TCP   7s
nginx-service   NodePort    10.101.171.181   <none>        80:30080/TCP     6d2h

部署SpringBoot应用

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mall-tiny-fabric-deployment
  labels:
    app: mall-tiny-fabric
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mall-tiny-fabric
  template:
    metadata:
      labels:
        app: mall-tiny-fabric
    spec:
      containers:
        - name: mall-tiny-fabric
          # 指定Docker Hub中的镜像地址
          image: macrodocker/mall-tiny-fabric:0.0.1-SNAPSHOT
          ports:
            - containerPort: 8080
          env:
            # 指定数据库连接地址
            - name: spring.datasource.url
              value: jdbc:mysql://mysql-service:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
            # 指定日志文件路径
            - name: logging.path
              value: /var/logs
          volumeMounts:
            - mountPath: /var/logs
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /home/docker/mydata/app/mall-tiny-fabric/logs
            type: DirectoryOrCreate
kubectl apply -f mall-tiny-fabric-deployment.yaml
[macro@linux-local k8s]$ kubectl get pods
NAME                                           READY   STATUS    RESTARTS   AGE
mall-tiny-fabric-deployment-8684857dff-pnz2t   1/1     Running   0          47s
mysql-deployment-5dccc96ccf-sfxvg              1/1     Running   0          25m
nginx-volume-deployment-6f6c89976d-nv2rn       1/1     Running   4          6d6h
nginx-volume-deployment-6f6c89976d-tmhc5       1/1     Running   4          6d5h
[macro@linux-local k8s]$ kubectl logs -f mall-tiny-fabric-deployment-8684857dff-pnz2t
apiVersion: v1
kind: Service
metadata:
  name: mall-tiny-fabric-service
spec:
  type: NodePort
  selector:
    app: mall-tiny-fabric
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
      # Node上的静态端口
      nodePort: 30180
kubectl apply -f mall-tiny-fabric-service.yaml
[macro@linux-local k8s]$ kubectl get services
NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes                 ClusterIP   10.96.0.1        <none>        443/TCP          7d23h
mall-tiny-fabric-service   NodePort    10.100.112.84    <none>        8080:30180/TCP   5s
mysql-service              NodePort    10.107.189.51    <none>        3306:30306/TCP   13m
nginx-service              NodePort    10.101.171.181   <none>        80:30080/TCP     6d2h
curl $(minikube ip):30180/swagger-ui.html

外部访问应用

由于使用Minikube安装的K8S Node处于Linux服务器的内网环境,无法直接从外部访问,所以我们需要安装一个Nginx反向代理下才能访问。

server {
    listen       80;
    server_name  mall-tiny.macrozheng.com; #修改域名

    location / {
        proxy_set_header Host $host:$server_port;
        proxy_pass   http://192.168.49.2:30180; #修改为代理服务地址
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
192.168.5.94 mall-tiny.macrozheng.com

总结

通过把SpringBoot应用部署到K8S上的一顿操作,我们可以发现在K8S上部署和在Docker上部署有很多相似之处。K8S上很多部署用的脚本,直接翻译之前使用Docker Compose的脚本即可,非常类似。如果你之前用过Docker,那么你就可以轻松上手K8S!

上一篇下一篇

猜你喜欢

热点阅读