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 @ 2023-12-24 19:08  osbreak  阅读(49)  评论(0编辑  收藏  举报