k8s-statefulSet控制器-网络标识
1. k8s-statefulSet控制器-网络标识
- 稳定的网络标识:使用Headless Service(相比普通Service只是将spec.clusterIP定义为None)来维护Pod网络身份,会为每个Pod分配一个数字编号并且按照编号顺序部署。还需要在StatefulSet添加serviceName: “nginx” 字段指定StatefulSet控制器要使用这个Headless Service。
- 稳定主要体现在主机名和Pod A记录:
- 主机名:<statefulset名称>-<编号>
- Pod DNS A记录:<statefulset名称-编号>.
. .svc.cluster.local
1.1 headless service示例
apiVersion: v1
kind: Service
metadata:
name: web
namespace: default
spec:
clusterIP: None #无头服务
selector: #容器选择器
app: nginx # 指定关联pod名称
ports:
- protocol: TCP # 协议
port: 80 # service 端口
targetPort: 80 # 容器端口
type: ClusterIP # 服务类型
1.2 statefulset的pod示例
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "web" # 绑定的服务
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
1.3 执行命令
-
查询pod创建顺序
kubectl get pods
-
查询主机名
[root@k8s-master statefulset]# kubectl exec web-0 -- hostname web-0
-
测试A记录解析
[root@k8s-master statefulset]# kubectl run -it dns-test --image busybox:1.28.4 /bin/sh / # nslookup web.default.svc.cluster.local Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: web.default.svc.cluster.local Address 1: 10.244.107.214 web-2.web.default.svc.cluster.local Address 2: 10.244.169.145 web-1.web.default.svc.cluster.local Address 3: 10.244.36.88 web-0.web.default.svc.cluster.local
2.k8s-案例
-
创建配置目录
[root@k8s-master yaml]# mkdir -p statefulset [root@k8s-master yaml]# cd statefulset/
2.1 部署headless service
-
安装headless service服务
[root@k8s-master statefulset]# vim headless-service.yaml [root@k8s-master statefulset]# cat headless-service.yaml apiVersion: v1 kind: Service metadata: name: web namespace: default spec: clusterIP: None #无头服务 selector: #容器选择器 app: nginx # 指定关联pod名称 ports: - protocol: TCP # 协议 port: 80 # service 端口 targetPort: 80 # 容器端口 type: ClusterIP # 服务类型
-
启动服务
[root@k8s-master statefulset]# kubectl apply -f headless-service.yaml service/web created
-
查看服务
[root@k8s-master statefulset]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17d web ClusterIP None <none> 80/TCP 2m44s
2.2 部署statefulset的服务
-
编写statefulset_pod服务
[root@k8s-master statefulset]# vim statefulset_pod.yaml [root@k8s-master statefulset]# cat statefulset_pod.yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "web" # 绑定的服务 replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 name: web
-
启动服务
[root@k8s-master statefulset]# kubectl apply -f statefulset_pod.yaml statefulset.apps/web created
-
验证服务
[root@k8s-master statefulset]# kubectl get pods NAME READY STATUS RESTARTS AGE nfs-client-provisioner-95c9579-m2f69 1/1 Running 0 2d4h secret-demo-pod 1/1 Running 0 5d23h web-0 1/1 Running 0 42s web-1 1/1 Running 0 37s web-2 1/1 Running 0 31s
3. 验证服务
-
查询pod创建顺序
[root@k8s-master pv]# kubectl get pods NAME READY STATUS RESTARTS AGE nfs-client-provisioner-95c9579-qslkg 1/1 Running 0 9m49s web-0 1/1 Running 0 18h web-1 1/1 Running 0 18h web-2 1/1 Running 0 18h
-
查询主机名
[root@k8s-master pv]# kubectl exec -it web-0 -- hostname web-0 [root@k8s-master pv]# kubectl exec -it headless-web-0 -- hostname headless-web-0
-
测试A记录解析
[root@k8s-master statefulset]# kubectl run -it test --image=busybox:1.28.4 -- /bin/sh If you don't see a command prompt, try pressing enter. / # nslookup web Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: web Address 1: 10.244.85.204 web-2.web.default.svc.cluster.local Address 2: 10.244.85.203 web-0.web.default.svc.cluster.local Address 3: 10.244.58.196 web-1.web.default.svc.cluster.local / # nslookup web-0.web.default.svc.cluster.local Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: web-0.web.default.svc.cluster.local Address 1: 10.244.85.203 web-0.web.default.svc.cluster.local