k8s中部署nginx-ingress实现外部访问k8s集群内部服务

news2024/11/18 0:32:53

k8s通过nginx-ingress实现集群外网访问功能

一:ingress概述

1.1 ingress 工作原理

step1:ingress contronler通过与k8s的api进行交互,动态的去感知k8s集群中ingress服务规则的变化,然后读取它,并按照定义的ingress规则,转发到k8s集群中对应的service。

step2:而这个ingress规则写明了哪个域名对应k8s集群中的哪个service,然后再根据ingress-controller中的nginx配置模板,生成一段对应的nginx配置。

step3:然后再把该配置动态的写到ingress-controller的pod里,该ingress-controller的pod里面运行着一个nginx服务,控制器会把生成的nginx配置写入到nginx的配置文件中,然后reload一下,使其配置生效,以此来达到域名分配置及动态更新的效果。

在这里插入图片描述

1.2 ingress可以解决的问题

1)动态配置服务

如果按照传统方式, 当新增加一个服务时, 我们可能需要在流量入口加一个反向代理指向我们新的k8s服务. 而如果用了Ingress, 只需要配置好这个服务, 当服务启动时, 会自动注册到Ingress的中, 不需要而外的操作。

2)减少不必要的端口暴露

配置过k8s的都清楚, 第一步是要关闭防火墙的, 主要原因是k8s的很多服务会以NodePort方式映射出去, 这样就相当于给宿主机打了很多孔, 既不安全也不优雅. 而Ingress可以避免这个问题, 除了Ingress自身服务可能需要映射出去, 其他服务都不要用NodePort方式。

二、部署nginx-ingress

2.0 2.2 相关部署组件说明
PodNamenamespaceServiceNameports部署方式作用
ingress-nginx-controllertestingress-nginx-controllerNodePort :80(http)、443(https)DaemonSet实现基于灵活的 ingress 策略定义的服务路由功能
ingress-nginx-admission-createtestingress-nginx-controller-admissionLoadBalancer: 443Job是用来创建证书的,需要指定证书的名称、域名、ns等信息
ingress-nginx-admission-patchtestingress-nginx-controller-admissionLoadBalancer: 443Job将前面创建的证书中的ca提取出来,写入到指定的admission webhook配置中
ingress实例testingressnginx转发具体配置文件
2.1 编写nginx-ingreess相关资源文件
[root@master1 ingress]# cat > nginx-ingress.yaml << EOF
apiVersion: v1
kind: Namespace
metadata:
  name: test
---
apiVersion: v1
automountServiceAccountToken: true
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx
  namespace: test
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission
  namespace: test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx
  namespace: test
rules:
- apiGroups:
  - ""
  resources:
  - namespaces
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - configmaps
  - pods
  - secrets
  - endpoints
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses/status
  verbs:
  - update
- apiGroups:
  - networking.k8s.io
  resources:
  - ingressclasses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resourceNames:
  - ingress-controller-leader
  resources:
  - configmaps
  verbs:
  - get
  - update
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - create
- apiGroups:
  - coordination.k8s.io
  resourceNames:
  - ingress-controller-leader
  resources:
  - leases
  verbs:
  - get
  - update
- apiGroups:
  - coordination.k8s.io
  resources:
  - leases
  verbs:
  - create
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
  - patch
- apiGroups:
  - discovery.k8s.io
  resources:
  - endpointslices
  verbs:
  - list
  - watch
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission
  namespace: test
rules:
- apiGroups:
  - ""
  resources:
  - secrets
  verbs:
  - get
  - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx
rules:
- apiGroups:
  - ""
  resources:
  - configmaps
  - endpoints
  - nodes
  - pods
  - secrets
  - namespaces
  verbs:
  - list
  - watch
- apiGroups:
  - coordination.k8s.io
  resources:
  - leases
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
  - patch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses/status
  verbs:
  - update
- apiGroups:
  - networking.k8s.io
  resources:
  - ingressclasses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - discovery.k8s.io
  resources:
  - endpointslices
  verbs:
  - list
  - watch
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission
rules:
- apiGroups:
  - admissionregistration.k8s.io
  resources:
  - validatingwebhookconfigurations
  verbs:
  - get
  - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx
  namespace: test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: ingress-nginx
