kubernetes ingress-nginx 5分钟部署好,80端口暴露服务,不借助负载均衡

安装:

先下载:

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.2/deploy/static/provider/cloud/deploy.yaml

如果打不开,请使用国内源。

修改里面Service name: ingress-nginx-controller 的一段,找到type:

type 改为NodePort ,因为你没有LoadBalance可测试。
type: NodePort

然后:

kubectl apply -f deploy.yaml

查看ingress-controllor

kubectl get pods --namespace=ingress-nginx

执行这个命令:

kubectl wait --namespace ingress-nginx \
  --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller \
  --timeout=120s

出现:condition met
安装完毕

查看服务、出现如下:

# kubectl get service ingress-nginx-controller --namespace=ingress-nginx
NAME                       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                 AGE
ingress-nginx-controller   NodePort   10.103.21.218   <none>        80:32317/TCP,443:32337/TCP   13m

记住这里的端口:32317 (实例)

创建deploy 和service:

kubectl create deployment demo --image=httpd --port=80
kubectl expose deployment demo

创建ingress:

kubectl create ingress demo --class=nginx \
  --rule www.demo.io/=demo:80

查看你的ingress controllor 所在node:

ingress-nginx-controller-776cdbb5d4-skxtw   1/1     Running     5          19m
[root@master1 leon]# kubectl get pod -n ingress-nginx -o wide
NAME                                        READY   STATUS      RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
ingress-nginx-admission-create-bb8p8        0/1     Completed   0          19m   10.244.1.15   node1   <none>           <none>
ingress-nginx-admission-patch-796mz         0/1     Completed   1          19m   10.244.1.16   node1   <none>           <none>
**ingress-nginx-controller-776cdbb5d4-skxtw   1/1     Running     5          19m   10.244.2.18   node2**   <none>           <none>

注意:是Node2,那么浏览器访问的IP,就是NODE2的ip(实例:192.168.132.52)

则在你的浏览器所在机器hosts表增加记录:

192.168.132.52 www.demo.io

然后用浏览器访问:
http://www.demo.io:32317,就可以看到“It works!”

表示成功。

对外暴露端口改成80

修改下载下来的deploy.yaml 还是 name: ingress-nginx-controller ,这一段。
增加 NodePort:80 和NodePort:443

如下:

apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.1.2
    helm.sh/chart: ingress-nginx-4.0.18
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  externalTrafficPolicy: Local
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - appProtocol: http
    name: http
    port: 80
    nodePort: 80
    protocol: TCP
    targetPort: http
  - appProtocol: https
    name: https
    port: 443
    nodePort: 443
    protocol: TCP
    targetPort: https
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  type: NodePort

这样直接执行,会出现错误:nodePort: Invalid value valid ports is 30000-32767,
是因为k8s的node节点的端口默认被限制在30000-32767的范围,

先修改kube的默认配置:

vi /etc/kubernetes/manifests/kube-apiserver.yaml

在spec.containers.command的最后面加上 --service-cluster-ip-range 这一行,如下内容

- --service-node-port-range=1-65535

最后 重启 kubelet

systemctl daemon-reload
systemctl restart kubelet

然后再执行:

kubectl apply -f deploy.yaml

然后打开浏览器查看:

kubernetes ingress-nginx 5分钟部署好,80端口暴露服务,不借助负载均衡_第1张图片

搞定!

你可能感兴趣的:(linux,kubernetes,linux,nginx,kubernetes)