NodePort
清单文件
---
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
labels:
app: myapp
release: stable
spec:
selector:
app: myapp
release: stable
# Service类型为NodePort
type: NodePort
ports:
- name: myapp
# Node宿主机监听端口为30080
nodePort: 30080
# Service服务监听端口为80
port: 80
protocol: TCP
# Pods服务端口为app-port也就是80
targetPort: app-port
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
revisionHistoryLimit: 3
selector:
matchLabels:
app: myapp
release: stable
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
name: myapp-container
namespace: default
labels:
app: myapp
release: stable
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
ports:
- name: app-port
containerPort: 80
protocol: TCP
测试效果 获取Service信息
# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d <none>
myapp-svc NodePort 10.105.231.167 <none> 80:30080/TCP 7m app=myapp,release=stable
获取详细详细信息
# kubectl describe myapp-svc
error: the server doesn't have a resource type "myapp-svc"
[root@node001 mainifest]# kubectl describe svc myapp-svc
Name: myapp-svc
Namespace: default
Labels: app=myapp
release=stable
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"myapp","release":"stable"},"name":"myapp-svc","namespace":"default"},...
Selector: app=myapp,release=stable
Type: NodePort
IP: 10.105.231.167
Port: myapp 80/TCP
TargetPort: app-port/TCP
NodePort: myapp 30080/TCP
Endpoints: 10.244.1.41:80,10.244.2.128:80,10.244.2.129:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
测试访问
# curl node002:30080/hostname.html
myapp-deploy-6f96c5bb-c8sh7
[root@node001 mainifest]# curl node002:30080/hostname.html
myapp-deploy-6f96c5bb-22z7q
[root@node001 mainifest]# curl node002:30080/hostname.html
myapp-deploy-6f96c5bb-zrdv4
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myapp-deploy-6f96c5bb-22z7q 1/1 Running 0 9m 10.244.2.129 node003
myapp-deploy-6f96c5bb-c8sh7 1/1 Running 0 9m 10.244.2.128 node003
myapp-deploy-6f96c5bb-zrdv4 1/1 Running 0 9m 10.244.1.41 node002
通过dns访问测试
# kubectl run -it client --image=busybox -- /bin/sh
/ # wget -q -O - http://myapp-svc.default.svc.cluster.local/hostname.html
myapp-deploy-6f96c5bb-c8sh7
/ # wget -q -O - http://myapp-svc.default.svc.cluster.local/hostname.html
myapp-deploy-6f96c5bb-22z7q
/ # wget -q -O - http://myapp-svc.default.svc.cluster.local/hostname.html
myapp-deploy-6f96c5bb-zrdv4