subjects:
- kind: ServiceAccount
  name: ingress-nginx
  namespace: test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission
  namespace: test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: ingress-nginx-admission
subjects:
- kind: ServiceAccount
  name: ingress-nginx-admission
  namespace: test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ingress-nginx
subjects:
- kind: ServiceAccount
  name: ingress-nginx
  namespace: test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ingress-nginx-admission
subjects:
- kind: ServiceAccount
  name: ingress-nginx-admission
  namespace: test
---
apiVersion: v1
data:
  allow-snippet-annotations: "true"
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-controller
  namespace: test
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-controller
  namespace: test
spec:
  externalTrafficPolicy: Local
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - appProtocol: http
    name: http
    port: 80
    protocol: TCP
    targetPort: http
  - appProtocol: https
    name: https
    port: 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
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-controller-admission
  namespace: test
spec:
  ports:
  - appProtocol: https
    name: https-webhook
    port: 443
    targetPort: webhook
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  type: LoadBalancer
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-controller
  namespace: test
spec:
  minReadySeconds: 0
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/component: controller
      app.kubernetes.io/instance: ingress-nginx
      app.kubernetes.io/name: ingress-nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
    spec:
      hostNetwork: true
      containers:
      - args:
        - /nginx-ingress-controller
        - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
        - --election-id=ingress-controller-leader
        - --controller-class=k8s.io/ingress-nginx
        - --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
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: LD_PRELOAD
          value: /usr/local/lib/libmimalloc.so
        image: anjia0532/google-containers.ingress-nginx.controller:v1.4.0
        imagePullPolicy: IfNotPresent
        lifecycle:
          preStop:
            exec:
              command:
              - /wait-shutdown
        livenessProbe:
          failureThreshold: 5
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        name: controller
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        - containerPort: 443
          name: https
          protocol: TCP
        - containerPort: 8443
          name: webhook
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          requests:
            cpu: 100m
            memory: 90Mi
        securityContext:
          allowPrivilegeEscalation: true
          capabilities:
            add:
            - NET_BIND_SERVICE
            drop:
            - ALL
          runAsUser: 101
        volumeMounts:
        - mountPath: /usr/local/certificates/
          name: webhook-cert
          readOnly: true
      dnsPolicy: ClusterFirst
      nodeSelector:
        kubernetes.io/os: linux
      serviceAccountName: ingress-nginx
      terminationGracePeriodSeconds: 300
      volumes:
      - name: webhook-cert
        secret:
          secretName: ingress-nginx-admission
---
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission-create
  namespace: test
spec:
  template:
    metadata:
      labels:
        app.kubernetes.io/component: admission-webhook
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
        app.kubernetes.io/version: 1.4.0
      name: ingress-nginx-admission-create
    spec:
      containers:
      - args:
        - create
        - --host=ingress-nginx-controller-admission,ingress-nginx-controller-admission.$(POD_NAMESPACE).svc
        - --namespace=$(POD_NAMESPACE)
        - --secret-name=ingress-nginx-admission
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: anjia0532/google-containers.ingress-nginx.kube-webhook-certgen:v20220916-gd32f8c343
        imagePullPolicy: IfNotPresent
        name: create
        securityContext:
          allowPrivilegeEscalation: false
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: OnFailure
      securityContext:
        fsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
      serviceAccountName: ingress-nginx-admission
---
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission-patch
  namespace: test
spec:
  template:
    metadata:
      labels:
        app.kubernetes.io/component: admission-webhook
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
        app.kubernetes.io/version: 1.4.0
      name: ingress-nginx-admission-patch
    spec:
      containers:
      - args:
        - patch
        - --webhook-name=ingress-nginx-admission
        - --namespace=$(POD_NAMESPACE)
        - --patch-mutating=false
        - --secret-name=ingress-nginx-admission
        - --patch-failure-policy=Fail
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: anjia0532/google-containers.ingress-nginx.kube-webhook-certgen:v20220916-gd32f8c343
        imagePullPolicy: IfNotPresent
        name: patch
        securityContext:
          allowPrivilegeEscalation: false
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: OnFailure
      securityContext:
        fsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
      serviceAccountName: ingress-nginx-admission
