k8s:: Service 管理 deployment
Service 管理 deployment

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

1.1 構建service綁定deployment,可以供外部客戶端訪問
kubectl expose deployment nginx-deployment --port=8888 --target-port=80 -n test1 --type=NodePort

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)。

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加上域名配置,客戶端就能使用域名訪問

浙公網安備 33010602011771號