Docker Kubernetes 命令行創建Pod
環境:
- 系統:Centos 7.4 x64
- Docker版本:18.09.0
- Kubernetes版本:v1.8
- 管理節點:192.168.1.79
- 工作節點:192.168.1.78
- 工作節點:192.168.1.77
管理節點:創建並運行Nginx鏡像
kubectl run nginx --image=nginx --replicas=3

kubectl run:運行容器 nginx:服務名 --image:鏡像名稱 --replicas:副本數

命令:kubectl get pods NAME READY STATUS RESTARTS AGE nginx-7c87f569d-7wtw6 1/1 Running 1 18h nginx-7c87f569d-cvhw5 1/1 Running 1 18h nginx-7c87f569d-jq49n 1/1 Running 1 18h
1、創建內網訪問的service
管理節點:創建service通過deployment資源管理,並暴露一個內網訪問地址
kubectl expose deployment nginx --port=88 --target-port=80

kubectl expose deployment 發布服務名 --port=暴露端口 --target-port=容器端口
注:同過kubernetes負載均衡暴露出一個唯一的IP地址。

命令:kubectl get service nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx ClusterIP 10.10.10.187 <none> 88/TCP 1m
工作節點:測試內網訪問
curl -I 10.10.10.187:88
HTTP/1.1 200 OK Server: nginx/1.15.6 Date: Thu, 22 Nov 2018 07:00:44 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 06 Nov 2018 13:32:09 GMT Connection: keep-alive ETag: "5be197d9-264" Accept-Ranges: bytes
2、創建內網訪問基礎上,創建外網訪問暴露端口
管理節點:創建Service通過deployment 並暴露88端口(在88端口負載TCP流量)
kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80

kubectl expose deployment 資源名 --port=暴露內網端口 --type=協議類型 --target-port=容器端口
管理節點:查看外網暴露端口
kubectl get service nginx
# 32428為外網暴露隨機端口 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx NodePort 10.10.10.134 <none> 88:32428/TCP 4m
外網測試訪問
curl -I http://192.168.1.78:32428
HTTP/1.1 200 OK Server: nginx/1.15.6 Date: Thu, 22 Nov 2018 07:16:30 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 06 Nov 2018 13:32:09 GMT Connection: keep-alive ETag: "5be197d9-264" Accept-Ranges: bytes