---
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: nginx
spec:
  controller: k8s.io/ingress-nginx
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.4.0
  name: ingress-nginx-admission
webhooks:
- admissionReviewVersions:
  - v1
  clientConfig:
    service:
      name: ingress-nginx-controller-admission
      namespace: test
      path: /networking/v1/ingresses
  failurePolicy: Fail
  matchPolicy: Equivalent
  name: validate.nginx.ingress.kubernetes.io
  rules:
  - apiGroups:
    - networking.k8s.io
    apiVersions:
    - v1
    operations:
    - CREATE
    - UPDATE
    resources:
    - ingresses
  sideEffects: None
EOF
应用生效
[root@master1 ingress]# kubectl apply -f nginx-ingress.yaml
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
daemonset.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created

验证
[root@master1 ingress]# kubectl get pods -n test
NAME                                     READY   STATUS             RESTARTS      AGE
ingress-nginx-admission-create-z2xgq     0/1     Completed          0             3m8s
ingress-nginx-admission-patch-qpnh7      0/1     Completed          2             3m8s
ingress-nginx-controller-kc7km           1/1     Running            0             3m8s
ingress-nginx-controller-knjm6           0/1     CrashLoopBackOff   3 (19s ago)   3m8s
ingress-nginx-controller-mzqjn           1/1     Running            0             3m8s
ingress-nginx-controller-xcxsd           1/1     Running            0             3m8s
nfs-client-provisioner-fb55999fb-pcrqt   1/1     Running            0             4h11m
web-0                                    1/1     Running            0             4h5m
web-1                                    1/1     Running            0             4h5m
[root@master1 ingress]# kubectl logs -n test ingress-nginx-controller-knjm6
-------------------------------------------------------------------------------
NGINX Ingress controller
  Release:       v1.4.0
  Build:         50be2bf95fd1ef480420e2aa1d6c5c7c138c95ea
  Repository:    https://github.com/kubernetes/ingress-nginx
  nginx version: nginx/1.19.10

-------------------------------------------------------------------------------

F0524 06:17:54.168788       6 main.go:67] port 80 is already in use. Please check the flag --http-port

报错解决:

由POD报错日志可知,80端口被占用了,解决后重启pod即可。

[root@master1 ingress]# docker ps|grep rancher
56e840839dc1        rancher/rancher:v2.7.0-rc12                                     "entrypoint.sh"          7 days ago          Up 7 days     0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   rancher
[root@master1 ingress]# docker stop rancher
rancher
[root@master1 ingress]# kubectl get pods -n test
NAME                                     READY   STATUS             RESTARTS      AGE
ingress-nginx-admission-create-z2xgq     0/1     Completed          0             8m46s
ingress-nginx-admission-patch-qpnh7      0/1     Completed          2             8m46s
ingress-nginx-controller-kc7km           1/1     Running            0             8m46s
ingress-nginx-controller-knjm6           0/1     CrashLoopBackOff   6 (53s ago)   8m46s
ingress-nginx-controller-mzqjn           1/1     Running            0             8m46s
ingress-nginx-controller-xcxsd           1/1     Running            0             8m46s
nfs-client-provisioner-fb55999fb-pcrqt   1/1     Running            0             4h17m
web-0                                    1/1     Running            0             4h11m
web-1                                    1/1     Running            0             4h11m
[root@master1 ingress]# kubectl delete pods -n test ingress-nginx-controller-knjm6
pod "ingress-nginx-controller-knjm6" deleted
[root@master1 ingress]# kubectl get pods -n test
NAME                                     READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-z2xgq     0/1     Completed   0          9m13s
ingress-nginx-admission-patch-qpnh7      0/1     Completed   2          9m13s
ingress-nginx-controller-kc7km           1/1     Running     0          9m13s
ingress-nginx-controller-mzqjn           1/1     Running     0          9m13s
ingress-nginx-controller-r7knt           1/1     Running     0          12s
ingress-nginx-controller-xcxsd           1/1     Running     0          9m13s
nfs-client-provisioner-fb55999fb-pcrqt   1/1     Running     0          4h17m
web-0                                    1/1     Running     0          4h11m
web-1                                    1/1     Running     0          4h11m

