讲述deployment、service、ingress资源的lables关系

在实验之前,我们都知道,lable是k8s中内部找寻各个资源的依据,比如deployment需要跟那个pod资源进行绑定,通过lable。service资源如何跟pod资源进行绑定,通过lable。service资源如何跟service资源进行绑定,通过lable。

总结:

vi deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:                       
  name: nginx-dp      # (1)定义此Deployment资源的名字是nginx-dp,在 kubectl get deploy 中的 name 显示的名字,包括kubectl get pod 中
                       的name也是显示的此名字,这里有疑问,为什么不显示如下spec.template.spec.containers中的name: nginx,因为你是定义在deployment资源下
  namespace: default  # 在那个命名空间下,定义的资源都在此资源下,否则无法找到其他资源
  labels:             # (2)定义此Deployment资源的lable,以便namespace: default下的其他的资源找lables是app: nginx-lable的delpoynebt资源
    app: nginx-lable  # lables的名字app: nginx-lable
spec:
  selector:           # (3)此位置是选择标签,意思是此Deployment资源跟那个pod资源进行绑定
    matchLabels:
      app: nginx-1.7.9  # 此位置必须填写的是pod资源的label,也就是(4)
  replicas: 1
----------------------
  template:           # 元数据,开始定义pod
    metadata:
      labels:         # (4)定义pod资源的labels
        app: nginx-1.7.9
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

总结:
(1) 是kubectl get deploy 包括 kubectl get pod 中名字
(2) 定义的此Deployment资源的lable
(3) 选择标签,此Deployment资源跟那个pod资源进行绑定,写pod资源的label,也就是(4)
(4) 定义pod资源的labels

[root@hdss7-21 ~]# kubectl get deployment -owide
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES        SELECTOR
nginx-dp   1/1     1            1           10m   nginx        nginx:1.7.9   app=nginx-1.7.9
[root@hdss7-21 ~]# kubectl get pod -owide
NAME                        READY   STATUS             RESTARTS   AGE   IP           NODE                NOMINATED NODE   READINESS GATES
nginx-dp-7cbfbcdc75-rvq2s   1/1     Running            0          10m   172.7.21.4   hdss7-21.host.com              

[root@hdss7-21 ~]# curl 172.7.21.4



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

[root@hdss7-21 ~]#

vi svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc   # (5)定义此Service资源的的名字是啥
  namespace: default    # 定义此service资源所在的名称空间,必须跟dedployment资源、pod资源在同一个名称空间下
  labels:       # 定义此servcice资源的lables,以便namespace: default下的其他的资源找lables是app: nodeport-lable的
    app: nginx-svc-label
spec:
  selector:   # (6)标签选择器,选择要跟那个pod资源进行绑定,是pod资源label名称
    app: nginx-1.7.9
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

总结:

(6) 一定要跟pod资源的label相同,也就是(4),而不是(2),deployment资源,因为访问service资源,代理的是后端的pod。

[root@hdss7-21 ~]# kubectl get svc -owide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE    SELECTOR
kubernetes   ClusterIP   192.168.0.1              443/TCP   115d   
nginx-svc    ClusterIP   192.168.58.8             80/TCP    8m6s   app=nginx-1.7.9

[root@hdss7-11 ~]# vi /var/named/od.com.zone

$ORIGIN od.com.
$TTL 600        ; 10 minutes
@               IN SOA  dns.od.com. dnsadmin.od.com. (
                                2020010501 ; serial
                                10800      ; refresh (3 hours)
                                900        ; retry (15 minutes)
                                604800     ; expire (1 week)
                                86400      ; minimum (1 day)
                                )
                                NS   dns.od.com.
$TTL 60 ; 1 minute
dns                A    10.4.7.11
nginx              A    10.4.7.10

[root@hdss7-11 ~]# systemctl restart named
[root@hdss7-11 ~]# dig -t A nginx.od.com +short @10.4.7.11
10.4.7.10

vi ingress.yaml

kind: Ingress
apiVersion: extensions/v1beta1
metadata: 
  name: nginx-ingress
  namespace: default
  labels:
    app: nginx-ingress-label
spec:
  rules:
  - host: nginx.od.com
    http:
      paths:
      - path: /
        backend: 
          serviceName: nginx-svc   # 选择Service资源的的名字
          servicePort: 80
[root@hdss7-21 ~]# curl nginx.od.com



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

你可能感兴趣的:(k8s,java,运维,docker)