007.kubernets的headless service配置和ingress的简单配置

前面配置了servcie的nodepoint和clusterIP附在均衡

一 headless service配置

1.1 默认下的DNS配置

[root@docker-server1 deployment]# kubectl get  svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        2d18h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP       2d7h
wordpress    NodePort    10.96.72.115    <none>        80:32080/TCP   2d7h

headless service也属于clusterIP模式,不过设置为none

[root@docker-server1 deployment]# cd ../svcs/

[root@docker-server1 svcs]# vi nginx-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    name: hello-deployment
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 80

[root@docker-server1 svcs]# kubectl get deployment

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
hello-deployment   3/3     3            3           6m12s
mysql              1/1     1            1           2d7h
wordpress          1/1     1            1           2d7h

[root@docker-server1 svcs]# kubectl get deployment hello-deployment -o yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"hello-deployment","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"name":"hello-deployment"}},"strategy":{"rollingUpdate":{"maxSurge":"10%","maxUnavailable":0},"type":"RollingUpdate"},"template":{"metadata":{"labels":{"name":"hello-deployment"}},"spec":{"containers":[{"image":"nginx:1.14","name":"webserver","ports":[{"containerPort":80}]}]}}}}
  creationTimestamp: "2020-01-12T13:01:40Z"
  generation: 1
  name: hello-deployment
  namespace: default
  resourceVersion: "113947"
  selfLink: /apis/apps/v1/namespaces/default/deployments/hello-deployment
  uid: a65ecd15-4e40-4596-8bf1-414eb877bc98
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      name: hello-deployment
  strategy:
    rollingUpdate:
      maxSurge: 10%
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: hello-deployment
    spec:
      containers:
      - image: nginx:1.14
        imagePullPolicy: IfNotPresent
        name: webserver
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
  - lastTransitionTime: "2020-01-12T13:01:42Z"
    lastUpdateTime: "2020-01-12T13:01:42Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-01-12T13:01:40Z"
    lastUpdateTime: "2020-01-12T13:01:42Z"
    message: ReplicaSet "hello-deployment-5fdb46d67c" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 3
  replicas: 3
  updatedReplicas: 3

[root@docker-server1 svcs]# kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
goproxy                             1/1     Running   1          2d10h
hello-deployment-5fdb46d67c-dqnnh   1/1     Running   0          7m54s
hello-deployment-5fdb46d67c-s68tf   1/1     Running   0          7m54s
hello-deployment-5fdb46d67c-x5nwl   1/1     Running   0          7m54s
init-demo                           1/1     Running   1          2d9h
mysql-5d4695cd5-x9j2z               1/1     Running   1          2d7h
nginx                               2/2     Running   8          2d13h
nginx-volume                        1/1     Running   1          2d10h
wordpress-6cbb67575d-ztxxp          1/1     Running   1          2d7h

[root@docker-server1 svcs]# kubectl apply -f nginx-svc.yaml

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        2d18h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP       2d7h
nginx-svc    ClusterIP   10.96.235.140   <none>        80/TCP         5s
wordpress    NodePort    10.96.72.115    <none>        80:32080/TCP   2d7h

[root@docker-server1 svcs]# curl http://10.96.235.140

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

1.2 查看详细信息

[root@docker-server1 ~]# kubectl describe svc nginx-svc  