创建一个ingree,测试外网访问
查看当前svc
[root@master1 ingress]# kubectl get svc -n test
NAME                                 TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort       10.96.117.202   <none>        80:32210/TCP,443:32008/TCP   11m
ingress-nginx-controller-admission   LoadBalancer   10.96.131.36    <pending>     443:32639/TCP                11m
nginx                                ClusterIP      None            <none>        80/TCP                       4h14m
You have new mail in /var/spool/mail/root
[root@master1 ingress]# kubectl get svc -n test nginx -oyaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx","namespace":"test"},"spec":{"clusterIP":"None","ports":[{"name":"web","port":80}],"selector":{"app":"nginx"}}}
  creationTimestamp: "2023-05-24T02:11:37Z"
  labels:
    app: nginx
  name: nginx
  namespace: test
  resourceVersion: "2499378"
  uid: a4584c4d-51ea-4bf0-b711-880090ad1dae
spec:
  clusterIP: None
  clusterIPs:
  - None
  internalTrafficPolicy: Cluster
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - name: web
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

创建ingress
cat > web-ing.yaml << EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-ing
  namespace: test
spec:
  rules:
  - host: web.example.com
    http:
      paths:
      - backend:
          service:
            name: nginx
            port:
              number: 80
        path: /
        pathType: Prefix
EOF
应用
[root@master1 ingress]# kubectl apply -f web-ing.yaml
ingress.networking.k8s.io/web-ing created

验证
# 确认部署
  1. Ingress-nginx-controller:

​ 为了高可用,部署方式采用DaemonSet,所以集群内可调度的节点都会部署一个(master节点不可调度),并确认处于Runnning状态。

  1. Ingress-nginx-controller服务

​ 使用NodePort方式,确认PORTS后面出现30000以上的本地端口

[root@master1 nfs-provisioner]# kubectl get pod,svc -n test
NAME                                         READY   STATUS      RESTARTS   AGE
pod/ingress-nginx-admission-create-z2xgq     0/1     Completed   0          67m
pod/ingress-nginx-admission-patch-qpnh7      0/1     Completed   2          67m
pod/ingress-nginx-controller-kc7km           1/1     Running     0          67m
pod/ingress-nginx-controller-mzqjn           1/1     Running     0          67m
pod/ingress-nginx-controller-r7knt           1/1     Running     0          58m
pod/ingress-nginx-controller-xcxsd           1/1     Running     0          67m
pod/nfs-client-provisioner-fb55999fb-pcrqt   1/1     Running     0          5h15m
pod/web-0                                    1/1     Running     0          5h9m
pod/web-1                                    1/1     Running     0          5h9m
pod/web-7849c945f4-k9xzz                     1/1     Running     0          21m
pod/web-7849c945f4-x246j                     1/1     Running     0          21m

NAME                                         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx-controller             NodePort       10.96.117.202   <none>        80:32210/TCP,443:32008/TCP   67m
service/ingress-nginx-controller-admission   LoadBalancer   10.96.131.36    <pending>     443:32639/TCP                67m
service/nginx                                ClusterIP      10.96.6.151     <none>        80/TCP                       21m
[root@master1 nfs-provisioner]# telnet 10.140.20.142 32210
Trying 10.140.20.142...
Connected to 10.140.20.142.
Escape character is '^]'.
^CConnection closed by foreign host.
访问验证
[root@master1 nfs-provisioner]# kubectl exec -n test web-7849c945f4-k9xzz -it bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@web-7849c945f4-k9xzz:/# ls /usr/share/nginx/html/
root@web-7849c945f4-k9xzz:/#
root@web-7849c945f4-k9xzz:/# ls /usr/share/nginx/html/
root@web-7849c945f4-k9xzz:/# echo 1 >  /usr/share/nginx/html/index.html
root@web-7849c945f4-k9xzz:/# curl http://localhost/
1
root@web-7849c945f4-k9xzz:/# exit
[root@master1 nfs-provisioner]# kubectl get svc -n test
NAME                                 TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort       10.96.117.202   <none>        80:32210/TCP,443:32008/TCP   51m
ingress-nginx-controller-admission   LoadBalancer   10.96.131.36    <pending>     443:32639/TCP                51m
nginx                                ClusterIP      10.96.6.151     <none>        80/TCP                       5m40s
[root@master1 nfs-provisioner]# kubectl get ing -n test
NAME      CLASS   HOSTS             ADDRESS         PORTS   AGE
web-ing   nginx   web.example.com   10.96.117.202   80      26m

