Answers: <p>In the service you have selector <code>app: ch8app-nginx</code>, but in the deployment you have labels <code>app: ch8app</code>. They don't match.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-08-29T02:56:29.743Z
Answers: 2024-08-29T02:56:29.743Z
Answers: Almenon
Body: <p>I’m new to Kubernetes and learning about Deployments, Services, and Ingress as part of a CKAD video course.  I created a YAML file for a Deployment, and a separate YAML file for a Service, and I’d like to map or hook up the Service with the Deployment.  The container is a simple Nginx image, and I would like to be able to access the the Nginx server from the internal cluster IP address to begin with.</p>
  <p>The deployment:</p>
  <pre><code>user@k8s-box:~/ch8$ cat ch8_deploy.yml 
   apiVersion: apps/v1
   kind: Deployment
   metadata:
    creationTimestamp: null
    labels:
      app: ch8app
    name: ch8app-nginx
   spec:
    replicas: 5
    selector:
      matchLabels:
        app: ch8app
    strategy: 
      rollingUpdate:
        maxUnavailable: 2
    template:
      metadata:
        creationTimestamp: null
        labels:
          app: ch8app
      spec:
        containers:
        - image: nginx:latest
          name: nginx
          resources: {}
   status: {}
  </code></pre>
  <p>The ClusterIP service:</p>
  <pre><code>user@k8s-box:~/ch8$ cat ch8_svc.yml
  apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: null
    labels:
      app: ch8app
    name: ch8svc   
  spec:
    ports:
    - name: &quot;tcp80&quot;
      port: 80
      protocol: TCP
      targetPort: 80
    selector:
      app: ch8app-nginx  
    type: ClusterIP
  status:
    loadBalancer: {}
  </code></pre>
  <p>Then I load the files:</p>
  <pre><code>kubectl create -f ch8_deploy.yml
  kubectl create -f ch8_svc.yml
  </code></pre>
  <p>When I try to access port 80 with curl from the cluster internal IP address, I’m not able to connect.  What am I missing in the above that is causing the mismatch?  If I run the below, I’m able to connect successfully:</p>
  <pre><code>kubectl create -f ch8_deploy.yml
  kubectl expose deploy ch8app-nginx —-port=80
  </code></pre>
  <p>While the above works, it’s not something like the YAML file that I could commit to a git repo for reproducing the environment.</p>
  
Tags: <kubernetes><yaml>
Title: Kubernetes: Mapping a Service to a Deployment with YAML files
UserDisplayName: MicrocontrollersToo
ThreadText: # Kubernetes: Mapping a Service to a Deployment with YAML files
  
  (Asked by: username_0 on 2024-08-27)
  
  I’m new to Kubernetes and learning about Deployments, Services, and Ingress as part of a CKAD video course. I created a YAML file for a Deployment, and a separate YAML file for a Service, and I’d like to map or hook up the Service with the Deployment. The container is a simple Nginx image, and I would like to be able to access the the Nginx server from the internal cluster IP address to begin with.
  
  The deployment:
  
  ```
  user@k8s-box:~/ch8$ cat ch8_deploy.yml 
   apiVersion: apps/v1
   kind: Deployment
   metadata:
    creationTimestamp: null
    labels:
      app: ch8app
    name: ch8app-nginx
   spec:
    replicas: 5
    selector:
      matchLabels:
        app: ch8app
    strategy: 
      rollingUpdate:
        maxUnavailable: 2
    template:
      metadata:
        creationTimestamp: null
        labels:
          app: ch8app
      spec:
        containers:
        - image: nginx:latest
          name: nginx
          resources: {}
   status: {}
  
  ```
  
  The ClusterIP service:
  
  ```
  user@k8s-box:~/ch8$ cat ch8_svc.yml
  apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: null
    labels:
      app: ch8app
    name: ch8svc   
  spec:
    ports:
    - name: "tcp80"
      port: 80
      protocol: TCP
      targetPort: 80
    selector:
      app: ch8app-nginx  
    type: ClusterIP
  status:
    loadBalancer: {}
  
  ```
  
  Then I load the files:
  
  ```
  kubectl create -f ch8_deploy.yml
  kubectl create -f ch8_svc.yml
  
  ```
  
  When I try to access port 80 with curl from the cluster internal IP address, I’m not able to connect. What am I missing in the above that is causing the mismatch? If I run the below, I’m able to connect successfully:
  
  ```
  kubectl create -f ch8_deploy.yml
  kubectl expose deploy ch8app-nginx —-port=80
  
  ```
  
  While the above works, it’s not something like the YAML file that I could commit to a git repo for reproducing the environment.
  
  **Tags:** kubernetes, yaml
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-08-29. Score: 0 | Accepted answer
  
  In the service you have selector `app: ch8app-nginx`, but in the deployment you have labels `app: ch8app`. They don't match.
  
  
  ---
  
  