天天看點

輕松掌握k8s的kubectl使用指令行操作Service知識點02

作者:鵬的快樂

1、Service将同類型一組應用統一IP通路

将一組 Pods 網絡服務的抽象方法。統一Ip後,預設就實作了負載均衡。

1、隻在Pod内部任意機器通路的ClusterIp類型

在指令行操作生成一個ClusterIp位址。這種ClusterIp隻能在Pod内部通路。

輕松掌握k8s的kubectl使用指令行操作Service知識點02

生成了ClusterIp之後,這時也可以使用應用名加命名空間的形式通路,如下

輕松掌握k8s的kubectl使用指令行操作Service知識點02

2、可以在公網上通路的NodePort類型

輕松掌握k8s的kubectl使用指令行操作Service知識點02

在指令行操作生成一個ClusterIp位址,并多了一個随機外網通路的端口。

這個端口生成是随機的,範圍如下:

輕松掌握k8s的kubectl使用指令行操作Service知識點02

相當于在這個應用的所有副本都開了這個端口,是以使用任意台機器的IP位址加該端口都能通路到。包括node/master機器IP,隻要加上了外網通路的端口即可實作。

2、具體看指令行操作中說明。

指令行操作

1、将Deployment應用統一ClusterIp位址通路

--port 叢集裡通路的端口, --target-port 應用運作的的端口

kubectl expose deployment my-dep --port=8000 --target-port=80 --type=ClusterIP           

等同于沒有--type的

輕松掌握k8s的kubectl使用指令行操作Service知識點02

注意ClusterIp隻能在Pod内部通路,不能外網通路

同樣可以使用yaml檔案形式來設定,内容如下,使用 kubectl apply -f dd.yaml

apiVersion: v1
kind: Service
metadata:
labels:
app: my-dep
name: my-dep
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 80
selector:
app: my-dep
type: ClusterIP           

2、查詢Deployment應用叢集的Service資訊

簡寫形式

輕松掌握k8s的kubectl使用指令行操作Service知識點02
  • 隻設定了ClusterIp類型的應用的Service資訊查詢結果。

my-dep應用Pod内部通路位址就是10.96.8.0:8000

輕松掌握k8s的kubectl使用指令行操作Service知識點02
  • 設定了NodePort類型的應用的Service資訊查詢結果

如下展示了不僅有ClusterIp,多了30948端口。這個端口可以在外網進行通路了。

輕松掌握k8s的kubectl使用指令行操作Service知識點02

查所有kubectl get svc -A 如果根據名字過濾則 kubectl get svc -n ingress-nginx

3、根據Deployment應用标簽名稱查詢應用

使用标簽檢索Pod

kubectl get pod -l app=my-dep

4、删除Deployment應用的ClusterIp通路

輕松掌握k8s的kubectl使用指令行操作Service知識點02

5、查詢所有應用的标簽

輕松掌握k8s的kubectl使用指令行操作Service知識點02

6、将Deployment應用統一NodePort位址通路

輕松掌握k8s的kubectl使用指令行操作Service知識點02

同樣可以使用yaml檔案形式,内容如下,使用 kubectl apply -f dd.yaml

apiVersion: v1
kind: Service
metadata:
labels:
app: my-dep
name: my-dep
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 80
selector:
app: my-dep
type: NodePort           
輕松掌握k8s的kubectl使用指令行操作Service知識點02

這個端口生成是随機的,範圍如下:

輕松掌握k8s的kubectl使用指令行操作Service知識點02

相當于在這個應用的所有副本都開了30948端口,是以使用任意台機器的IP位址加該端口都能通路到。

繼續閱讀