Name:              nginx-svc
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"nginx-svc","namespace":"default"},"spec":{"ports":[{"port":80,"ta...
Selector:          name=hello-deployment
Type:              ClusterIP
IP:                10.96.235.140
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.24:80,10.244.1.25:80,10.244.2.15:80
Session Affinity:  None
Events:            <none>

1.3 域的默认配置项

在/root/kubeadm-config.yaml 有这两个配置项

dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.17.0
networking:
  dnsDomain: cluster.local        #默认的域名后缀
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16

会配置两个coredns服务

[root@docker-server1 svcs]# kubectl get pods -n kube-system

NAME                                      READY   STATUS    RESTARTS   AGE
coredns-6955765f44-8kxdg                  1/1     Running   1          2d18h
coredns-6955765f44-m66bw                  1/1     Running   1          2d18h
etcd-192.168.132.131                      1/1     Running   1          2d9h
kube-apiserver-192.168.132.131            1/1     Running   2          2d18h
kube-controller-manager-192.168.132.131   1/1     Running   4          2d18h
kube-flannel-ds-amd64-dbckr               1/1     Running   1          2d18h
kube-flannel-ds-amd64-fg972               1/1     Running   1          2d18h
kube-flannel-ds-amd64-m9lgq               1/1     Running   1          2d18h
kube-proxy-7xgt9                          1/1     Running   1          2d18h
kube-proxy-k8kb7                          1/1     Running   1          2d18h
kube-proxy-q867d                          1/1     Running   1          2d18h
kube-scheduler-192.168.132.131            1/1     Running   4          2d18h 

[root@docker-server1 svcs]# kubectl get svc -n kube-system

NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   2d18h

1.4 进入容器查看dns

[root@docker-server1 svcs]# kubectl exec -it hello-deployment-5fdb46d67c-dqnnh /bin/bash

[root@docker-server1 svcs]# kubectl exec -it hello-deployment-5fdb46d67c-dqnnh /bin/bash
root@hello-deployment-5fdb46d67c-dqnnh:/# cat /etc/resolv.conf 
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

coredns会默认创建一个域名解析

1.5 创建一个busybox pod测试

[root@docker-server1 ~]# cd /yamls/deployment/

[root@docker-server1 deployment]# vi busybox-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: busybox
    namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      name: busybox
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 10%
      maxUnavailable: 0
  template:
    metadata:
      labels:
        name: busybox
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
          - /bin/sh
          - -c
          - "sleep 3600"

[root@docker-server1 deployment]# kubectl apply -f busybox-deployment.yaml

deployment.apps/busybox created

[root@docker-server1 deployment]# kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
busybox-546555c84-2psbb             1/1     Running   0          13s
goproxy                             1/1     Running   1          2d11h
hello-deployment-5fdb46d67c-dqnnh   1/1     Running   0          24m
hello-deployment-5fdb46d67c-s68tf   1/1     Running   0          24m
hello-deployment-5fdb46d67c-x5nwl   1/1     Running   0          24m
init-demo                           1/1     Running   1          2d10h
mysql-5d4695cd5-x9j2z               1/1     Running   1          2d7h
nginx                               2/2     Running   8          2d13h
nginx-volume                        1/1     Running   1          2d10h
wordpress-6cbb67575d-ztxxp          1/1     Running   1          2d7h

[root@docker-server1 deployment]# kubectl exec -it busybox-546555c84-2psbb /bin/sh

/ # cat /etc/resolv.conf 

nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

/ # nslookup  nginx-svc.default.svc.cluster.local

Server:        10.96.0.10
Address:    10.96.0.10:53

Name:    nginx-svc.default.svc.cluster.local
Address: 10.96.235.140

/ # ping nginx-svc.default.svc.cluster.local

PING nginx-svc.default.svc.cluster.local (10.96.235.140): 56 data bytes

[root@docker-server1 deployment]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        2d19h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP       2d7h
nginx-svc    ClusterIP   10.96.235.140   <none>        80/TCP         22m
wordpress    NodePort    10.96.72.115    <none>        80:32080/TCP   2d7h

1.6 Headles service设置

Headles service不适用clusterip,他直接解析到关联的POD

[root@docker-server1 svcs]# kubectl delete -f nginx-svc.yaml

[root@docker-server1 svcs]# vi nginx-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    name: hello-deployment
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 80
      targetPort: 80

[root@docker-server1 svcs]# kubectl apply -f nginx-svc.yaml 

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        2d19h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP       2d8h
nginx-svc    ClusterIP   None            <none>        80/TCP         10s
wordpress    NodePort    10.96.72.115    <none>        80:32080/TCP   2d7h

[root@docker-server1 deployment]# kubectl exec -it busybox-546555c84-2psbb /bin/sh

/ # ping   nginx-svc.default.svc.cluster.local

PING nginx-svc.default.svc.cluster.local (10.244.1.24): 56 data bytes
PING nginx-svc.default.svc.cluster.local (10.244.1.25): 56 data bytes
PING nginx-svc.default.svc.cluster.local (10.244.2.15): 56 data bytes

轮询解析到pod中

[root@docker-server1 svcs]# kubectl get pods -o wide |grep hello

hello-deployment-5fdb46d67c-dqnnh   1/1     Running   0          45m     10.244.1.25   192.168.132.132   <none>           <none>
hello-deployment-5fdb46d67c-s68tf   1/1     Running   0          45m     10.244.2.15   192.168.132.133   <none>           <none>
hello-deployment-5fdb46d67c-x5nwl   1/1     Running   0          45m     10.244.1.24   192.168.132.132   <none>           <none>

1.7 缺点

当外部访问使用映射的方式进行暴露端口进行访问时,业务比较多时,暴露大量的端口映射,同时效率不高

[root@docker-server1 svcs]# netstat -ntlp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:10248         0.0.0.0:*               LISTEN      967/kubelet         
tcp        0      0 127.0.0.1:10249         0.0.0.0:*               LISTEN      8386/kube-proxy     
tcp        0      0 192.168.132.131:2379    0.0.0.0:*               LISTEN      4581/etcd           
tcp        0      0 127.0.0.1:2379          0.0.0.0:*               LISTEN      4581/etcd           
tcp        0      0 192.168.132.131:2380    0.0.0.0:*               LISTEN      4581/etcd           
tcp        0      0 127.0.0.1:2381          0.0.0.0:*               LISTEN      4581/etcd           
tcp        0      0 127.0.0.1:10257         0.0.0.0:*               LISTEN      4467/kube-controlle 
tcp        0      0 127.0.0.1:40081         0.0.0.0:*               LISTEN      967/kubelet         
tcp        0      0 127.0.0.1:10259         0.0.0.0:*               LISTEN      4509/kube-scheduler 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1228/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1602/master         
tcp6       0      0 :::32443                :::*                    LISTEN      8386/kube-proxy     
tcp6       0      0 :::10250                :::*                    LISTEN      967/kubelet         
tcp6       0      0 :::10251                :::*                    LISTEN      4509/kube-scheduler 
tcp6       0      0 :::6443                 :::*                    LISTEN      4493/kube-apiserver 
tcp6       0      0 :::10252                :::*                    LISTEN      4467/kube-controlle 
tcp6       0      0 :::32080                :::*                    LISTEN      8386/kube-proxy     
tcp6       0      0 :::10256                :::*                    LISTEN      8386/kube-proxy     
tcp6       0      0 :::22                   :::*                    LISTEN      1228/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1602/master   

二 ingress的配置使用

2.1 介绍

使用ingress暴露端口

[root@docker-server1 svcs]# kubectl get deploy

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
busybox            1/1     1            1           34m
hello-deployment   3/3     3            3           58m
mysql              1/1     1            1           2d8h
wordpress          1/1     2            2           2d8h

[root@docker-server1 svcs]# kubectl edit deploy wordpress

007.kubernets的headless service配置和ingress的简单配置

[root@docker-server1 svcs]# kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
busybox-546555c84-2psbb             1/1     Running   0          34m
goproxy                             1/1     Running   1          2d11h
hello-deployment-5fdb46d67c-dqnnh   1/1     Running   0          58m
hello-deployment-5fdb46d67c-s68tf   1/1     Running   0          58m
hello-deployment-5fdb46d67c-x5nwl   1/1     Running   0          58m
init-demo                           1/1     Running   1          2d10h
mysql-5d4695cd5-x9j2z               1/1     Running   1          2d8h
nginx                               2/2     Running   9          2d13h
nginx-volume                        1/1     Running   1          2d11h
wordpress-6cbb67575d-bxdsk          1/1     Running   0          10s
wordpress-6cbb67575d-ztxxp          1/1     Running   1          2d8h

[root@docker-server1 svcs]# kubectl get deploy

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
busybox            1/1     1            1           34m
hello-deployment   3/3     3            3           58m
mysql              1/1     1            1           2d8h
wordpress          2/2     2            2           2d8h

使用ingress暴露端口

Kubernetes暴露服务的方式目前有三种:

  • LoadBlancer Service
  • NodePort Service
  • Ingress

虽然通过NodePort Service和LoadBlancer Service可以将集群内服务对外暴露。但事实上,各自又存在各自的问题:

  • LoadBalancer Service 通常用于与公有云厂商对接,当然也可以自行实现其接口以完成与企业自建的负载均衡器对接。事实上LoadBanlacer Service的工作机制就是调用外部的负载均衡器以实现服务暴露,这依托于外部的负载均衡器。

  • NodePort Service 它的实现机制其实就是在每个node节点上都开启一个端口,并通过iptables的dnat方式将这个宿主机端口映射至集群内部的service ip上。nodeport的问题是,当集群当中的服务越来越多的时候,在每个node上开启的端口也越来越多,最终我们要维护大量的端口映射关系,这使得业务管理工作变得非常复杂。

2.2 Ingress架构及原理

007.kubernets的headless service配置和ingress的简单配置

 

针对上述nodeport暴露服务的问题,其实可以有一个解决办法,即使用一个nginx/haproxy这样的负载均衡器,只监听在一个端口上,比如80或443,然后按照域名往后端转发。将这样的负载均衡器以pod的方式运行在集群中,并通过hostNetwork或者nodeport的方式只暴露负载均衡器监听的端口即可。

这里其实有一个问题,就是nginx/haproxy怎么知道什么域名应该转发至什么后端? 后端应用的pod如果发生变化,nginx/haproxy又应该如何感知到,并同步更新自己的配置文件然后重载配置?

而这就是ingress的作用。官方的ingress由三部分组成:

  • Ingress类型的资源:其实就是个规则文件,其定义流量的转发规则
  • Ingress Controller: 通过与kubernetes api交互,动态的去感知集群中ingress规则变化,然后读取它,再按照自己的模板生成一段nginx配置,再写到nginx pod里,最后reload一下nginx
  • Nginx:真正负责流量转发的负载均衡器

事实上,kubernetes已经将ingress Controller和nginx结合到一起,统称之ingress controller,所以在实际部署中,只需要部署ingress controller即可。

2.3 Nginx Ingress配置

官方托管代码位置:https://github.com/kubernetes/ingress-nginx

[root@docker-server1 svcs]# cd ../

[root@docker-server1 yamls]# mkdir ingress

[root@docker-server1 yamls]# cd ingress

[root@docker-server1 ingress]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

[root@docker-server1 ingress]# mv mandatory.yaml  nginx-controller.yaml

007.kubernets的headless service配置和ingress的简单配置

添加一个hostNetwork,同时如果在国内,镜像可能需要使用代理

[root@docker-server1 ingress]# kubectl apply -f nginx-controller.yaml 

namespace/ingress-nginx created
configmap/nginx-configuration created
configmap/tcp-services created
configmap/udp-services created
serviceaccount/nginx-ingress-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
deployment.apps/nginx-ingress-controller created
limitrange/ingress-nginx created

[root@docker-server1 ingress]# kubectl get ns

NAME                   STATUS   AGE
default                Active   2d20h
ingress-nginx          Active   35s
kube-node-lease        Active   2d20h
kube-public            Active   2d20h
kube-system            Active   2d20h
kubernetes-dashboard   Active   2d19h

[root@docker-server1 ingress]# kubectl get pods -n ingress-nginx

NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-5c6985f9cc-wkngv   1/1     Running   0          65s

[root@docker-server1 ingress]# kubectl get pods -n ingress-nginx -o wide

NAME                                        READY   STATUS    RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-5c6985f9cc-wkngv   1/1     Running   0          99s   192.168.132.132   192.168.132.132   <none>           <none>

2.3 配置一个ingress的描述文件

[root@docker-server1 ingress]# vim nginx-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: hello-ingress
spec:
  rules:
  - host: hello.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-svc
          servicePort: 80

[root@docker-server1 ingress]# kubectl apply -f nginx-ingress.yaml 

[root@docker-server1 ingress]# kubectl get ingress

NAME            HOSTS               ADDRESS   PORTS   AGE
hello-ingress   hello.example.com             80      6s

[root@docker-server1 ingress]# kubectl get ingress -o yaml

apiVersion: v1
items:
- apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"hello-ingress","namespace":"default"},"spec":{"rules":[{"host":"hello.example.com","http":{"paths":[{"backend":{"serviceName":"nginx-svc","servicePort":80},"path":"/"}]}}]}}
    creationTimestamp: "2020-01-12T14:27:19Z"
    generation: 1
    name: hello-ingress
    namespace: default
    resourceVersion: "126400"
    selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/hello-ingress
    uid: ea3585f5-dc5d-4df0-91c9-b8422bf4f27e
  spec:
    rules:
    - host: hello.example.com
      http:
        paths:
        - backend:
            serviceName: nginx-svc
            servicePort: 80
          path: /
  status:
    loadBalancer: {}
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

[root@docker-server1 ingress]# kubectl describe ingress hello-ingress

Name:             hello-ingress
Namespace:        default
Address:          
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host               Path  Backends
  ----               ----  --------
  hello.example.com  
                     /   nginx-svc:80 (10.244.1.24:80,10.244.1.25:80,10.244.2.15:80)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"hello-ingress","namespace":"default"},"spec":{"rules":[{"host":"hello.example.com","http":{"paths":[{"backend":{"serviceName":"nginx-svc","servicePort":80},"path":"/"}]}}]}}

Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  67s   nginx-ingress-controller  Ingress default/hello-ingress

