一、創建單個nginx-pod文件
apiVersion: v1 #指定api版本,此值必須在kubectl apiversion中
kind: Pod #指定創建資源的角色/類型(pod\deployment\svc)
metadata: #資源的元數據/屬性
name: nginx #資源的名字,在同一個namespace中必須唯一,kubectl顯示名
labels: #設定資源的標籤
app: web #爲app標籤打上web字段
namespace: test #存放的空間
spec: #指定該資源的內容
containers: #該POD運行容器的相關信息
- name: nginx-test #容器的名稱,docker ps 看到的名稱
image: test-harbor.cedarhd.com/public/nginx:curl #容器使用的鏡像
ports: #指定容器的端口
- containerPort: 80 #容器使用的端口
[root@test-nodes1 ~]# kubectl create -f nginx-pod.yaml
pod/nginx created
[root@test-nodes1 ~]# kubectl get pod -o wide -n test
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 13s 172.7.21.7 test-nodes1.cedarhd.com <none> <none>
二、創建deployment控制器nginx-dp的yaml文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-dp
namespace: test
spec:
replicas: 2 #複本數
template: #deployment模板內容
metadata:
labels:
app: nginx-proxy
spec:
containers:
- name: nginx
image: test-harbor.cedarhd.com/public/nginx:curl
ports:
- containerPort: 80
[root@test-nodes1 ~]# kubectl create -f nginx-dp.yaml
deployment.extensions/nginx-dp created
[root@test-nodes1 ~]# kubectl get pod -o wide -n test
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-dp-856666d759-cwqfc 1/1 Running 0 9m57s 172.7.21.7 test-nodes1.cedarhd.com <none> <none>
nginx-dp-856666d759-lvr9c 1/1 Running 0 9m57s 172.7.22.7 test-nodes2.cedarhd.com <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-dp 2/2 2 2 8s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dp-856666d759 2 2 2 8s
三、創建svc服務並關聯上面的deployment(通過app:nginx-proxy)
apiVersion: v1
kind: Service
metadata:
name: nginx-dp #svc的顯示名
namespace: test
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx-proxy #關聯當前集羣上app爲nginx-proxy字段
[root@test-nodes1 ~]# kubectl get svc -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.249.33 <none> 80/TCP 6s
TCP 192.168.249.33:80 nq #相應的映射
-> 172.7.21.7:80 Masq 1 0 0
-> 172.7.22.7:80 Masq 1 0 0