kubectl version
Client Version: v1.28.2

1、创建pv和pvc

# 若有需要挂载目录,则可以创建pv、pvc
# 存储:
	emptyDir:据不会被持久化,它会随着Pod的结束而销毁。
	hostPath:将Node主机中一个实际目录挂在到Pod中,以供容器使用,这样的设计就可以保证Pod销毁了,但是数据依据可以存在于Node主机上。(数据不共享)
	nfs:无论Pod在节点上怎么转移,只要Node跟NFS的对接没问题,数据就可以成功访问。(数据共享)
# 此处选择的是数据共享的nfs方式。	
kubectl apply -f pv-pvc.yaml
# pv-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: accumulation-pv
  labels:
    type: accumulation-logs
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: manual
  nfs:
    path: "/data/java/java-logs/accumulation/"
    server: 192.168.255.142
    readOnly: false

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: accumulation-pvc
  namespace: dev
  labels:
    type: accumulation-logs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual

2、创建Deployment

# 先将java项目的docker image 上传至仓库
# 没有docker镜像仓库的可以参考教程 https://blog.csdn.net/qq_41076892/article/details/139692963
kubectl apply -f accumulation.yaml
# accumulation.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: accumulation
  namespace: dev
spec:
  replicas: 2
  selector:
    matchLabels:
      app: accumulation
  template:
    metadata:
      labels:
        app: accumulation
    spec:
      securityContext:
        runAsUser: 0
        fsGroup: 0
      containers:
        - name: accumulation
          image: 192.168.255.132/library/java-accumulation:202406120420
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              memory: "512Mi"
            limits:
              memory: "1024Mi"
          ports:
            - name: rest
              containerPort: 8077
              protocol: TCP
          volumeMounts:
            - name: java-logs
              mountPath: /java-logs/accumulation/
          env:
            - name: REDIS_PORT
              value: "6379"
      volumes:
        - name: java-logs
          persistentVolumeClaim:
            claimName: accumulation-pvc

3、创建service和ingress

kubectl apply -f accumulation-svc-ingress.yaml
# accumulation-svc-ingress.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: accumulation-svc
  namespace: dev
spec:
  selector:
    app: accumulation
  ports:
    - protocol: TCP
      port: 8077
      targetPort: 8077
  clusterIP: None
  type: ClusterIP
  
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: accumulation-ingress
  namespace: dev
spec:
  ingressClassName: nginx
  rules:
    - host: tomcat.demo.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: accumulation-svc
                port:
                  number: 8077

验证

[root@k8s-master vue]# kubectl get ingress -n dev
NAME                   CLASS   HOSTS                                      ADDRESS           PORTS   AGE
accumulation-ingress   nginx   nginx.demo.com,tomcat.demo.com             192.168.255.143   80      2d2h
# 在Windows系统的hosts文件添加
	192.168.255.143 tomcat.demo.com  nginx.demo.com
	
#	nginx.demo.com是前端项目的域名,tomcat.demo.com是后端项目的域名。
vue访问后端项目,
    使用内部域名方式:   
        {serviceName}.{namespace}.svc.cluster.local:{port}
        例如:http://accumulation-svc.dev.svc.cluster.local:8077
在不同环境:
    使用ingress方式:
        http://tomcat.demo.com

此时k8s集群中的vue项目、java项目正常启动,在浏览器上能够正常访问。
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