#登录k8s集群外的一台服务器做完hosts解析后进行验证
root@k8s-master1:~# tail -n 1 /etc/hosts
10.140.20.141 web.example.com
root@k8s-master1:~# curl  http://web.example.com
1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/563492.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

jsp手机回收软件系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 jsp手机回收软件系统 是一套完善的web设计系统&#xff0c;对理解JSP java编程开发语言有帮助 &#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发&#xff0c;数据库为Mysql&#xff0c;使…

Moonbeam社区治理|参与委托投票问卷,瓜分2000U奖励

社区治理升级意味着公链正走向可持续和透明化发展&#xff0c;让每位GLMR所有者都参与治理&#xff0c;是Moonbeam成为真正去中心化公链的重要一环。 Moonbeam治理 OpenGov为Moonbeam生态带来了多角色委托功能&#xff0c;使Token持有者能够根据track委托Token进行投票。委托…

零基础如何入门渗透测试

作为一名多年的渗透测试工程师&#xff0c;了解到很多零基础的初学者都面临着学习渗透测试的困难。在这里&#xff0c;我会提供一些指导性的建议和方法&#xff0c;帮助初学者快速入门&#xff0c;开启学习之旅。 一、什么是渗透测试 在学习渗透测试之前&#xff0c;建议先了解…

虹科技术 | 虹科EtherCAT增量编码器输入模块数据采集实操测试

1. 背景介绍 编码器是将信号或数据进行编制、转换为可用以通讯、传输和存储的信号形式的设备。编码器把角位移或直线位移转换成电信号&#xff0c;前者称为码盘&#xff0c;后者称为码尺。按照读出方式编码器可以分为接触式和非接触式两种&#xff1b;按照工作原理编码器可分为…

Android | Android 系统架构

参考&#xff1a; Android Developers(https://developer.android.google.cn/) 平台架构 Android 是基于 Linux 的开源软件栈&#xff0c;下图为官网给出的 Android 平台主要组件。 Android 平台从上&#xff08;直接与用户交互&#xff09;到下&#xff08;直接与硬件交互&a…

Mastodon 长毛象多租户:自定义域名、自定义账号别名

概念 自定义域名后缀 假设&#xff0c;Mastodon 主节点域名 domain1.com&#xff0c;我在该域名下拥有一个用户 user1domain1.com。 配置自定义域名后缀支持后&#xff0c;也可以通过 user1domain2.com 搜索到。该配置需要在主节点中设置 ALTERNATE_DOMAINS。 自定义账号别…

DOS的常用指令:

DOS的常用指令&#xff1a; DOS【介绍】&#xff1a;磁盘操作系统 cmd是操作DOS的媒介&#xff0c;dos可以操作Windows的目录结构&#xff0c; 基本操作指令&#xff1a; cmd【控制台】->发给dos【解析】->win的目录结构 常用操作指令&#xff1a; 《一》目录操作 &a…

QT学习笔记-QT5.15.2使用qtopcua5.15.2实现与PLC通讯(上)

QT学习笔记-QT5.15.2使用qtopcua5.15.2实现与PLC通讯&#xff08;上&#xff09; 环境说明背景思路perl依赖安装qtopcua插件编译解决编译报错问题解决安装mingw32-make install报错问题 环境说明 操作系统&#xff1a;Windows10 专业版 64位 开发工具&#xff1a;Qt 5.15.2 OP…

Python提取PDF文字的10个方法,OCR识别扫描版pdf,图片pdf格式的10种ocr汉字识别方法

Python 读取扫描版 PDF、图片 PDF 并进行 OCR 识别的方法&#xff1a; pytesseract&#xff1a;一种基于 Python 的 OCR 库&#xff0c;可用于识别扫描版 PDF 和图片 PDF 中的文本。 它可以使用 Google 的 OCR 引擎进行识别&#xff0c;也可以使用本地的 OCR 引擎进行识别。使…

阿里云免费ssl证书申请与部署

