首頁  >  文章  >  Java  >  springboot專案怎麼部署到k8s上

springboot專案怎麼部署到k8s上

PHPz
PHPz轉載
2023-05-15 10:04:051407瀏覽

springboot部署到k8s步驟

  • springboot專案打包鏡像部署到鏡像倉庫

  • 登入私有鏡像倉庫,拉去鏡像

  • 建立deployment

  • 暴露服務存取埠

建立secret

登入私有倉庫需要建立secret,儲存docker registry的認證資訊

建立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:
  creationTimestamp: 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:
        - name: 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 對外連接埠為31921,安全群組需開放該連接埠才能存取

到這裡我們服務部署就算完成了,看一下所有的節點

~$ 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>        443/TCP          301d
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/k8sdemo   2/2     2            2           39m
~$

以上是springboot專案怎麼部署到k8s上的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除