k8s:: Service 管理 deployment

Service 管理 deployment

image


1.0 构建service绑定deployment

/* 在service层创建端口8888,转发到test1命名空间pod的80端口 */
kubectl  expose deployment nginx-deployment --port=8888 --target-port=80 -n test1
/* 删除service层 */
kubectl delete service nginx-deployment -n test1

image

1.1 构建service绑定deployment,可以供外部客户端访问

kubectl  expose deployment nginx-deployment --port=8888 --target-port=80 -n test1 --type=NodePort

image

1.3 yml文件方式创建

# metadata层信息要一致 
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test1
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  namespace: test1
  name: nginx-deployment
  labels:
    app: nginx-deployment
spec:
  selector:
    app: nginx
  ports:
  - port: 8888
    targetPort: 80
  type: NodePort

一般不会直接使用Service 管理 deployment,而是使用ingress对象。


Ingress

Ingress 是 Kubernetes 的一种 API 对象。
将集群内部的 Service 通过 HTTP/HTTPS 方式暴露到集群外部,并通过规则定义 HTTP/HTTPS 的路由。
Ingress 具备如下特性:集群外部可访问的 URL、负载均衡、SSL Termination、按域名路由(name-based virtual hosting)。
image

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test1
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---

apiVersion: v1
kind: Service
metadata:
  namespace: test1
  name: nginx-deployment
  labels:
    app: nginx-deployment
spec:
  selector:
    app: nginx
  ports:
  - port: 8888
    targetPort: 80
  type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: test1
  name: nginx-ingress
spec:
  ingressClassName: ingress
  rules:
  - host: ingress.nginx.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-deployment
            port:
              number: 8888

win下hosts加上域名配置,客户端就能使用域名访问

image

posted @   osbreak  阅读(50)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示