跳转至

4. service资源

1. 为什么k8s要引入service资源

#运行在docker中的业务,想要被外界访问,我们需要为它做端口映射才能被访问。
那么运行在k8s中的容器,为什么不能直接为它做端口映射呢?

2. k8s中的3种IP地址类型

nodeip      #节点IP,如k8s-node1
clusterip   #k8s的vip,只能内部访问,在配置文件中指定,默认 10.254.0.0/16
podip       #启动的pod的IP

3. 查看clusterIP

kubectl get all 
[root@k8s-master rc]# kubectl get all
NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
svc/kubernetes   10.254.0.1   <none>        443/TCP   6h        #ip

NAME             READY     STATUS    RESTARTS   AGE
po/myweb-f14fx   1/1       Running   0          3m
po/myweb-rz03f   1/1       Running   0          3m

4. 创建svc目录

cd /k8s
mkdir svc
cd svc

5. 创建svc的yaml文件

vim nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
      targetPort: 80
  selector:
    app: myweb

6. 创建svc

kubectl create -f nginx-svc.yaml
[root@k8s-master svc]# kubectl get svc
NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.254.0.1   <none>        443/TCP   6h
[root@k8s-master svc]#
[root@k8s-master svc]#
[root@k8s-master svc]# kubectl create -f nginx-svc.yaml
service "myweb" created
[root@k8s-master svc]#
[root@k8s-master svc]# kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1      <none>        443/TCP        6h
myweb        10.254.243.14   <nodes>       80:30000/TCP   3s

7. 查看svc

kubectl describe svc myweb
[root@k8s-master svc]# kubectl describe svc myweb
Name:                   myweb
Namespace:              default
Labels:                 <none>
Selector:               app=myweb
Type:                   NodePort
IP:                     10.254.243.14
Port:                   <unset> 80/TCP
NodePort:               <unset> 30000/TCP
Endpoints:              172.16.70.2:80,172.16.77.2:80
Session Affinity:       None
No events.

8. 删除所有pod

kubectl delete pod --all
[root@k8s-master svc]# kubectl get pods -owide
NAME          READY     STATUS    RESTARTS   AGE       IP            NODE
myweb-f14fx   1/1       Running   0          7m        172.16.70.2   k8s-node2
myweb-rz03f   1/1       Running   0          7m        172.16.77.2   k8s-node1
[root@k8s-master svc]#
[root@k8s-master svc]#
[root@k8s-master svc]# kubectl delete pod --all
pod "myweb-f14fx" deleted
pod "myweb-rz03f" deleted
[root@k8s-master svc]#
[root@k8s-master svc]# kubectl get pods -owide
NAME          READY     STATUS    RESTARTS   AGE       IP            NODE
myweb-0p0bc   1/1       Running   0          3s        172.16.77.3   k8s-node1
myweb-r6mvn   1/1       Running   0          3s        172.16.34.3   k8s-master

9. 查看pod状态

kubectl get all
[root@k8s-master svc]# kubectl get all
NAME       DESIRED   CURRENT   READY     AGE
rc/myweb   2         2         2         7m

NAME             CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc/kubernetes   10.254.0.1      <none>        443/TCP        6h
svc/myweb        10.254.243.14   <nodes>       80:30000/TCP   2m

NAME             READY     STATUS    RESTARTS   AGE
po/myweb-0p0bc   1/1       Running   0          21s
po/myweb-r6mvn   1/1       Running   0          21s

10. 浏览器访问30000端口

#192.168.178.151:30000

最后更新: 2022-02-22 04:55:01