K8s云原生Java开发

helm 部署一个测试项目(2 pods) 到k8s (Dock

2025-08-09  本文已影响0人  _浅墨_

1、在 Kubernetes 部署一个测试项目(2 个 Pods 副本)

那我给你一个超简化版 Helm Chart,直接能在 Kubernetes 部署一个测试项目(2 个 Pods 副本),你在 Mac Docker Desktop 的 K8s 或 Minikube 上都可以跑。

1️⃣ 创建 Chart

helm create test-app

生成目录:

test-app/
  Chart.yaml
  values.yaml
  templates/

2️⃣ 编辑 Chart.yaml

apiVersion: v2
name: test-app
description: A simple test app deployment
version: 0.1.0
appVersion: "1.0"

3️⃣ 修改 values.yaml(核心配置)

replicaCount: 2

image:
  repository: nginx   # 测试项目用 nginx 镜像
  tag: latest
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

4️⃣ 修改 templates/deployment.yaml

找到 templates/deployment.yaml,简化成:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  labels:
    app: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: 80

5️⃣ 修改 templates/service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}
  labels:
    app: {{ .Chart.Name }}
spec:
  type: {{ .Values.service.type }}
  selector:
    app: {{ .Chart.Name }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: 80

6️⃣ 部署到 K8s

helm install test-app ./test-app

7️⃣ 验证

kubectl get pods
kubectl get svc

你会看到:

NAME                           READY   STATUS    RESTARTS   AGE
test-app-xxxxxx                1/1     Running   0          10s
test-app-yyyyyy                1/1     Running   0          10s

8️⃣ 访问测试服务(端口转发)

kubectl port-forward svc/test-app 8080:80

然后浏览器访问:

http://localhost:8080

你就能看到 Nginx 欢迎页面。

2、Helm Chart 里加 Ingress 配置,这样直接用域名访问

好,那我直接帮你把 Ingress 配进 Helm Chart,部署完就能通过域名访问(比如 http://test.local)。

1️⃣ 前提

2️⃣ 更新 values.yaml

replicaCount: 2

image:
  repository: nginx
  tag: latest
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: true
  className: nginx
  annotations: {}
  hosts:
    - host: test.local
      paths:
        - path: /
          pathType: Prefix
  tls: []

3️⃣ 新建 templates/ingress.yaml

{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Release.Name }}
  labels:
    app: {{ .Chart.Name }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  ingressClassName: {{ .Values.ingress.className }}
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ .path }}
            pathType: {{ .pathType }}
            backend:
              service:
                name: {{ $.Release.Name }}
                port:
                  number: {{ $.Values.service.port }}
          {{- end }}
    {{- end }}
  {{- if .Values.ingress.tls }}
  tls:
    {{- toYaml .Values.ingress.tls | nindent 4 }}
  {{- end }}
{{- end }}

4️⃣ 部署

helm install test-app ./test-app

5️⃣ 验证

kubectl get pods
kubectl get ingress

输出示例:

NAME              CLASS   HOSTS       ADDRESS         PORTS   AGE
test-app          nginx   test.local  192.168.65.2    80      5s

6️⃣ 访问

浏览器直接打开:

http://test.local

你会看到 Nginx 欢迎页面 🎉

上一篇 下一篇

猜你喜欢

热点阅读