K8S Service-NodePort:固定端口

Service-NodePort:固定端口

两个节点

192.168.81.141 k8s-master

192.168.81.140 k8s-node02

1、service-demo1.yaml定义yaml文件


# -----------------------------------
# 定义Service
# ----------------------------------

apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  type: NodePort
  selector:
    app: myapp
  # Service 能够将任意入站 port 映射到某个 targetPort。 默认情况下,出于方便考虑,targetPort 会被设置为与 port 字段相同的值。
  
  # 当在Kubernetes集群中创建多个Service时,确保每个Service使用的NodePort都是唯一的。

  # 使用集群内任意一台nodeIP:nodePort都可以访问到service服务
  ports:
  - port: 18080
    targetPort: 18080
    nodePort: 30001
  
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  # 副本数
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      imagePullSecrets:
      - name: kevin-registry-secret
      containers:
      - name: myapp
        image: 192.168.81.141:45678/library/myapp:v1
        ports:
        - containerPort: 18080

2、启动Pod

kubectl apply -f service-demo1.yaml

3、查看对应的SVC,Pod

[root@k8s-master workspace]# kubectl get pod -A -o wide | grep myapp
default                myapp-78794f6bf4-vhpmx                       1/1     Running   0               66m     10.244.235.235   k8s-master   <none>           <none>
default                myapp-78794f6bf4-zq7w6                       1/1     Running   0               66m     10.244.58.218    k8s-node02   <none>           <none>
[root@k8s-master workspace]# kubectl get svc -A -o wide | grep myapp
default                myapp                       NodePort    10.105.194.242   <none>        18080:30001/TCP          64m     app=myapp
[root@k8s-master workspace]# 
[root@k8s-master workspace]# curl 192.168.81.140:30001/ping
ok[root@k8s-master workspace]curl 192.168.81.141:30001/ping
ok[root@k8s-master workspace]#

可以看出来,通过nodeIP:nodePort

4、Nginx配置,

upstream  myapp {
    #server   10.105.194.242:18080 max_fails=3  fail_timeout=30s;
    
    server   192.168.81.141:30001 max_fails=3  fail_timeout=30s;
    server   192.168.81.140:30001 max_fails=3  fail_timeout=30s;
}


server {
       listen 28080;
       server_name localhost;
       location / {
            proxy_pass  http://myapp;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 25s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;
      }
}
--- apiVersion: apps/v1 kind: Deployment metadata: annotations: k8s.kuboard.cn/workload: tap-web labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: tap-web name: tap-web namespace: cat-service resourceVersion: '68153899' spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: tap-web strategy: rollingUpdate: maxSurge: 2 maxUnavailable: 5 type: RollingUpdate template: metadata: annotations: kubectl.kubernetes.io/restartedAt: '2025-06-10T16:31:54+08:00' creationTimestamp: null labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: tap-web spec: containers: - envFrom: - configMapRef: name: com-config image: 'easzlab.io.local:5000/cta/prod/tap-web:0.17.2' imagePullPolicy: IfNotPresent name: tap-web resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst nodeSelector: tap/normal: tap-normal restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 --- apiVersion: v1 kind: Service metadata: annotations: {} labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: tap-web name: tap-web namespace: cat-service resourceVersion: '26804268' spec: clusterIP: 10.68.225.40 clusterIPs: - 10.68.225.40 externalTrafficPolicy: Cluster internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: teefzx nodePort: 30114 port: 80 protocol: TCP targetPort: 80 selector: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: tap-web sessionAffinity: None type: NodePort
最新发布
06-12
### Kubernetes Deployment 和 Service 配置示例 以下是一个包含 `tap-web` 服务Kubernetes Deployment 和 Service 的 YAML 文件结构及其参数说明。 #### 1. Deployment 配置示例 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: tap-web-deployment namespace: default spec: replicas: 3 selector: matchLabels: app: tap-web template: metadata: labels: app: tap-web spec: containers: - name: tap-web image: nginx:latest ports: - containerPort: 80 resources: limits: cpu: "500m" memory: "512Mi" requests: cpu: "250m" memory: "256Mi" livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 10 readinessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 10 ``` - **apiVersion**: 定义 API 版本,这里是 `apps/v1`[^4]。 - **kind**: 定义资源类型,这里是 `Deployment`。 - **metadata**: 包含名称和命名空间等元数据信息。 - **spec**: 描述 Deployment 的详细配置。 - **replicas**: 指定副本数为 3[^4]。 - **selector**: 定义如何找到关联的 Pod,通过 `matchLabels` 匹配标签。 - **template**: 定义 Pod 的模板。 - **metadata.labels**: 为 Pod 添加标签。 - **spec.containers**: 定义容器的详细信息。 - **name**: 容器名称为 `tap-web`。 - **image**: 使用的镜像为 `nginx:latest`。 - **ports**: 容器暴露的端口为 80。 - **resources**: 定义资源限制和请求。 - **livenessProbe** 和 **readinessProbe**: 定义健康检查和就绪检查。 #### 2. Service 配置示例 ```yaml apiVersion: v1 kind: Service metadata: name: tap-web-service namespace: default spec: selector: app: tap-web ports: - protocol: TCP port: 80 targetPort: 80 type: ClusterIP ``` - **apiVersion**: 定义 API 版本,这里是 `v1`[^2]。 - **kind**: 定义资源类型,这里是 `Service`。 - **metadata**: 包含名称和命名空间等元数据信息。 - **spec**: 描述 Service 的详细配置。 - **selector**: 通过标签选择器匹配后端 Pod,这里匹配标签为 `app: tap-web`。 - **ports**: 定义服务的端口映射。 - **protocol**: 协议类型为 TCP。 - **port**: 服务暴露的端口为 80。 - **targetPort**: 转发到 Pod 的端口为 80。 - **type**: 定义服务类型为 `ClusterIP`[^2]。 #### 参数说明 - **Deployment**: - `replicas`: 确保有多个 Pod 副本运行以提高可用性。 - `livenessProbe` 和 `readinessProbe`: 确保 Pod 始终处于健康状态并可接受流量。 - `resources`: 控制 Pod 的资源使用,避免资源耗尽或浪费。 - **Service**: - `selector`: 将流量路由到具有指定标签的 Pod。 - `type`: `ClusterIP` 是默认的服务类型,仅在集群内部可见。 ### 示例代码验证 可以使用以下命令生成 JSON 报告以验证配置文件的有效性: ```bash kubeval --output=json deployment.yaml > kubeval-report.json kube-score score -o json deployment.yaml > kube-score-report.json ``` 这将生成两个 JSON 文件,分别用于验证配置文件的语法和最佳实践[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半山猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值