Skip to content

Instantly share code, notes, and snippets.

@coresolve
Last active November 20, 2017 18:39
Show Gist options
  • Save coresolve/c2124d472e3a919b7eb4df808ee6c1ae to your computer and use it in GitHub Desktop.
Save coresolve/c2124d472e3a919b7eb4df808ee6c1ae to your computer and use it in GitHub Desktop.
service with multiple deployments

You can create a couple of deployments with the following commands:

kubectl run echo1 --image=gcr.io/google_containers/echoserver:1.4 --replicas=2 --labels=app=echo,version=1.4 --port=8080

kubectl run echo2 --image=gcr.io/google_containers/echoserver:1.5 --replicas=2 --labels=app=echo,version=1.5 --port=8080

Now we have 4 pods running 2 from each deployment. Let's take a look at the labels associated with them.

$ kubectl get pods --show-labels
NAME                     READY     STATUS    RESTARTS   AGE       LABELS
echo1-2780720731-02c31   1/1       Running   0          2m        app=echo,pod-template-hash=2780720731,version=1.4
echo1-2780720731-g3v0q   1/1       Running   0          2m        app=echo,pod-template-hash=2780720731,version=1.4
echo2-3071610666-b731z   1/1       Running   0          1m        app=echo,pod-template-hash=3071610666,version=1.5
echo2-3071610666-rc3cf   1/1       Running   0          1m        app=echo,pod-template-hash=3071610666,version=1.5

Now we can create a service with the correct selector We can test that we have the right selector with the kubectl get pods -l <selector> commands. This uses the same selector mechanism that our service will use.

$ kubectl get pods -l app=echo --show-labels
NAME                     READY     STATUS    RESTARTS   AGE       LABELS
echo1-2780720731-02c31   1/1       Running   0          5m        app=echo,pod-template-hash=2780720731,version=1.4
echo1-2780720731-g3v0q   1/1       Running   0          5m        app=echo,pod-template-hash=2780720731,version=1.4
echo2-3071610666-b731z   1/1       Running   0          5m        app=echo,pod-template-hash=3071610666,version=1.5
echo2-3071610666-rc3cf   1/1       Running   0          5m        app=echo,pod-template-hash=3071610666,version=1.5

$ kubectl get pods -l app=echo,version=1.4 --show-labels
NAME                     READY     STATUS    RESTARTS   AGE       LABELS
echo1-2780720731-02c31   1/1       Running   0          5m        app=echo,pod-template-hash=2780720731,version=1.4
echo1-2780720731-g3v0q   1/1       Running   0          5m        app=echo,pod-template-hash=2780720731,version=1.4

For our purposes we want to expose both deployments. We can do that by selecting app=echo as described above.

$ kubectl create service clusterip echo --tcp=80:8080 -o yaml --dry-run
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: echo
  name: echo
spec:
  ports:
  - name: 80-8080
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: echo
  type: ClusterIP
status:
  loadBalancer: {}

In the example above the service selector is app: echo. This will match the labels for all 4 of our pods.

We can use the kubectl get endpoints command to pull back the list of endpoints that are behind a service.

I am going to throw in a jsonpath stuff to make it easier to see.

$ kubectl get endpoints echo -o jsonpath='{.subsets[0].addresses[*].targetRef.name}'
echo2-3071610666-rc3cf echo1-2780720731-g3v0q echo1-2780720731-02c31 echo2-3071610666-b731z

$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
echo1-2780720731-02c31   1/1       Running   0          22m
echo1-2780720731-g3v0q   1/1       Running   0          22m
echo2-3071610666-b731z   1/1       Running   0          22m
echo2-3071610666-rc3cf   1/1       Running   0          22m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment