当前位置: 首页 > 知识库问答 >
问题:

如何使用一个YAML部署两个服务器,其中一个依赖于Kubernetes中的另一个

齐招
2023-03-14

我有两个应用程序,nginx和redis,其中nginx使用redis缓存一些数据,因此必须在nginx中配置redis地址。

一方面,我可以首先应用redis部署并获得它的IP,然后应用nginx部署来在我的MiniKube中设置这两个应用程序。

但另一方面,为了简化Kubernetes仪表板中用于QA的安装,我希望创建一个Kubernetes YAML文件(如GoogleCloudPlatform/microservices-demo/kubernetes-manifests.YAML)来将这两个应用程序部署到两个不同的吊舱上。但是,如果我通过环境变量来实现,我就无法获得redis地址。

那么我该如何实现呢?

apiVersion: apps/v1
kind: Deployment
metadata:
 name: redis-master
 labels:
  app: redis
spec:
 selector:
  matchLabels:
   app: redis
   role: master
   tier: backend
 replicas: 2
 template:
  metadata:
   labels:
    app: redis
    role: master
    tier: backend
  spec:
   containers:
   - name: master-c
     image: docker.io/redis:alpine
     ports:
     - containerPort: 6379
---
apiVersion: apps/v1
kind: Deployment
metadata:
 name: my-nginx
spec:
 selector: # Defines how the Deployment finds which Pods to manage.
  matchLabels:
   app: my-nginx
 template:
  metadata: # Defines what the newly created Pods are labeled.
   labels:
    app: my-nginx
    tier: frontend
  spec:
   terminationGracePeriodSeconds: 5
    containers:
    - name: my-nginx # Defines container name
      image: my-nginx:dev # docker image load -i my-nginx-docker_image.tar
      imagePullPolicy: Never # Always, IfNotPresent (default), Never
      ports:
      env:
      - name: NGINX_ERROR_LOG_SEVERITY_LEVEL
        value: debug
      - name: MY_APP_REDIS_HOST
        # How to use the IP address of the POD with redis-master labeled that is created by the previous deployment?
        value: 10.86.50.235
        # https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
        # valueFrom:
        #  fieldRef:
        #   fieldPath: status.podIP # this is the current POD IP
      - name: MY_APP_CLIENT_ID
        value: client_id
      - name: MY_APP_CLIENT_SECRET
        # https://kubernetes.io/docs/concepts/configuration/secret
        value: client_secret
---
# https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service
apiVersion: v1
kind: Service
# https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
# https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
# metadata - Data that helps uniquely identify the object, including a name string, UID, and optional namespace
metadata:
 name: my-nginx
spec:
 type: NodePort
 selector:
  # Defines a proper selector for your pods with corresponding `.metadata.labels` field. 
  # Verify it using: kubectl get pods --selector app=my-nginx || kubectl get pod -l app=my-nginx
  # Make sure the service points to correct pod by, for example, `kubectl describe pod -l app=my-nginx`
  app: my-nginx
 ports:
 # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
 - name: http
   port: 6080
   targetPort: 80
   # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
   nodePort: 30080
 - name: https
   port: 6443
   targetPort: 443
   nodePort: 30443

