Linking Service to Pods or ReplicaSets using label selectors

As of Kubernetes version 1.9, Service only supports the equality-based selector to bind to Pods or ReplicaSet.

Let's create one Service that binds to nginx, which belongs to the production environment and the pilot project. Remember that nginx also belongs to the frontend tier:

//check your selector filter is correct or not
$ kubectl get pods -l 'environment=production,project=pilot,tier=frontend'
NAME READY STATUS RESTARTS AGE
pilot.prod.nginx 1/1 Running 0 19m


//create Service yaml that specify selector
$ cat pilot-nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: pilot-nginx-svc
spec:
type: NodePort
ports:
- protocol: TCP
port: 80
selector:
project: pilot
environment: production
tier: frontend


//create pilot-nginx-svc
$ kubectl create -f pilot-nginx-svc.yaml
service "pilot-nginx-svc" created

Here is the equivalent, where you can use the kubectl expose command to specify the label selector:

$ kubectl expose pod pilot.prod.nginx --name=pilot-nginx-svc2 --type=NodePort --port=80 --selector="project=pilot,environment=develop,tier=frontend"
service "pilot-nginx-svc2" exposed

Based on your Kubernetes environment, if you are using minikube, it is easier to check your Service with minikube service <Service name>, as in the following screenshot. If you are not using minikube, access to any Kubernetes node and assigned Service port number. For the following screenshot, it would be <node ip>:31981 or <node ip>:31820:

Access to Service which is running on minikube
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.248.162