apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deployment
spec:
selector:
matchLabels:
app: httpd # 通过标签选择被控制的pod
replicas: 3
template:
metadata:
labels:
app: httpd # 给pod打上标签,Service、Deployment 将会用这个 label 来挑选 Pod
spec:
containers:
- name: httpd
image: httpd
ports:
- containerPort: 80 # 转发到后端pod的端口号
apiVersion: v1
kind: Service
metadata:
name: httpd-service # 必填,service名称
spec:
selector:
app: httpd # 必填,在selector字段中指定了为哪一个标签的app进行负载均衡
ports: # 将 Service 的 8080 端口映射到 Pod 的 80 端口,使用 TCP 协议
- protocol: TCP
port: 8080 # service监听端口
targetPort: 80 # 转发到后端pod的端口号
httpd-service分配到一个CLUSTER-IP,可以通过该IP访问后端的httpd Pod。
$ sudo iptables-save | grep '10.98.165.152'
-A KUBE-SERVICES ! -s 10.244.0.0/16 -d 10.98.165.152/32 -p tcp -m comment --comment "default/httpd-service cluster IP" -m tcp --dport 8080 -j KUBE-MARK-MASQ
-A KUBE-SERVICES -d 10.98.165.152/32 -p tcp -m comment --comment "default/httpd-service cluster IP" -m tcp --dport 8080 -j KUBE-SVC-EHNHZY2AA2RZYURI
这两条规则的含义:
-A KUBE-SVC-EHNHZY2AA2RZYURI -m comment --comment "default/httpd-service" -m statistic --mode random --probability 0.33333333349 -j KUBE-SEP-RWFU5RJRM7QIVQGD
-A KUBE-SVC-EHNHZY2AA2RZYURI -m comment --comment "default/httpd-service" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-CX4QANADYSW4JX2E
-A KUBE-SVC-EHNHZY2AA2RZYURI -m comment --comment "default/httpd-service" -j KUBE-SEP-HGKXYQ3Y7I7MLDXP
-A KUBE-SEP-RWFU5RJRM7QIVQGD -s 10.244.1.13/32 -m comment --comment "default/httpd-service" -j KUBE-MARK-MASQ
-A KUBE-SEP-RWFU5RJRM7QIVQGD -p tcp -m comment --comment "default/httpd-service" -m tcp -j DNAT --to-destination 10.244.1.13:80
-A KUBE-SEP-CX4QANADYSW4JX2E -s 10.244.1.14/32 -m comment --comment "default/httpd-service" -j KUBE-MARK-MASQ
-A KUBE-SEP-CX4QANADYSW4JX2E -p tcp -m comment --comment "default/httpd-service" -m tcp -j DNAT --to-destination 10.244.1.14:80
-A KUBE-SEP-HGKXYQ3Y7I7MLDXP -s 10.244.2.5/32 -m comment --comment "default/httpd-service" -j KUBE-MARK-MASQ
-A KUBE-SEP-HGKXYQ3Y7I7MLDXP -p tcp -m comment --comment "default/httpd-service" -m tcp -j DNAT --to-destination 10.244.2.5:80
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd2-deployment
namespace: kube-public # 指定命名空间
spec:
selector: # 通过标签选择被控制的pod
matchLabels:
app: httpd2
replicas: 2
template:
metadata:
labels:
app: httpd2 # 给pod打上标签,Service、Deployment 将会用这个 label 来挑选 Pod
spec:
containers:
- name: httpd2
image: httpd
ports:
- containerPort: 80 # 转发到后端pod的端口号
--- # 多个资源可以在一个 YAML 文件中定义,用 --- 分割
apiVersion: v1
kind: Service
metadata:
name: httpd2-service
namespace: kube-public # 指定命名空间
spec:
selector:
app: httpd2
ports: # 将 Service 的 8080 端口映射到 Pod 的 80 端口,使用 TCP 协议
- protocol: TCP
port: 8080 # service监听端口
targetPort: 80 # 转发到后端pod的端口号
ClusterIP:Service通过Cluster内部的IP对外提供服务,只有Cluster内部的节点跟Pod可以访问。NodePort:Service通过Cluster节点的静态端口对外提供服务。Cluster外部可以通过: 访问Service。LoadBalancer:Service利用cloud provider特有的load balancer对外提供服务,cloud provider 负责将 load balancer 的流量导向 Service。目前支持的 cloud provider 有 GCP、AWS、Azur 等。
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd3-deployment
namespace: kube-public # 指定命名空间
spec:
selector: # 通过标签选择被控制的pod
matchLabels:
app: httpd3
replicas: 1
template:
metadata:
labels:
app: httpd3 # 给pod打上标签,Service、Deployment 将会用这个 label 来挑选 Pod
spec:
containers:
- name: httpd3
image: httpd
ports:
- containerPort: 80 # 转发到后端pod的端口号
--- # 多个资源可以在一个 YAML 文件中定义,用 --- 分割
apiVersion: v1
kind: Service
metadata:
name: httpd3-service
namespace: kube-public # 指定命名空间
spec:
type: NodePort # 添加NodePort类型的Service
selector:
app: httpd3
ports: # 将 Service 的 8080 端口映射到 Pod 的 80 端口,使用 TCP 协议
- protocol: TCP
port: 8080 # service监听端口
targetPort: 80 # 转发到后端pod的端口号
$ sudo iptables-save | grep 'httpd3-service'
-A KUBE-NODEPORTS -p tcp -m comment --comment "kube-public/httpd3-service" -m tcp --dport 30150 -j KUBE-MARK-MASQ
-A KUBE-NODEPORTS -p tcp -m comment --comment "kube-public/httpd3-service" -m tcp --dport 30150 -j KUBE-SVC-X5MD3Z6GVRFIQV4Q
规则的含义:访问当前节点30150端口的请求会被应用规则KUBE-SVC-X5MD3Z6GVRFIQV4Q
-A KUBE-SVC-X5MD3Z6GVRFIQV4Q -m comment --comment "kube-public/httpd3-service" -j KUBE-SEP-CA4JWI7MMTWMJLYK
-A KUBE-SEP-CA4JWI7MMTWMJLYK -s 10.244.1.27/32 -m comment --comment "kube-public/httpd3-service" -j KUBE-MARK-MASQ
-A KUBE-SEP-CA4JWI7MMTWMJLYK -p tcp -m comment --comment "kube-public/httpd3-service" -m tcp -j DNAT --to-destination 10.244.1.27:80
NodePort默认的是从 30000-32767 中随机分配一个可用的端口,但我们可以用nodePort指定某个特定的端口:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd3-deployment
namespace: kube-public # 指定命名空间
spec:
selector: # 通过标签选择被控制的pod
matchLabels:
app: httpd3
replicas: 1
template:
metadata:
labels:
app: httpd3 # 给pod打上标签,Service、Deployment 将会用这个 label 来挑选 Pod
spec:
containers:
- name: httpd3
image: httpd
ports:
- containerPort: 80 # 转发到后端pod的端口号
--- # 多个资源可以在一个 YAML 文件中定义,用 --- 分割
apiVersion: v1
kind: Service
metadata:
name: httpd3-service
namespace: kube-public # 指定命名空间
spec:
type: NodePort # 添加NodePort类型的Service
selector:
app: httpd3
ports: # 将 Service 的 8080 端口映射到 Pod 的 80 端口,使用 TCP 协议
- protocol: TCP
port: 8080 # Service(Cluster IP)上监听端口
targetPort: 80 # 转发到后端pod的监听端口号、
nodePort: 30150 # 节点上监听的端口
最终,Node 和 ClusterIP 在各自端口上接收到的请求都会通过 iptables 转发到 Pod 的 targetPort。