K8S基础 - 06Service
一、介绍
- node network, pod network, cluster network
- service: virtual IP, CoreDNS, kube-dns
- api-service <-- watch --> kube-proxy
- 工作模式: userspace, iptables, ipvs
- userspace: 1.1-
- iptables: 1.10-
- ipvs: 1.11+
- 类型:ExternalName, ClusterIP, NodePort, LoadBalance
[root@k8s-master ~]# kubectl explain pod.spec
hostIPC <boolean>
Use the host's ipc namespace. Optional: Default to false.
hostNetwork <boolean>
Host networking requested for this pod. Use the host's network namespace.
If this option is set, the ports that will be used must be specified.
Default to false.
hostPID <boolean>
Use the host's pid namespace. Optional: Default to false.
二、 Service
2.1 创建ClusterIP Service
[root@k8s-master ~]# cat svc-redis.yml
apiVersion: v1
kind: Service
metadata:
name: redis-demo
namespace: default
spec:
selector:
app: redis
role: logstor
clusterIP: 10.98.98.98
type: ClusterIP
ports:
- port: 6379
targetPort: 6379
[root@k8s-master ~]# kubectl apply -f svc-redis.yml service/redis-demo created [root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis ClusterIP 10.99.58.88 <none> 6379/TCP 3d22h redis-demo ClusterIP 10.98.98.98 <none> 6379/TCP 24s [root@k8s-master ~]# kubectl describe svc redis-demo Name: redis-demo Namespace: default Labels: <none> Annotations: <none> Selector: app=redis,role=logstor Type: ClusterIP IP: 10.98.98.98 Port: <unset> 6379/TCP TargetPort: 6379/TCP Endpoints: 10.244.2.80:6379 Session Affinity: None Events: <none> [root@k8s-master ~]# telnet 10.98.98.98 6379 Trying 10.98.98.98... Connected to 10.98.98.98. Escape character is '^]'. ^] telnet> quit Connection closed.
2.2 创建NodePort Service
[root@k8s-master pod-k8s]# cat svc-mynginx.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-mynginx
spec:
type: NodePort
selector:
app: mynginx
ports:
- name: http
nodePort: 30089
port: 80
targetPort: 80
[root@k8s-master pod-k8s]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17d
svc-mynginx NodePort 10.109.228.159 <none> 80:30089/TCP 31h
[root@k8s-master pod-k8s]# while true; do curl http://192.168.6.33:30089/hostname.html; sleep 1; done
mynginx-app-bddc44777-pl9vj
mynginx-app-bddc44777-fx6gj
mynginx-app-bddc44777-p9lkh
mynginx-app-bddc44777-fx6gj
mynginx-app-bddc44777-pl9vj
mynginx-app-bddc44777-fx6gj
mynginx-app-bddc44777-p9lkh
mynginx-app-bddc44777-p9lkh
mynginx-app-bddc44777-pl9vj
2.3 设置会话亲和性
默认 sessionAffinity: None
[root@k8s-master ~]# kubectl patch svc svc-mynginx -p '{"spec":{"sessionAffinity":"ClientIP"}}'
service/svc-mynginx patched
[root@k8s-master ~]# while true; do curl http://192.168.6.33:30089/hostname.html; sleep 1; done
mynginx-app-bddc44777-pl9vj
mynginx-app-bddc44777-pl9vj
mynginx-app-bddc44777-pl9vj
mynginx-app-bddc44777-pl9vj
资源记录:SVC_NAME.NS_NAME.DOMAIN.LTD.
svc.cluster.local.
redis.default.svc.cluster.local.
三、无头service
3.1 创建Service
[root@k8s-master pod-k8s]# cat svc-mynginx-headless.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-mynginx-headless
namespace: default
spec:
selector:
app: mynginx
clusterIP: "None" # 不能使用""
ports:
- port: 80
targetPort: 80
[root@k8s-master ~]# kubectl apply -f svc-mynginx-headless.yaml
service/svc-mynginx-headless created
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17d
svc-mynginx NodePort 10.109.228.159 <none> 80:30089/TCP 31h
svc-mynginx-headless ClusterIP None <none> 80/TCP 3s
3.2 解析service
# 正常解析地址为Service的ClusterIP
[root@k8s-master ~]# dig -t -A svc-mynginx.default.svc.cluster.local @10.96.0.10
;; ANSWER SECTION:
svc-mynginx.default.svc.cluster.local. 30 IN A 10.109.228.159
[root@k8s-master ~]# dig -t -A svc-mynginx-headless.default.svc.cluster.local @10.96.0.10
;; ANSWER SECTION:
svc-mynginx-headless.default.svc.cluster.local. 30 IN A 10.244.2.26
svc-mynginx-headless.default.svc.cluster.local. 30 IN A 10.244.3.34
svc-mynginx-headless.default.svc.cluster.local. 30 IN A 10.244.1.27
[root@k8s-master ~]# kubectl get pods -l app=mynginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mynginx-app-bddc44777-fx6gj 1/1 Running 1 31h 10.244.1.27 k8s-node31.bearpx.com <none> <none>
mynginx-app-bddc44777-p9lkh 1/1 Running 1 31h 10.244.3.34 k8s-node32.bearpx.com <none> <none>
mynginx-app-bddc44777-pl9vj 1/1 Running 1 31h 10.244.2.26 k8s-node33.bearpx.com <none> <none>