Skip to content

Commit bbd225f

Browse files
authored
Create Readme.md
1 parent 2d9921f commit bbd225f

1 file changed

Lines changed: 209 additions & 0 deletions

File tree

Kubernetese/StatefulSets/Readme.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
2+
# kubectl create -f web.yml
3+
4+
You will need to use two terminal windows.
5+
6+
In the first terminal, use kubectl get to watch the creation of the StatefulSet's Pods.
7+
8+
# kubectl get pods -w -l app=nginx
9+
10+
In the second terminal, use kubectl create to create the headless Service and StatefulSet defined in web.yaml.
11+
12+
# kubectl create -f web.yaml
13+
14+
# kubectl get service nginx
15+
16+
# kubectl get statefulset web
17+
18+
# kubectl get pods -w -l app=nginx
19+
20+
When Pods are being deployed, they are created sequentially, ordered from {0..n-1}
21+
Notice that the web-1 Pod is not launched until the web-0 Pod is Running and Ready
22+
23+
Pods in a StatefulSet
24+
***********************
25+
Examining the Pod's Ordinal Index
26+
27+
# kubectl get pods -l app=nginx
28+
29+
30+
Note that, the Pods' names take the form <statefulset name>-<ordinal index>. Since the web StatefulSet has two replicas, it creates two Pods, web-0 and web-1.
31+
32+
Stable Network Identities
33+
*************************
34+
35+
Each Pod has a stable hostname based on its ordinal index.
36+
37+
for i in 0 1; do kubectl exec "web-$i" -- sh -c 'hostname'; done
38+
39+
*****************
40+
41+
Use kubectl run to execute a container that provides the nslookup command from the dnsutils package. Using nslookup on the Pods' hostnames,
42+
you can examine their in-cluster DNS addresses:
43+
44+
# kubectl run -i --tty --image busybox:1.28 dns-test --restart=Never --rm
45+
46+
above command starts a new shell. In that new shell, run:
47+
48+
# Run this in the dns-test container shell
49+
50+
nslookup web-0.nginx
51+
nslookup web-1.nginx
52+
53+
and now exit the container shell: exit
54+
55+
The CNAME of the headless service points to SRV records (one for each Pod that is Running and Ready).
56+
The SRV records point to A record entries that contain the Pods' IP addresses.
57+
58+
****************************
59+
Delete the pods in statefulset
60+
61+
# kubectl delete pod -l app=nginx
62+
63+
Notice that StatefulSet will restart them, and both Pods will transition to Running and Ready:
64+
65+
# kubectl get pod -w -l app=nginx
66+
67+
Use kubectl exec and kubectl run to view the Pods' hostnames and in-cluster DNS entries. First, view the Pods' hostnames:
68+
69+
for i in 0 1; do kubectl exec web-$i -- sh -c 'hostname'; done
70+
71+
Next run:
72+
73+
kubectl run -i --tty --image busybox:1.28 dns-test --restart=Never --rm /bin/sh
74+
75+
which starts a new shell.
76+
In that new shell, run:
77+
78+
nslookup web-0.nginx
79+
80+
The output is similar to:
81+
82+
Server: 10.0.0.10
83+
Address 1: 10.0.0.10 kube-dns.kube-system.svc.cluster.local
84+
85+
Name: web-0.nginx
86+
Address 1: 10.244.1.7
87+
88+
nslookup web-1.nginx
89+
Server: 10.0.0.10
90+
Address 1: 10.0.0.10 kube-dns.kube-system.svc.cluster.local
91+
92+
Name: web-1.nginx
93+
Address 1: 10.244.2.8
94+
95+
The Pods' ordinals, hostnames, SRV records, and A record names have not changed, but the IP addresses associated with the Pods may have changed.
96+
97+
This is why it is important not to configure other applications to connect to Pods in a StatefulSet by IP address.
98+
*************************************
99+
100+
Stable storage and statefulset
101+
*************************
102+
103+
Get the PersistentVolumeClaims for web-0 and web-1:
104+
105+
kubectl get pvc -l app=nginx
106+
107+
The StatefulSet controller created two PersistentVolumeClaims that are bound to two PersistentVolumes.
108+
109+
The NGINX webserver, by default, serves an index file from /usr/share/nginx/html/index.html.
110+
The volumeMounts field in the StatefulSet's spec ensures that the /usr/share/nginx/html directory is backed by a PersistentVolume.
111+
112+
Let's Write the Pods' hostnames to their index.html files :
113+
114+
for i in 0 1; do kubectl exec "web-$i" -- sh -c 'echo "$(hostname)" > /usr/share/nginx/html/index.html'; done
115+
116+
117+
Lets verify that the NGINX webservers serve the hostnames:
118+
119+
for i in 0 1; do kubectl exec -i -t "web-$i" -- curl http://localhost/; done
120+
121+
**********************
122+
123+
Delete the pods of the statefulset
124+
125+
126+
In one terminal, watch the StatefulSet's Pods:
127+
128+
# kubectl get pod -w -l app=nginx
129+
130+
In a second terminal, delete all of the StatefulSet's Pods:
131+
132+
# kubectl delete pod -l app=nginx
133+
134+
Examine the output of the kubectl get command in the first terminal, and wait for all of the Pods to transition to Running and Ready.
135+
136+
137+
# kubectl get pod -w -l app=nginx
138+
139+
Verify the web servers continue to serve their hostnames:
140+
141+
for i in 0 1; do kubectl exec -i -t "web-$i" -- curl http://localhost/; done
142+
143+
Even though web-0 and web-1 were rescheduled, they continue to serve their hostnames because the PersistentVolumes associated
144+
with their PersistentVolumeClaims are remounted to their volumeMounts. No matter what node web-0and web-1 are scheduled on,
145+
their PersistentVolumes will be mounted to the appropriate mount points.
146+
147+
***************************
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+

0 commit comments

Comments
 (0)