Service Magic

Create a Namespace:

kubectl create namespace funstuff
kubectl config set-context --current --namespace=funstuff

Deploy mypython

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mypython-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mypython
  template:
    metadata:
      labels:
        app: mypython
    spec:
      containers:
      - name: mypython
        image: quay.io/rhdevelopers/mypython:v1
        ports:
        - containerPort: 8000
EOF

Deploy mygo

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mygo-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mygo
  template:
    metadata:
      labels:
        app: mygo
    spec:
      containers:
      - name: mygo
        image: quay.io/rhdevelopers/mygo:v1
        ports:
        - containerPort: 8000
EOF

Deploy mynode

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mynode-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mynode
  template:
    metadata:
      labels:
        app: mynode
    spec:
      containers:
      - name: mynode
        image: quay.io/rhdevelopers/mynode:v1
        ports:
        - containerPort: 8000
EOF
watch kubectl get pods --show-labels
NAME                                   READY   STATUS    RESTARTS   AGE     LABELS
mygo-deployment-6d944c5c69-kcvmk       1/1     Running   0          2m11s   app=mygo,pod-template-hash=6d944c5c69
mynode-deployment-fb5457c5-hhz7h       1/1     Running   0          2m1s    app=mynode,pod-template-hash=fb5457c5
mypython-deployment-6874f84d85-2kpjl   1/1     Running   0          3m53s   app=mypython,pod-template-hash=6874f84d85
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: mystuff
spec:
  ports:
  - name: http
    port: 8000
  selector:
    inservice: mypods
  type: LoadBalancer
EOF
kubectl describe service my-service
kubectl get endpoints
NAME         ENDPOINTS   AGE
my-service   <none>      2m6s
kubectl get endpoints my-service -o json | jq '.subsets[].addresses[].ip'
jq: error (at <stdin>:18): Cannot iterate over null (null)
  • Minikube

  • Hosted

IP=$(minikube ip -p devnation)
PORT=$(kubectl get service/my-service -o jsonpath="{.spec.ports[*].nodePort}")

If using a hosted Kubernetes cluster like OpenShift then use curl and the EXTERNAL-IP address with port 8080 or get it using kubectl:

IP=$(kubectl get service my-service -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
PORT=$(kubectl get service my-service -o jsonpath="{.spec.ports[*].port}")
If you are in AWS, you need to get the hostname instead of ip.
IP=$(kubectl get service my-service -o jsonpath="{.status.loadBalancer.ingress[0].hostname}")

Curl the Service:

curl $IP:$PORT

And run loop script:

while true
do curl $IP:$PORT
sleep 0.8
done
curl: (7) Failed to connect to 35.224.233.213 port 8000: Connection refused
curl: (7) Failed to connect to 35.224.233.213 port 8000: Connection refused
kubectl label pod -l app=mypython inservice=mypods
curl: (7) Failed to connect to 35.224.233.213 port 8000: Connection refused
Python Hello on mypython-deployment-6874f84d85-2kpjl
Python Hello on mypython-deployment-6874f84d85-2kpjl
Python Hello on mypython-deployment-6874f84d85-2kpjl
kubectl label pod -l app=mynode inservice=mypods
Python Hello on mypython-deployment-6874f84d85-2kpjl
Python Hello on mypython-deployment-6874f84d85-2kpjl
Node Hello on mynode-deployment-fb5457c5-hhz7h 0
Node Hello on mynode-deployment-fb5457c5-hhz7h 1
Python Hello on mypython-deployment-6874f84d85-2kpjl
Python Hello on mypython-deployment-6874f84d85-2kpjl
Python Hello on mypython-deployment-6874f84d85-2kpjl
kubectl label pod -l app=mygo inservice=mypods
Node Hello on mynode-deployment-fb5457c5-hhz7h 59
Node Hello on mynode-deployment-fb5457c5-hhz7h 60
Go Hello on mygo-deployment-6d944c5c69-kcvmk
Python Hello on mypython-deployment-6874f84d85-2kpjl
Python Hello on mypython-deployment-6874f84d85-2kpjl
kubectl get endpoints my-service -o json | jq '.subsets[].addresses[].ip'
"10.130.2.43"
"10.130.2.44"
"10.130.2.45"

See the Pod IPs:

kubectl get pods -o wide

Remove mypython Pod from the Service:

kubectl label pod -l app=mypython inservice-
kubectl get endpoints my-service -o json | jq '.subsets[].addresses[].ip'
"10.130.2.44"
"10.130.2.45"

Clean Up

kubectl delete namespace funstuff