增加了一些网络输出,

    
    Microsoft Windows [Version 10.0.18362.900]
    (c) 2019 Microsoft Corporation. All rights reserved.
    
    PS C:\Users\ssfang> kubectl get pods
    NAME                            READY   STATUS    RESTARTS   AGE
    my-nginx-pod                    1/1     Running   9          5d14h
    redis-master-7db899bccb-npl6s   1/1     Running   3          2d15h
    redis-master-7db899bccb-rgx47   1/1     Running   3          2d15h
    
    C:\Users\ssfang> kubectl exec redis-master-7db899bccb-npl6s -- cat /etc/resolv.conf
    nameserver 10.96.0.10
    search default.svc.cluster.local svc.cluster.local cluster.local
    options ndots:5
    
    C:\Users\ssfang> kubectl exec my-nginx-pod -- cat /etc/resolv.conf
    nameserver 10.96.0.10
    search default.svc.cluster.local svc.cluster.local cluster.local
    options ndots:5
    
    C:\Users\ssfang> kubectl -n kube-system get svc
    NAME                                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
    ingress-nginx-controller-admission   ClusterIP   10.108.221.2   <none>        443/TCP                  7d11h
    kube-dns                             ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   7d17h
    
    
    C:\Users\ssfang> kubectl get ep kube-dns --namespace=kube-system
    NAME       ENDPOINTS                                                 AGE
    kube-dns   172.17.0.2:53,172.17.0.5:53,172.17.0.2:9153 + 3 more...   7d17h
    
    C:\Users\ssfang> kubectl get ep kube-dns --namespace=kube-system -o=yaml
    apiVersion: v1
    kind: Endpoints
    metadata:
      annotations:
        endpoints.kubernetes.io/last-change-trigger-time: "2020-07-09T02:08:35Z"
      creationTimestamp: "2020-07-01T09:34:44Z"
      labels:
        k8s-app: kube-dns
        kubernetes.io/cluster-service: "true"
        kubernetes.io/name: KubeDNS
      managedFields:
      - apiVersion: v1
        fieldsType: FieldsV1
        fieldsV1:
          f:metadata:
            f:annotations:
              .: {}
              f:endpoints.kubernetes.io/last-change-trigger-time: {}
            f:labels:
              .: {}
              f:k8s-app: {}
              f:kubernetes.io/cluster-service: {}
              f:kubernetes.io/name: {}
          f:subsets: {}
        manager: kube-controller-manager
        operation: Update
        time: "2020-07-09T02:08:35Z"
      name: kube-dns
      namespace: kube-system
      resourceVersion: "523617"
      selfLink: /api/v1/namespaces/kube-system/endpoints/kube-dns
    subsets:
    - addresses:
        nodeName: minikube
        targetRef:
          kind: Pod
          namespace: kube-system
          resourceVersion: "523566"
          uid: ed3a9f46-718a-477a-8804-e87511db16d1
      - ip: 172.17.0.5
        nodeName: minikube
        targetRef:
          kind: Pod
          name: coredns-546565776c-hmm5s
          namespace: kube-system
          resourceVersion: "523616"
          uid: ae21c65c-e937-4e3d-8a7a-636d4f780855
      ports:
      - name: dns-tcp
        port: 53
        protocol: TCP
      - name: metrics
        port: 9153
        protocol: TCP
      - name: dns
        port: 53
        protocol: UDP
    C:\Users\ssfang> kubectl get service
    NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                         AGE
    kubernetes           ClusterIP   10.96.0.1     <none>        443/TCP                         7d20h
    my-nginx-service     NodePort    10.98.82.96   <none>        6080:30080/TCP,6443:30443/TCP   7d13h
    
    PS C:\Users\ssfang> kubectl describe pod/my-nginx-pod | findstr IP
    IP:           172.17.0.8
    IPs:
      IP:  172.17.0.8
    PS C:\Users\ssfang> kubectl describe service/my-nginx-service | findstr IP
    IP:                       10.98.82.96
    
    C:\Users\ssfang> kubectl describe pod/my-nginx-65ffdfb5b5-dzgjk | findstr IP
    IP:           172.17.0.4
    IPs:
      IP:           172.17.0.4
    