[root@docker-server1 ingress]# kubectl get pods -n ingress-nginx

NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-5c6985f9cc-wkngv   1/1     Running   0          9m37s

2.4 进入容器查看

[root@docker-server1 ingress]# kubectl exec -it nginx-ingress-controller-5c6985f9cc-wkngv -n ingress-nginx /bin/bash

bash-5.0$ pwd
/etc/nginx

bash-5.0$ cat nginx.conf

007.kubernets的headless service配置和ingress的简单配置

[root@docker-server1 ingress]# kubectl get pods -n ingress-nginx -o wide

NAME                                        READY   STATUS    RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx-ingress-controller-5c6985f9cc-wkngv   1/1     Running   0          99s   192.168.132.132   192.168.132.132   <none>           <none>

解析到192.168.132.132,修改本机hosts

007.kubernets的headless service配置和ingress的简单配置

2.5 访问nginx

访问 http://hello.example.com/

007.kubernets的headless service配置和ingress的简单配置

 

 

2.6 使用ingress访问wordpress

[root@docker-server1 ingress]# vi wordpress-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: hello-world
spec:
  rules:
  - host: blog.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: wordpress
          servicePort: 80

[root@docker-server1 ingress]# kubectl apply -f wordpress-ingress.yaml 

[root@docker-server1 ingress]# kubectl get ingress

