1. 程式人生 > >k8s創建服務與公開接口

k8s創建服務與公開接口

查看 bsp tle margin ces pac describe names meta

8. 通過服務訪問pod

1.創建服務

服務創建會用應用的標簽確定哪個pod

部署服務

cat nginx.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: nginx

spec:

replicas: 4

template:

metadata:

labels:

app: nginx

spec:

containers:

- name: nginx

image: nginx

ports:

- containerPort: 80 #此處暫不起作用

創建服務

cat servi.yml

apiVersion: v1

kind: Service

metadata:

name: nginx

spec:

# type: NodePort

selector:

app: nginx

ports:

- protocol: TCP

# nodePort: 30000

port: 8080

targetPort: 80

kubectl apply -f servi.yml

kubectl get service

技術分享圖片

kubectl describe service nginx #查看服務詳細信息

所有實現均通過防火墻實現

2.訪問服務

查看命名空間

kubectl get namespace

技術分享圖片


kubectl get deployment --namespace=kube-system

#查看kube-dns (服務器)

訪問service

可以通過服務名.空間名:端口形式訪問

查看服務空間名


技術分享圖片


3.創建服務分配不同命名空間

多個服務寫入一個文件---分隔

cat ng.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: nginx

namespace: kube-public

spec:

replicas: 3

template:

metadata:

labels:

app: nginx

spec:

containers:

- name: nginx

image: nginx

ports:

- containerPort: 80

---

apiVersion: v1

kind: Service

metadata:

name: nginx

namespace: kube-public

spec:

selector:

app: nginx

ports:

- protocol: TCP

port: 8080

targetPort: 80

跨空間訪問

kubectl run busybox --rm -ti --image=busybox /bin/sh

技術分享圖片

8. 公網訪問pod

cat ng.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: nginx

namespace: kube-public

spec:

replicas: 3

template:

metadata:

labels:

app: nginx

spec:

containers:

- name: nginx

image: nginx

ports:

- containerPort: 80

---

apiVersion: v1

kind: Service

metadata:

name: nginx

namespace: kube-public

spec:

selector:

app: nginx

ports:

- protocol: TCP

port: 8080

targetPort: 80

---

apiVersion: v1

kind: Service

metadata:

name: nginx

namespace: kube-public

spec:

type: NodePort

selector:

app: nginx

ports:

- protocol: TCP

nodePort: 30000 #固定端口30000-32767,不寫會自動分配

port: 8080

targetPort: 80

kubectl get service --namespace=kube-public 查看非默認空間

kubectl get pod --namespace=kube-public










k8s創建服務與公開接口