DNS for StatefulSet

StatefulSet was described in Chapter 3, Playing with Containers. It assigns a pod name with a sequence number—for example, my-nginx-0, my-nginx-1, my-nginx-2. StatefulSet also uses these pod names to assign a DNS entry instead of IP addresses. Because it uses Kubernetes Service, FQDN appear as follows: <StatefulSet name>-<sequence number>.<Service name>.<Namespace name>.svc.cluster.local.

Let's create StatefulSet to examine how DNS works in StatefulSet:

  1. Prepare StatefulSet and normal service YAML configurations as follows:
$ cat nginx-sts.yaml 
apiVersion: v1
kind: Service
metadata:
name: nginx-sts-svc
labels:
app: nginx-sts
spec:
ports:
- port: 80
selector:
app: nginx-sts
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: nginx-sts
spec:
serviceName: "nginx-sts-svc"
replicas: 3
template:
metadata:
labels:
app: nginx-sts
spec:
containers:
- name: nginx-sts
image: nginx
ports:
- containerPort: 80
restartPolicy: Always
  1. Create StatefulSet on chap8-domain2:
$ kubectl create -f nginx-sts.yaml --namespace=chap8-domain2
service "nginx-sts-svc" created
statefulset "nginx-sts" created
  1. Use the kubectl command to check the status of the pod and service creation:
//check StatefulSet (in short sts)
$ kubectl get sts --namespace=chap8-domain2
NAME DESIRED CURRENT AGE
nginx-sts 3 3 46s


//check Service (in short svc)
$ kubectl get svc nginx-sts-svc --namespace=chap8-domain2
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-sts-svc ClusterIP 10.104.63.124 <none> 80/TCP 8m


//check Pod with "-o wide" to show an IP address
$ kubectl get pods --namespace=chap8-domain2 -o wide
NAME READY STATUS RESTARTS AGE IP NODE
my-apache-55fb679f49-z9gsr 1/1 Running 1 22h 172.17.0.4 minikube
nginx-sts-0 1/1 Running 0 2m 172.17.0.2 minikube
nginx-sts-1 1/1 Running 0 2m 172.17.0.9 minikube
nginx-sts-2 1/1 Running 0 1m 172.17.0.10 minikube
  1. Launch the busybox pod in the foreground:
$ kubectl run -it busybox --restart=Never --image=busybox 
  1. Use the nslookup command to query the service's IP address:
# nslookup nginx-sts-svc.chap8-domain2.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: nginx-sts-svc.chap8-domain2.svc.cluster.local
Address 1: 10.104.63.124 nginx-sts-svc.chap8-domain2.svc.cluster.local
  1. Use the nslookup command to query the individual pod's IP address:
# nslookup nginx-sts-0.nginx-sts-svc.chap8-domain2.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx-sts-0.nginx-sts-svc.chap8-domain2.svc.cluster.local
Address 1: 172.17.0.2 nginx-sts-0.nginx-sts-svc.chap8-domain2.svc.cluster.local


# nslookup nginx-sts-1.nginx-sts-svc.chap8-domain2.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx-sts-1.nginx-sts-svc.chap8-domain2.svc.cluster.local
Address 1: 172.17.0.9 nginx-sts-1.nginx-sts-svc.chap8-domain2.svc.cluster.local


# nslookup nginx-sts-2.nginx-sts-svc.chap8-domain2.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx-sts-2.nginx-sts-svc.chap8-domain2.svc.cluster.local
Address 1: 172.17.0.10 nginx-sts-2.nginx-sts-svc.chap8-domain2.svc.cluster.local
  1. Clean up the busybox pod:
# exit
$ kubectl delete pod busybox
pod "busybox" deleted
..................Content has been hidden....................

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