Service:通过Pod的Label选择器,选择一组Pod提供服务

kubectl get svc

#默认空间中kubernetes是apiserver,使用443端口访问
curl 10.96.0.1:443

#Service是由kube-dns提供域名解析
kubectl get svc -n kube-system

创建一个Pod和Service
vi nginx-svc.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.15.12-alpine
        name: nginx
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80   #Service的端口
      targetPort: 80  #容器的端口

执行

kubectl apply -f nginx-svc.yaml
kubectl get pods --show-labels
kubectl get svc --show-labels
#访问Service
curl http://10.96.103.220

#进入一个nginx容器
kubectl exec -it nginx-b7599c689-vl2jk -- sh

#容器内部可以直接使用Service名称进行访问
wget http://my-service 

#跨命名空间访问需要加上命名空间名称
wget http://metrics-server.kube-system:443
exit

使用NodePort端口访问

kubectl edit svc my-service
  type: ClusterIP #集群内部访问
  type: NodePort # 在Kube-Proxy的节点上打开一个端口,此端口可以代理至后端Pod

Service默认NodePort端口范围

kubectl get svc
grep "service-node-port" /etc/kubernetes/manifests/kube-apiserver.yaml

使用Service代理k8s外部服务

假定一个已有的外部Java服务,并没有部署在k8s中,访问的地址:123.58.180.8:80
注意:其中Service的Name需要与Endpoints的相同,才能自动建立绑定关系
vi nginx-svc-external.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-svc-external
  name: nginx-svc-external
spec:
  ports:
  - name: http
    port: 80 
    protocol: TCP
    targetPort: 80
  type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
  labels:
    app: nginx-svc-external
  name: nginx-svc-external
subsets:
- addresses:
  - ip: 123.58.180.8 
  ports:
  - name: http
    port: 80
    protocol: TCP

执行

curl http://123.58.180.8

kubectl apply -f nginx-svc-external.yaml
kubectl get svc 

#使用service对应内部IP访问
curl http://10.96.39.178

#endpoints
kubectl get ep

ExternalName Service代理k8s外部域名

定义一个Service,通过Service的名称访问到外部域名
vi external-domain.yaml

vi external-domain.yaml
kind: Service
apiVersion: v1
metadata:
  name: external-service
spec:
  type: ExternalName
  externalName: www.baidu.com

执行

kubectl apply -f external-domain.yaml
kubectl get svc 
kubectl get pods

#进入某一个容器
kubectl exec -it nginx-b7599c689-nf9jp -- sh
wget http://external-service
exit
多端口Service
kubectl get svc kube-dns -n kube-system -oyaml

  ports:
  - name: dns
    port: 53
    protocol: UDP
    targetPort: 53
  - name: dns-tcp
    port: 53
    protocol: TCP
    targetPort: 53

Ingress

Kind:Ingress相当于nginx.conf
Ingress Controller相当于nginx

官方安装文档:
https://kubernetes.github.io/ingress-nginx/deploy/#quick-start

下载安装

wget -O ingress-deploy.yaml https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/baremetal/deploy.yaml

vi ingress-deploy.yaml
#将image替换成国内源
registry.aliyuncs.com/google_containers/nginx-ingress-controller:v1.5.1
registry.aliyuncs.com/google_containers/kube-webhook-certgen:v20220916-gd32f8c343


kubectl apply -f ingress-deploy.yaml
kubectl get pods -A
kubectl get pods -n ingress-nginx
使用域名发布k8s的服务
#准备一个Pod和Service
kubectl create deploy nginx --image=nginx:1.15.12-alpine
kubectl expose deploy nginx --port 80

#查看Ingress版本号
kubectl api-resources | grep ingress

vi web-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: nginx.test.com
    http:
      paths:
      - backend:
          service:
            name: nginx  #Service名称
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific

执行

kubectl apply -f web-ingress.yaml 
kubectl get ingress
kubectl get ingressclass
kubectl get svc -n ingress-nginx
kubectl get pod -n ingress-nginx

查看Ingress内部配置文件

kubectl exec -it ingress-nginx-controller-55b5c578d7-7gzsh -n ingress-nginx -- sh
ls
cat nginx.conf | grep -A20 'nginx.test.com'
exit
ingress不配置域名发布服务

vi web-ingress-spec.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress-spec
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - backend:
          service:
            name: nginx  #Service名称
            port:
              number: 80
        path: /spec-path
        pathType: ImplementationSpecific

执行

kubectl apply -f web-ingress-spec.yaml 
kubectl get ingress
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