代码语言:javascript复制
[root@vms61 chap9-svc]# kubectl create deployment web1 --image=nginx --dry-run=client -o yaml > web1.yaml
[root@vms61 chap9-svc]# vi web1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web1
name: web1
spec:
replicas: 3
selector:
matchLabels:
app: web1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
status: {}
[root@vms61 chap9-svc]# kubectl apply -f web1.yaml
deployment.apps/web1 created
[root@vms61 chap9-svc]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web1-5bfb6d8dcc-jqrfl 1/1 Running 0 4s
web1-5bfb6d8dcc-nggqm 1/1 Running 0 4s
web1-5bfb6d8dcc-qrhtj 1/1 Running 0 4s
[root@vms61 chap9-svc]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1-5bfb6d8dcc-jqrfl 1/1 Running 0 55s 10.244.196.36 vms62 <none> <none>
web1-5bfb6d8dcc-nggqm 1/1 Running 0 55s 10.244.196.37 vms62 <none> <none>
web1-5bfb6d8dcc-qrhtj 1/1 Running 0 55s 10.244.116.8 vms63 <none> <none>
每个pod的IP只能是集群内部可访问,集群里的pod——不管是哪个ns或在哪个节点,都是可以的
SVC定位的是app1:web1和app2:web2
注意这里如果指定了port为非80端口,curl的时候需要加上写明
代码语言:javascript复制[root@vms61 chap9-svc]# cat web1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
aa: bb
name: web1
spec:
replicas: 3
selector:
matchLabels:
app2: web2
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app1: web1
app2: web2
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
status: {}
[root@vms61 chap9-svc]# kubectl apply -f web1.yaml
deployment.apps/web1 created
[root@vms61 chap9-svc]# kubectl expose --name=svc1 deployment web1 --port=80 --target-port=80
service/svc1 exposed
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc1 ClusterIP 10.107.89.230 <none> 80/TCP 7s
[root@vms61 chap9-svc]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1-6464d54bd7-dt9qt 1/1 Running 0 70s 10.244.196.38 vms62 <none> <none>
web1-6464d54bd7-vtkwz 1/1 Running 0 70s 10.244.116.6 vms63 <none> <none>
web1-6464d54bd7-xfgth 1/1 Running 0 70s 10.244.196.40 vms62 <none> <none>
[root@vms61 chap9-svc]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
web1-6464d54bd7-dt9qt 1/1 Running 0 79s app1=web1,app2=web2,pod-template-hash=6464d54bd7
web1-6464d54bd7-vtkwz 1/1 Running 0 79s app1=web1,app2=web2,pod-template-hash=6464d54bd7
web1-6464d54bd7-xfgth 1/1 Running 0 79s app1=web1,app2=web2,pod-template-hash=6464d54bd7
代码语言:javascript复制[root@vms61 chap9-svc]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc1 ClusterIP 10.107.89.230 <none> 80/TCP 8m33s app2=web2
[root@vms61 chap9-svc]# kubectl describe svc svc1
Name: svc1
Namespace: chap9-svc
Labels: aa=bb
Annotations: <none>
Selector: app2=web2
Type: ClusterIP
IP: 10.107.89.230
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.116.6:80,10.244.196.38:80,10.244.196.40:80
Session Affinity: None
Events: <none>
[root@vms61 chap9-svc]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1-6464d54bd7-dt9qt 1/1 Running 0 8m46s 10.244.196.38 vms62 <none> <none>
web1-6464d54bd7-vtkwz 1/1 Running 0 8m46s 10.244.116.6 vms63 <none> <none>
web1-6464d54bd7-xfgth 1/1 Running 0 8m46s 10.244.196.40 vms62 <none> <none>
那如果想使用app1: web1来定位呢?当然可以
通过如下命令即可,请自行测试
kubectl expose --name=svc1 deployment web1 --port=80 --target-port=80 --selector=app1=web1
代码语言:javascript复制[root@vms61 chap9-svc]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1-6464d54bd7-dt9qt 1/1 Running 0 13m 10.244.196.38 vms62 <none> <none>
web1-6464d54bd7-vtkwz 1/1 Running 0 13m 10.244.116.6 vms63 <none> <none>
web1-6464d54bd7-xfgth 1/1 Running 0 13m 10.244.196.40 vms62 <none> <none>
[root@vms61 chap9-svc]# kubectl exec -it web1-6464d54bd7-dt9qt -- bash
root@web1-6464d54bd7-dt9qt:/# echo 11111 > /usr/share/nginx/html/index.html
root@web1-6464d54bd7-dt9qt:/# exit
exit
[root@vms61 chap9-svc]# kubectl exec -it web1-6464d54bd7-vtkwz -- bash
root@web1-6464d54bd7-vtkwz:/# echo 22222 > /usr/share/nginx/html/index.html
root@web1-6464d54bd7-vtkwz:/# exit
exit
[root@vms61 chap9-svc]# kubectl exec -it web1-6464d54bd7-xfgth -- bash
root@web1-6464d54bd7-xfgth:/# echo 33333 > /usr/share/nginx/html/index.html
root@web1-6464d54bd7-xfgth:/# exit
exit
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc1 ClusterIP 10.107.89.230 <none> 80/TCP 14m
[root@vms61 chap9-svc]# curl -s 10.107.89.230
33333
[root@vms61 chap9-svc]# curl -s 10.107.89.230
22222
[root@vms61 chap9-svc]# curl -s 10.107.89.230
11111
[root@vms61 chap9-svc]# curl -s 10.107.89.230
11111
[root@vms61 chap9-svc]# curl -s 10.107.89.230
33333
[root@vms61 chap9-svc]# curl -s 10.107.89.230
11111
[root@vms61 chap9-svc]# curl -s 10.107.89.230
33333
[root@vms61 chap9-svc]# curl -s 10.107.89.230
33333
[root@vms61 chap9-svc]# curl -s 10.107.89.230
33333
[root@vms61 chap9-svc]# curl -s 10.107.89.230
11111
[root@vms61 chap9-svc]# curl -s 10.107.89.230
22222
[root@vms61 chap9-svc]# curl -s 10.107.89.230
22222
如果我现在有一个服务svc1,找出此服务后端到底有多少个pod
根据selector这个下的标签来定位(不是labels)
代码语言:javascript复制[root@vms61 chap9-svc]# kubectl describe svc svc1
Name: svc1
Namespace: chap9-svc
Labels: aa=bb
Annotations: <none>
Selector: app2=web2
Type: ClusterIP
IP: 10.105.180.153
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.116.6:80,10.244.196.38:80,10.244.196.40:80
Session Affinity: None
Events: <none>
[root@vms61 chap9-svc]# kubectl get pods -l app2=web2
NAME READY STATUS RESTARTS AGE
web1-6464d54bd7-dt9qt 1/1 Running 0 27m
web1-6464d54bd7-vtkwz 1/1 Running 0 27m
web1-6464d54bd7-xfgth 1/1 Running 0 27m