开发者

springboot项目部署到k8s上的方法步骤

开发者 https://www.devze.com 2023-05-05 11:08 出处:网络 作者: 越走越远的风
目录springboot部署到k8s步骤创建secret创建secret创建deployment的yaml文件创建deployment创建service, nodePortspringboot部署到k8s步骤
目录
  • springboot部署到k8s步骤
  • 创建secret
    • 创建secret
    • 创建deployment的yaml文件
    • 创建deployment
    • 创建service, nodePort

springboot部署到k8s步骤

  • springboot项目打包镜像部署到镜像仓库

  • 登录私有镜像仓库,拉去镜像

  • 创建deployment

  • 暴露服务访问端口

上篇文章已讲过 springboot构建docker镜像并推送到阿里云

创建secretjs

登录私有仓库需要创建secret,存储docker regist编程客栈ry的认证信息

创建secret

~$ kubectl create secret docker-registry fdf-docker-secret --docker-server=registry.cn-chengdu.aliyuncs.com --docker-username=17602117026 --docker-password=用户密码
secret/fdf-docker-secret created
~$
~$
~$ kubectl get secret
NAME                TYPE                             DATA   AGE
fdf-docker-secret   kubernetes.io/dockerconfigjson   1      15s

创建好之后,后续需要挂载到pod上

创建deployment的yaml文件

快速创建一个deployment,导出yaml文件

~$ kubectl create deployment k8sdemo --image=registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0 --dry-run=client -o yaml > k8sdemo.yaml
~$ cat k8sdemo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTi开发者_Python培训mestamp: null
  labels:
    app: k8sdemo
  name: k8sdemo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8sdemo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: k8sdemo
    spec:
      containers:
      - image: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
        name: k8s-test
        resources: {}
status: {}
~$

修改k8sdemo.yaml文件

#1.修改副本数量为2 
#2.挂在secret
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: k8sdemo
  name: k8sdemo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: k8sdemo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: k8sdemo
    spec:
      imagePullSecrets:
        - njavascriptame: fdf-docker-secret
      containers:
      - image: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
        name: k8s-test
        resources: {}
status: {}

创建deployment

~$ kubectl apply -f k8sdemo.yaml
deployment.apps/k8sdemo created
~$ kubectl get deploy
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
k8sdemo   0/2     2            0           12s
~$ kubectl get deploy
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
k8sdemo   2/2     2            2           24s
~$ kubectl get pod
NAME                       READY   STATUS    RESTARTS   AGE
k8sdemo-65d45fb49f-l4kx5   1/1     Running   0          28s
k8sdemo-65d45fb49f-pqsjw   1/1     Running   0          28s

创建service, nodePort

#port 服务端口    target-port pod端口
~$ kubectl expose deploy k8sdemo --port=9001 --target-port=9001 --type=NodePort
service/k8sdemo exposed
~$
~$ kubectl get svc k8sdemo
NAME      TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
k8sdemo   NodePort   10.96.10.79   <none>        9001:31921/TCP   12s
~$ curl http://127.0.0.1:31921/api/v1/user/name
zhangsan                                                                                                                            

可以看到serviced的ip为10.96.10.79 对外端口为 31http://www.devze.com921,安全组需开放该端口才能访问

到这里我们服务部署就算完成了,看一下所有的节点

~$ kubectl get secret,pod,svc,deploy
NAME                       TYPE                             DATA   AGE
secret/fdf-docker-secret   kubernetes.io/dockerconfigjson   1      52m
NAME                           READY   STATUS    RESTARTS   AGE
pod/k8sdemo-65d45fb49f-l4kx5   1/1     Running   0          39m
pod/k8sdemo-65d45fb49f-pqsjw   1/1     Running   0          39m
NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
service/k8sdemo      NodePort    10.96.10.79   <none>        9001:31921/TCP   5m51s
service/kubernetes   ClusterIP   10.96.0.1     <none>        4http://www.devze.com43/TCP          301d
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/k8sdemo   2/2     2            2           39m
~$

到此这篇关于springboot项目部署到k8s上的方法步骤的文章就介绍到这了,更多相关springboot部署到k8s内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号