kubernetes中部署ingress-nginx访问k8s内部服务

一、系统环境

CentOS Linux release 7.9.2009 (Core)
kubectl-1.20.4-0.x86_64
kubelet-1.20.4-0.x86_64
kubeadm-1.20.4-0.x86_64
kubernetes-cni-0.8.7-0.x86_64

二、k8s架构

| 用途 | ip地址 | 主机名 |
| master | 192.168.10.127 | minio-4 |
| node01 | 192.168.10.124 | minio-1 |
| node02 | 192.168.10.125 | minio-2 |
| node03 | 192.168.10.126 | minio-3 |
| nfs存储 | 192.168.10.143 | |

三、Ingress Controller原理

具体可以参考:深入理解ingress-nginx - 赵SIR - 博客园

Ingress Controller有很多实现,我们这里采用官方维护的Nginx控制器。

部署文档:https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md

四、具体安装方法

1、下载官方nignx版本

wget -O nginx-ingress.yaml https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.41.2/deploy/static/provider/cloud/deploy.yaml
image.gif

2、修改下载后的nginx-ingress.yaml

vim nginx-ingress.yaml
image.gif

3、增加行 replicas: 2 副本数

spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: ingress-nginx
      app.kubernetes.io/instance: ingress-nginx
      app.kubernetes.io/component: controller
  revisionHistoryLimit: 10
  strategy:
    rollingUpdate:
      maxUnavailable: 1
    type: RollingUpdate
  minReadySeconds: 0
  replicas: 2
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/component: controller
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirst

image.gif

4、修改镜像为国内镜像

增加行 hostNetwork: true
image: pollyduan/ingress-nginx-controller:v0.41.2

minReadySeconds: 0
  replicas: 2
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/component: controller
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirst
      containers:
        - name: controller
          image: pollyduan/ingress-nginx-controller:v0.41.2
          #image: k8s.gcr.io/ingress-nginx/controller:v0.48.1@sha256:e9fb216ace49dfa4a5983b183067e97496e7a8b307d2093f4278cd550c303899
          imagePullPolicy: IfNotPresent
          lifecycle:

image.gif

5、节点服务器上添加标签 和 ingress-deploy.yaml 中保持一致否则部署时会出现错误

image
image.gif

在每个节点执行打标签命令

#kubectl label nodes master ingress-ready=true
#kubectl label nodes master kubernetes.io/os=linux --overwrite
image.gif

查看label,应该就有刚才执行的两个标签

# kubectl get nodes --show-labels
NAME      STATUS   ROLES                  AGE    VERSION   LABELS
minio-1   Ready    master                 118d   v1.20.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-ready=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=minio-1,kubernetes.io/os=linux,node-role.kubernetes.io/master=true
minio-2   Ready    master                 118d   v1.20.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-ready=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=minio-2,kubernetes.io/os=linux,node-role.kubernetes.io/master=true
minio-3   Ready    master                 101d   v1.20.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-ready=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=minio-3,kubernetes.io/os=linux,node-role.kubernetes.io/master=true
minio-4   Ready    control-plane,master   118d   v1.20.4   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-ready=false,kubernetes.io/arch=amd64,kubernetes.io/hostname=minio-4,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=

image.gif
image
image.gif

6、部署ingress-nginx服务

#kubectl create -f nginx-ingress.yaml

namespace/ingress-nginx created
serviceaccount/ingress-nginx created
configmap/ingress-nginx-controller created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
service/ingress-nginx-controller-admission created
service/ingress-nginx-controller created
deployment.apps/ingress-nginx-controller created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
serviceaccount/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
image.gif

查看运行情况

# kubectl get pod,service -n ingress-nginx -o wide
NAME                                            READY   STATUS      RESTARTS   AGE   IP               NODE      NOMINATED NODE   READINESS GATES
pod/ingress-nginx-admission-create-bwg5x        0/1     Completed   0          72d   10.244.2.39      minio-2   <none>           <none>
pod/ingress-nginx-admission-patch-958vt         0/1     Completed   0          72d   10.244.1.31      minio-1   <none>           <none>
pod/ingress-nginx-controller-6ff975b7f7-2dcjh   1/1     Running     1          72d   192.168.10.126   minio-3   <none>           <none>
pod/ingress-nginx-controller-6ff975b7f7-fwlql   1/1     Running     1          72d   192.168.10.124   minio-1   <none>           <none>
pod/ingress-nginx-controller-6ff975b7f7-wdhkw   1/1     Running     1          72d   192.168.10.125   minio-2   <none>           <none>

NAME                                         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE   SELECTOR
service/ingress-nginx-controller             NodePort    10.10.26.171   <none>        80:30096/TCP,443:30113/TCP   72d   app.kubernetes.io/component=controller,app.kubernetes.io/instance=ingress-nginx,app.kubernetes.io/name=ingress-nginx
service/ingress-nginx-controller-admission   ClusterIP   10.10.3.13     <none>        443/TCP                      72d   app.kubernetes.io/component=controller,app.kubernetes.io/instance=ingress-nginx,app.kubernetes.io/name=ingress-nginx