一、证书申请 1、找到 ssl 证书 2、点击选择SSL 证书 进入其管理控台 3、如果你还没有免费证书&#xff0c;选择购买即可&#xff0c;一个自然年内每个账号可以领取一次数量为20的免费单域名试用证书额度&#xff0c;我的已经购买过来&#xff0c;今年的&#xff0c;所以无法…

网络安全各类WAF绕过技巧

一、WAF绕过 1、脏数据绕过 即传入一段长数据使waf失效&#xff0c;从而实现绕过waf。某些waf处理POST的数据时&#xff0c;只会检测开头的8K&#xff0c;后面选择全部放过。 例如&#xff0c;当发现某网站存在一个反序列化漏洞时&#xff0c;但是无回显&#xff0c;被waf拦…

MQTT中间件Eclipse Mosquitto安装和使用(.asc文件)MQTT监控命令mosquitto_sub(mosquitto C++库源码编译)

昨天弄的&#xff0c;今天忘了不少。。。 文章目录 参考链接安装MQTT服务中间件安装启动与查询卸载与清理 MQTT C支持库安装&#xff08;使C能使用相关库函数&#xff09;离线安装&#xff08;通过源码&#xff09;ubuntu官网下载软件包编译mosquitto客户端库 mosquitto Docker…

后端SpringBoot应用向云原生K8S平台迁移

目录 一、引言二、方式1&#xff1a;在K8S上部署Spring Cloud Alibaba三、方式2&#xff1a;在K8S上部署Spring Cloud K8S3.1 第1次优化&#xff1a;移除Spring Cloud K8S DiscoveryClient 四、方式3&#xff1a;在K8S上部署SpringBoot应用4.1 第2次优化&#xff1a;移除Spring…

acwing提高--DFS之剪枝与优化

剪枝与优化的方法 1.优化搜索顺序 大部分情况下&#xff0c;我们应该优先搜索分支较少的节点 2.排除等效冗余 3.可行性剪枝 4.最优性剪枝 5.记忆化搜索&#xff08;DP&#xff09; 1.小猫爬山 题目https://www.acwing.com/problem/content/description/167/ 1.优化搜索顺…

《操作系统》期末最全复习题及解析

文章目录 选择题填空题简答题程序题综合题1.银行家算法2.页面置换算法3.进程调度算法4.磁盘调度算法5.求物理/逻辑地址6.分页存储管理7.可变分区分配算法 选择题 若信号量S的初值为2&#xff0c;且有3个进程共享此信号量&#xff0c;则S的取值范围是&#xff08;B &#xff09;…

单词长度统计,统计数据放入列表

输入一段英文计算每个单词长度&#xff0c;统计不含非英文字符&#xff0c;列表输出。 【学习的细节是欢悦的历程】 Python 官网&#xff1a;https://www.python.org/ Free&#xff1a;大咖免费“圣经”教程《 python 完全自学教程》&#xff0c;不仅仅是基础那么简单…… 地址…

AI与税务管理:新技术带来的新机遇和新挑战

本文作者&#xff1a;王伊琳 人工智能&#xff08;Artificial Intelligence&#xff0c;AI&#xff09;是指由计算机系统或机器人模拟人类智能的过程和结果&#xff0c;包括感知、理解、学习、推理、决策等能力。近年来&#xff0c;随着计算机技术、互联网平台、大数据分析等的…

AI工具 ChatGPT-4 vs Google Bard , PostgreSQL 开发者会pick谁?

在人工智能 (AI) 进步的快节奏世界中&#xff0c;开发人员正在寻找最高效和突破性的解决方案来加快和提高他们的工作质量。对于 PostgreSQL 开发人员来说&#xff0c;选择理想的 AI 支持的工具以最专业的方式解决他们的查询至关重要。 近年来&#xff0c;人工智能工具的普及率…

Redis如何做到内存高效利用?过期key删除术解析!

大家好&#xff0c;我是小米&#xff0c;一个热衷于分享技术的小伙伴。今天我要和大家探讨一个关于 Redis 的话题&#xff1a;删除过期key。在使用 Redis 进行数据存储和缓存时&#xff0c;我们经常会遇到过期数据的处理问题。接下来&#xff0c;我将为大家介绍为什么要删除过期…