05.K8S-Service、Ingress使用
一、Service
1.端口暴露
sudo kubectl expose 服务类型 名称 --port=服务访问端口 --target-port=pod容器的端口 -n 命名空间
sudo kubectl expose deploy my-deploy --port=8000 --target-port=80 -n test
sudo kubectl expose deploy my-deploy --port=8000 --target-port=80 -n test --type=ClusterIP
ClusterIP | 集群内部访问 |
ExternalName | |
LoadBalancer | |
NodePort | 外部访问 |
2.查看服务
sudo kubectl get service -n test
3.删除服务
sudo kubectl delete service my-deploy -n test
4.yaml方式创建
apiVersion: v1
kind: Service
metadata:
labels:
app: my-deploy
name: my-deploy-service
namespace: test
spec:
type: ClusterIP
selector:
app: my-deploy
ports:
- port: 8000
protocol: TCP
targetPort: 80
5.其他pod容器里访问服务
服务内部使用服务名方式
curl 服务名.命名空间.svc:8000
curl my-deploy-service.test.svc:8000
IP方式
curl 10.107.46.33:8000
二、Ingress
Service的统一网络入口
官网查看下载地址:https://kubernetes.github.io/ingress-nginx/deploy/
github查看版本:https://github.com/kubernetes/ingress-nginx/
1.下载yaml文件
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.6.4/deploy/static/provider/cloud/deploy.yaml
2.替换镜像
#替换controller镜像
sed -i "s#registry.k8s.io/ingress-nginx/controller#registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller#g" deploy.yaml
#替换kube-webhook-certgen镜像
sed -i "s#registry.k8s.io/ingress-nginx/kube-webhook-certgen#registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen#g" deploy.yaml
#暴露方式修改
sed -i "s#type: LoadBalancer#type: NodePort#g" deploy.yaml
3.注释“externalTrafficPolicy: Local”这一行
3.安装
sudo kubectl apply -f deploy.yaml
4.查看端口进行访问
5.规则使用
先创建deploy和service
kind: Deployment
apiVersion: apps/v1
metadata:
name: i8000
labels:
k8s-app: i8000
spec:
replicas: 2
selector:
matchLabels:
k8s-app: i8000
template:
metadata:
name: i8000
labels:
k8s-app: i8000
spec:
containers:
- name: i8000
image: registry.cn-shenzhen.aliyuncs.com/atomic/ingress8000:v1
ports:
- containerPort: 8000
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: i8100
labels:
k8s-app: i8100
spec:
replicas: 2
selector:
matchLabels:
k8s-app: i8100
template:
metadata:
name: i8100
labels:
k8s-app: i8100
spec:
containers:
- name: i8100
image: registry.cn-shenzhen.aliyuncs.com/atomic/ingress8100:v1
ports:
- containerPort: 8100
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: i8000
name: i8000
spec:
selector:
k8s-app: i8000
ports:
- name: http
protocol: TCP
port: 8000
targetPort: 8000
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: i8100
name: i8100
spec:
selector:
k8s-app: i8100
ports:
- name: http
protocol: TCP
port: 8100
targetPort: 8100
创建ingress
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: ingress-i8000
spec:
ingressClassName: nginx
rules:
- host: "i8000.lry.cn"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: i8000
port:
number: 8000
- host: "i8100.lry.cn"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: i8100
port:
number: 8100
修改系统hosts文件
访问页面
7.路径截串
用法官网说明:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: ingress-rewrite
spec:
ingressClassName: nginx
rules:
- host: "*.lry.cn"
http:
paths:
- pathType: Prefix
path: /abc(.*?/|$)(.*)
backend:
service:
name: i8000
port:
number: 8000
8.限流
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
annotations:
nginx.ingress.kubernetes.io/limit-rps: "1"
name: ingress-rule
spec:
ingressClassName: nginx
rules:
- host: "*.lry.cn"
http:
paths:
- pathType: Exact
path: /
backend:
service:
name: i8000
port:
number: 8000
赞(2)
赏