image.gif
#kubectl get pods -n ingress-nginx

NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-bwg5x        0/1     Completed   0          72d
ingress-nginx-admission-patch-958vt         0/1     Completed   0          72d
ingress-nginx-controller-6ff975b7f7-2dcjh   1/1     Running     1          72d
ingress-nginx-controller-6ff975b7f7-fwlql   1/1     Running     1          72d
ingress-nginx-controller-6ff975b7f7-wdhkw   1/1     Running     1          72d

image.gif

如果pod状态有异样请使用kubectl describe pod pod名称 -n ingress-nginx 查看详情

#kubectl describe pod ingress-nginx-controller-6ff975b7f7-2dcjh -n ingress-nginx
Name:         ingress-nginx-controller-6ff975b7f7-2dcjh
Namespace:    ingress-nginx
Priority:     0
Node:         minio-3/192.168.10.126
Start Time:   Wed, 11 Aug 2021 13:52:07 +0800
Labels:       app.kubernetes.io/component=controller
              app.kubernetes.io/instance=ingress-nginx
              app.kubernetes.io/name=ingress-nginx
              pod-template-hash=6ff975b7f7
Annotations:  <none>
Status:       Running
IP:           192.168.10.126
IPs:
  IP:           192.168.10.126
Controlled By:  ReplicaSet/ingress-nginx-controller-6ff975b7f7
Containers:
  controller:
    Container ID:  docker://9fcedbe86046d9dda1f3f26778efcd38ede942e341fc65dec1a83a07c5822e26
    Image:         pollyduan/ingress-nginx-controller:v0.41.2
    Image ID:      docker-pullable://pollyduan/ingress-nginx-controller@sha256:c7f8c2eaa157cc18f267c6c89a9e659f65ee7b347547a37c6471aff78ca22021
    Ports:         80/TCP, 443/TCP, 8443/TCP
    Host Ports:    80/TCP, 443/TCP, 8443/TCP
    Args:
      /nginx-ingress-controller
      --election-id=ingress-controller-leader
      --ingress-class=nginx
      --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
      --validating-webhook=:8443
      --validating-webhook-certificate=/usr/local/certificates/cert
      --validating-webhook-key=/usr/local/certificates/key
      --publish-status-address=localhost
    State:          Running
      Started:      Wed, 15 Sep 2021 09:20:42 +0800
    Ready:          True
    Restart Count:  1
    Requests:
      cpu:      100m
      memory:   90Mi
    Liveness:   http-get http://:10254/healthz delay=10s timeout=1s period=10s #success=1 #failure=5
    Readiness:  http-get http://:10254/healthz delay=10s timeout=1s period=10s #success=1 #failure=3
    Environment:
      POD_NAME:       ingress-nginx-controller-6ff975b7f7-2dcjh (v1:metadata.name)
      POD_NAMESPACE:  ingress-nginx (v1:metadata.namespace)
      LD_PRELOAD:     /usr/local/lib/libmimalloc.so
    Mounts:
      /usr/local/certificates/ from webhook-cert (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from ingress-nginx-token-sjjs6 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  webhook-cert:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  ingress-nginx-admission
    Optional:    false
  ingress-nginx-token-sjjs6:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  ingress-nginx-token-sjjs6
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  ingress-ready=true
                 kubernetes.io/os=linux
Tolerations:     node-role.kubernetes.io/master:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:          <none>

image.gif

6、测试一个服务

#前提你的nginx服务已经部署好

vim pvc.test.cn-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: pvc.test.cn
  # 命名空间和代理的serviceName 所属命名空间保存一致,否则访问是会出现503错误
  namespace: default  
spec:
  rules:
  - host: pvc.test.cn
    http:
      paths:
      - backend:
          # 注册的服务名称
          serviceName: nginx-pvc-php74
          # 服务端口
          servicePort: 80

image.gif
#kubectl create -f pvc.test.cn-ingress.yaml
image.gif

查看状态

kubectl get ingress --all-namespaces
NAMESPACE    NAME                  CLASS    HOSTS                 ADDRESS     PORTS   AGE
default      pvc.test.cn          <none>   pvc.test.cn          localhost   80      44d

image.gif

在dns做一下域名解析

因为我这3台的node都装了ingress服务,所以dns上就解析了3条记录,这样就可以轮询

pvc     A   192.168.10.124
pvc     A   192.168.10.125
pvc     A   192.168.10.126

image.gif

打开浏览器输入pvc.test.cn

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,294评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,780评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,001评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,593评论 1 289
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,687评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,679评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,667评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,426评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,872评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,180评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,346评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,019评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,658评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,268评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,495评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,275评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,207评论 2 352

推荐阅读更多精彩内容