Docker Kubernetes Service 代理服務建立node
建立Service須要提早建立好pod容器。再建立Service時須要指定Pod標籤,它會提供一個暴露端口默會分配容器內網訪問的惟一IP地址。nginx
環境:vim
1、經過deployment建立podapi
一、建立yaml文件網絡
vim nginx-deployment.yamlapp
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.12 ports: - containerPort: 80
# 指定api版本 apiVersion: apps/v1beta2 # 指定須要建立的資源對象 kind: Deployment # 源數據、能夠寫name,命名空間,對象標籤 metadata: # 指定對象名稱 name: nginx-deployment # 描述資源相關信息 spec: # 指定副本數 replicas: 3 # 資源標籤選擇器 selector: # 匹配標籤字段 matchLabels: # 標籤名 app: nginx # 描述資源具體信息 template: # 源數據、能夠寫name,命名空間,對象標籤 metadata: # 指定標籤 labels: # 標籤名 app: nginx # 描述資源相關信息 spec: # 容器管理 containers: # 容器名稱 - name: nginx # 鏡像名稱 image: nginx:1.12 # 端口管理 ports: # 指定暴露容器端口 - containerPort: 80
二、建立deployment負載均衡
kubectl create -f nginx-deployment.yaml
2、建立Service NodePort代理服務tcp
一、建立yaml文件ide
vim service.yaml測試
apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - name: http protocol: TCP port: 888 targetPort: 80 nodePort: 30001 type: NodePort clusterIP: "10.10.10.11"
# api版本 apiVersion: v1 # 建立對象類型 kind: Service # 保存源數據信息 metadata: # service名稱 name: nginx-service # 具體service內容 spec: # 指定標籤 selector: # 標籤要與指定的pod便籤相同 app: nginx # 開放指定端口,可寫多個 ports: # port的指定名稱 - name: http # 負載均衡默認協議爲TCP protocol: TCP # 暴露的service端口 port: 888 # 容器端口 targetPort: 80 # 固定容器內網惟一IP clusterIP: "10.10.10.11" # node節點建立socker的暴露端口,默認30000~32767 nodePort: 30001 # 服務類型 type: NodePort
二、建立service
kubectl create -f service.yaml
命令:kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service2 NodePort 10.10.10.11 <none> 888:30001/TCP 1m
命令:kubectl describe service nginx-service Name: nginx-service Namespace: default Labels: app=nginx Annotations: <none> Selector: app=nginx Type: NodePort IP: 10.10.10.11 Port: http 888/TCP TargetPort: 80/TCP NodePort: http 30001/TCP Endpoints: 172.17.1.2:80,172.17.1.3:80,172.17.1.4:80 + 9 more... Session Affinity: None External Traffic Policy: Cluster Events: <none>
命令:netstat -lnpt | grep 30001
tcp6 0 0 :::30001 :::* LISTEN 74604/kube-proxy
指定代理serviceIP它會默認指定一個IP地址段 配置文件:/opt/kubernetes/cfg/kube-apiserver # 集羣分配的IP範圍 KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.10.10.0/24" 注:不要分配到與當前網絡衝突的網段
注:主配置文件內可修改默認nodeport端口範圍 配置文件添加參數--service-node-port-range。
三、測試訪問