以两个带有nginx的吊舱进行网络巡检为例,

  1. C:\users\ssfang>kubectl exec my-nginx-pod-it--bash
    
    # How to install nslookup, dig, host commands in Linux
    apt-get install dnsutils -y # In ubuntu
    yum install bind-utils -y # In RHEL/Centos
    
    root@my-nginx-pod:/etc# apt update && apt-get install -y dnsutils iputils-ping
    
    root@my-nginx-pod:/etc# nslookup my-nginx-service
    Server:         10.96.0.10
    Address:        10.96.0.10#53
    
    Name:   my-nginx-service.default.svc.cluster.local
    Address: 10.98.82.96
    
    root@my-nginx-pod:/etc# nslookup my-nginx-pod
    Server:         10.96.0.10
    Address:        10.96.0.10#53
    
    ** server can't find my-nginx-pod: SERVFAIL
    
    
    root@my-nginx-pod:/etc# ping -c3 -W60 my-nginx-pod
    PING my-nginx-pod (172.17.0.8) 56(84) bytes of data.
    64 bytes from my-nginx-pod (172.17.0.8): icmp_seq=1 ttl=64 time=0.011 ms
    64 bytes from my-nginx-pod (172.17.0.8): icmp_seq=2 ttl=64 time=0.021 ms
    64 bytes from my-nginx-pod (172.17.0.8): icmp_seq=3 ttl=64 time=0.020 ms
    
    --- my-nginx-pod ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2065ms
    rtt min/avg/max/mdev = 0.011/0.017/0.021/0.005 ms
    
    root@my-nginx-pod:/etc# ping -c3 -W20 my-nginx-service
    PING my-nginx-service.default.svc.cluster.local (10.98.82.96) 56(84) bytes of data.
    
    --- my-nginx-service.default.svc.cluster.local ping statistics ---
    3 packets transmitted, 0 received, 100% packet loss, time 2060ms
    
    
    root@my-nginx-pod:/etc# ping -c3 -W20 my-nginx-pod.default.svc.cluster.local
    ping: my-nginx-pod.default.svc.cluster.local: Name or service not known
    
    root@my-nginx-pod:/etc# ping -c3 -W20 my-nginx-service.default.svc.cluster.local
    PING my-nginx-service.default.svc.cluster.local (10.98.82.96) 56(84) bytes of data.
    
    --- my-nginx-service.default.svc.cluster.local ping statistics ---
    3 packets transmitted, 0 received, 100% packet loss, time 2051ms
    
    
    root@my-nginx-65ffdfb5b5-dzgjk:/etc# ping -c3 -W20 my-nginx-pod.default.svc.cluster.local
    ping: my-nginx-pod.default.svc.cluster.local: Name or service not known
    root@my-nginx-65ffdfb5b5-dzgjk:/etc# ping -c3 -W20 my-nginx-service.default.svc.cluster.local
    ping: my-nginx-service.default.svc.cluster.local: Name or service not known
    
    root@my-nginx-65ffdfb5b5-dzgjk:/etc# ping -c3 -W20 172.17.0.8
    PING 172.17.0.8 (172.17.0.8) 56(84) bytes of data.
    64 bytes from 172.17.0.8: icmp_seq=1 ttl=64 time=0.195 ms
    64 bytes from 172.17.0.8: icmp_seq=2 ttl=64 time=0.039 ms
    64 bytes from 172.17.0.8: icmp_seq=3 ttl=64 time=0.039 ms
    
    --- 172.17.0.8 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2055ms
    rtt min/avg/max/mdev = 0.039/0.091/0.195/0.073 ms


                             _             _
                _         _ ( )           ( )
      ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
    /' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
    | ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
    (_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
    
    $ ping default.svc.cluster.local
    ping: bad address 'default.svc.cluster.local'
    $ ping my-nginx-pod.default.svc.cluster.local
    ping: bad address 'my-nginx-pod.default.svc.cluster.local'
    $ ping my-nginx-service.default.svc.cluster.local
    ping: bad address 'my-nginx-service.default.svc.cluster.local'
    $ nslookup whoami
    Server:         10.86.50.1
    Address:        10.86.50.1:53
    
    ** server can't find whoami: NXDOMAIN
    
    ** server can't find whoami: NXDOMAIN
    
    $ ping -c3 -W20 172.17.0.8
    PING 172.17.0.8 (172.17.0.8): 56 data bytes
    64 bytes from 172.17.0.8: seq=0 ttl=64 time=0.053 ms
    64 bytes from 172.17.0.8: seq=1 ttl=64 time=0.035 ms
    64 bytes from 172.17.0.8: seq=2 ttl=64 time=0.040 ms
    
    --- 172.17.0.8 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.035/0.042/0.053 ms
    $ ping -c3 -W20 172.17.0.4
    PING 172.17.0.4 (172.17.0.4): 56 data bytes
    64 bytes from 172.17.0.4: seq=0 ttl=64 time=0.070 ms
    64 bytes from 172.17.0.4: seq=1 ttl=64 time=0.039 ms
    64 bytes from 172.17.0.4: seq=2 ttl=64 time=0.038 ms
    
    --- 172.17.0.4 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.038/0.049/0.070 ms

共有1个答案

林鸿彩
2023-03-14

硬编码IP地址不是一个好的做法。相反,您还可以为redis创建一个服务,并使用kubernetes dns配置在nginx部署中配置服务dns名称,如下my-svc.my-namespace.svc.cluster-domain.example所示。然后,您的nginx将通过该服务与redis容器通信。

 类似资料:
  • 问题内容: 我经历了太多的代码,试图找出我做错了什么,但我却步履蹒跚,浪费时间。这将非常简单,但我无法弄清楚。 我正在开发一个Android应用程序,需要将2个微调器放在一个活动上。第二个微调器将根据第一个微调器上选择的条目进行填充。听起来很简单吧? 所以这就是我想做的。如果用户选择Mobile,则应显示中的内容,当用户选择Large business 时应运行,最后一个条目应调用。我得到的只是一

  • 问题内容: 我已经开发了两个Springboot michroservices exservice1和service2。具有service1依赖性的service2微服务。 我在service2 pom文件中添加了service1的依赖关系,并在运行service2时在eclipse中可以正常工作。但是,当我在Jenkins中部署并为service1创建项目(Jenkins Job)并以maven

  • 例如,我想要: 如果用户已指定增量,请使用它。如果不是,请使用依赖于 n 的值。这可能吗?

  • 尝试从同一命名空间中的另一个服务连接到一个服务。使用ClusterIP创建服务。创建服务后使用该Ip访问服务。请求有时成功,有时失败,我看到两个pod都启动并运行。以下是服务配置

  • 我想创建一个自动应用其他插件(外部插件)的插件。这需要在调用“apply plugin”之前设置插件的buildscript依赖项。然而,我似乎无法在插件中添加buildscript依赖项,或者我得到:您无法更改未处于未解析状态的配置! 有解决办法吗? 我的示例(非工作)代码:

  • 我为欢迎屏幕设计了一个接口,其中有一个JFrame,包括两个JPanel(JPanel1在右边,JPanel2在左边)。左边的按钮用于切换JPanel1中的面板。我想按下一个按钮,用另一个JPanel替换JPanel1内容,但我不知道怎么做。请帮忙。