二、创建两个自主式Pod资源
要求在 Kubernetes 环境中,通过yaml文件的方式,创建2个Nginx Pod分别放置在两个不同的节点上,Pod使用hostPath类型的存储卷挂载,节点本地目录共享使用 /data,2个Pod副本测试页面二者要不同,以做区分,测试页面可自己定义。
#1、先空跑命令,生成nginx的pod模板文件 kubectl run mynginx --image=nginx:1.14 --port=80 --dry-run=client -o yaml > nginx-pod.yaml #2、修改模板文件 --- apiVersion: v1 kind: Pod metadata: labels: run: nginx name: nginx01 spec: nodeName: node01 containers: - image: nginx name: nginx ports: - containerPort: 80 volumeMounts: - name: node1-html mountPath: /usr/share/nginx/html/ volumes: - name: node1-html hostPath: path: /data/ type: DirectoryOrCreate --- apiVersion: v1 kind: Pod metadata: labels: run: nginx name: nginx02 spec: nodeName: node02 containers: - image: nginx name: nginx ports: - containerPort: 80 volumeMounts: - name: node2-html mountPath: /usr/share/nginx/html/ volumes: - name: node2-html hostPath: path: /data/ type: DirectoryOrCreate #使用yaml文件创建自主式Pod资源 kubectl apply -f nginx-pod.yaml pod/nginx01 created pod/nginx02 created #查看创建的两个pod,被调度到了不同的node节点 kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx01 1/1 Running 0 2m11s 10.244.1.2 node01 <none> <none> nginx02 1/1 Running 0 2m11s 10.244.2.2 node02 <none> <none> #---3、两个node节点的存储卷,写入不同的html文件内容,验证访问网页 #node01节点 echo "this is node01" > /data/index.html #node02节点 echo "this is node02 " > /data/index.html curl 10.244.1.2 #访问Node01节点的Pod的IP curl 10.244.2.2 #访问Node02节点的Pod的IP
三、创建service资源
编写service对应的yaml文件,使用NodePort类型和TCP 30000端口将Nginx服务发布出去。
[root@master opt]# vim pod-nginx-service.yaml apiVersion: v1 kind: Service metadata: labels: run: nginx name: nginx-service spec: ports: - port: 80 protocol: TCP targetPort: 80 nodePort: 30000 selector: run: nginx type: NodePort [root@master opt]# kubectl apply -f pod-nginx-service.yaml service/nginx-service created [root@master opt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47m nginx-service NodePort 10.110.35.189 <none> 80:30000/TCP 7s [root@master opt]# kubectl describe svc kubernetes nginx-service [root@master opt]# kubectl describe svc nginx-service Name: nginx-service Namespace: default Labels: run=nginx Annotations: <none> Selector: run=nginx Type: NodePort IP Families: <none> IP: 10.110.35.189 IPs: 10.110.35.189 Port: <unset> 80/TCP TargetPort: 80/TCP NodePort: <unset> 30000/TCP Endpoints: 10.244.1.2:80,10.244.2.2:80 Session Affinity: None External Traffic Policy: Cluster Events: <none>
[root@master opt]# curl 192.168.10.20:30000 this is node02 [root@master opt]# curl 192.168.10.10:30000 this is node02 [root@master opt]# curl 192.168.10.30:30000 this is node02 [root@master opt]# curl 192.168.10.10:30000 this is node01