Kubernets 外部访问

一、概念

Kubernetes的三种外部访问方式:NodePort、LoadBalancer 和 Ingress

1、NodePort

  服务是引导外部流量到你的服务的最原始方式。在所有节点(虚拟机)上开放一个特定端口,任何发送到该端口的流量都被转发到对应服务。端口范围只能是 30000-32767

2、Ingress

  暴露服务的最强大方式,Ingress 控制器有各种类型,比较常用的Ingress Nginx(kubernets 官方维护)、TraefikVoyagerIngress Kong

3、LoadBalancer

  服务是暴露服务到 internet 的标准方式。在 GKE 上,这种方式会启动一个 Network Load Balancer[2],它将给你一个单独的 IP 地址,转发所有流量到你的服务

二、部署测试

 这里用NodePort、Ingress进行部署,也是最常用的两种方式

 部署应用:https://www.cnblogs.com/fanxp/p/12084733.html

1、NodePort

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  # 暴露方式
  type: NodePort
  ports:
    # service 内部访问端口
  - port: 8080
    # pod 端口
    targetPort: 80
    # 对外暴露端口(如果不写默认随机30000-32767)
    nodePort: 30003

查看:

# 加载配置
kubectl apply -f nginx.yaml
# 查看service
kubectl get service

 找外部一台电脑浏览器:http://192.168.1.9:30003/ 这里ip换成node中任意一台ip地址

 2、Ingress

  这里使用kubernetes 维护的Ingress Nginx

  • helm v3 安装
  • helm 安装 Ingress Nginx
  • 配置Ingress Nginx

 helm 所有版本:https://github.com/helm/helm/releases

# 下载helm
curl -SLO https://get.helm.sh/helm-v3.0.2-linux-amd64.tar.gz
# 解压
tar -zxvf helm-v3.0.2-linux-amd64.tar.gz
# 移至 /bin 目录
mv linux-amd64/helm /usr/local/bin/helm
# 验证
helm version

# 添加repo
helm repo add stable https://kubernetes-charts.storage.googleapis.com
#阿里云repo(未测试)
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
# 查看
helm repo list

# 查看node ip
kubectl get nodes -o wide
# 安装nginx-ingress(controller.service.externalIPs[0]=node ip)
helm install nginx-ingress stable/nginx-ingress --set "controller.service.externalIPs[0]=192.168.1.222,controller.service.externalIPs[1]=192.168.1.9"

nginx.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-service-ingress
spec:
  rules:
    # 域名
  - host: nginx.fanxp.com
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 8080
        path: /

查看ingress

kubectl get ingress

 如果是局域网,在客户机host文件加上

192.168.1.222 nginx.fanxp.com

在浏览器中访问:nginx.fanxp.com 可以看到和NodePort一样的效果

所有代码:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deplayment
spec:
  selector:
    matchLabels:
      app: nginx
  # 数量
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx    
    spec:
      containers:
      - name: nginx
        # 指定镜像
        image: nginx:alpine
        # 指定暴露端口
        ports:
        - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  # 暴露方式
  type: NodePort
  ports:
    # service 内部访问端口
  - port: 8080
    # pod 端口
    targetPort: 80
    # 对外暴露端口(如果不写默认随机30000-32767)
    nodePort: 30003

---

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-service-ingress
spec:
  rules:
    # 域名
  - host: nginx.fanxp.com
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 8080
        path: /

感兴趣可以进入pod nginx-ingress-controller 查看nginx.conf 生成规则

kubectl exec -it nginx-ingress-controller-775b4967cb-w4rc7 sh
# 查看
cat nginx.conf

原文地址:https://www.cnblogs.com/fanxp/p/12091408.html