NAME            HOSTS               ADDRESS   PORTS   AGE
hello-ingress   hello.example.com             80      23m
hello-world     blog.example.com              80      49s

写入hosts文件解析并访问blog.example.com

007.kubernets的headless service配置和ingress的简单配置

但是转到32080

2.7 修复跳转问题

使用ingress时,不使用端口映射

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"wordpress","namespace":"default"},"spec":{"ports":[{"nodePort":32080,"port":80,"targetPort":80}],"selector":{"app":"wordpress"},"type":"NodePort"}}
  creationTimestamp: "2020-01-10T05:56:02Z"
  name: wordpress
  namespace: default
  resourceVersion: "101304"
  selfLink: /api/v1/namespaces/default/services/wordpress
  uid: 7d1c6de8-3d53-4235-9bd3-bb6e755aed2c
spec:
  clusterIP: 10.96.72.115
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 32080
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: wordpress
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

修改为

007.kubernets的headless service配置和ingress的简单配置

 [root@docker-server1 ingress]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    2d20h
mysql-svc    ClusterIP   10.96.240.104   <none>        3306/TCP   2d9h
nginx-svc    ClusterIP   None            <none>        80/TCP     75m
wordpress    ClusterIP   10.96.72.115    <none>        80/TCP     2d9h

[root@docker-server1 ingress]# curl -I -H "Host:blog.example.com"  http://192.168.132.132

HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.7
Date: Sun, 12 Jan 2020 15:01:35 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.26
X-Redirect-By: WordPress
Location: http://blog.example.com:32080/

依然发生转发,是因为在wordpress初始化的时候,把访问http://IP:32080写进数据库

重新安装一次

[root@docker-server1 ingress]# kubectl delete -f /yamls/deployment/mysql-deployment.yaml

[root@docker-server1 ingress]# kubectl delete -f /yamls/deployment/wordpress-deployment.yaml

[root@docker-server1 ingress]# kubectl delete -f /yamls/svcs/wordpress-svc.yaml

[root@docker-server1 ingress]# vi /yamls/svcs/wordpress-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress
spec:
  selector:
    app: wordpress
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 80

[root@docker-server1 ingress]# kubectl delete -f /yamls/svcs/mysql-svc.yaml

[root@docker-server1 ingress]# kubectl apply -f /yamls/deployment/mysql-deployment.yaml

[root@docker-server1 ingress]# kubectl apply -f /yamls/svcs/mysql-svc.yaml

[root@docker-server1 ingress]# kubectl apply -f /yamls/deployment/wordpress-deployment.yaml

[root@docker-server1 ingress]# kubectl apply -f /yamls/svcs/wordpress-svc.yaml

[root@docker-server1 ingress]# kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
busybox-546555c84-2psbb             1/1     Running   1          113m
goproxy                             1/1     Running   1          2d13h
hello-deployment-5fdb46d67c-dqnnh   1/1     Running   0          136m
hello-deployment-5fdb46d67c-s68tf   1/1     Running   0          136m
hello-deployment-5fdb46d67c-x5nwl   1/1     Running   0          136m
init-demo                           1/1     Running   1          2d12h
mysql-5d4695cd5-kzlms               1/1     Running   0          37s
nginx                               2/2     Running   10         2d15h
nginx-volume                        1/1     Running   1          2d12h
wordpress-6cbb67575d-b9md5          1/1     Running   0          22s

[root@docker-server1 ingress]# curl -I -H "Host:blog.example.com"  http://192.168.132.132

HTTP/1.1 302 Found
Server: nginx/1.17.7
Date: Sun, 12 Jan 2020 15:19:08 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.26
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
X-Redirect-By: WordPress
Location: http://blog.example.com/wp-admin/install.php

2.8 访问wordpress

访问http://blog.example.com

007.kubernets的headless service配置和ingress的简单配置

ingress配置访问wordpress完成


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

上一篇:003.kubernets对于namespace的管理


下一篇:kube-apiserver 配置参数解读(kubernets version 1.12.0)