环境:

两台虚拟机,

10.10.20.203 部署docker、etcd、flannel、kube-apiserver、kube-controller-manager、kube-scheduler

10.10.20.206 部署docker、flannel、kubelet、kube-proxy

1、创建nginx-rc.yaml

apiVersion: v1

kind: ReplicationController

metadata:

name: nginx-controller

spec:

replicas:2selector:

name: nginx

template:

metadata:

labels:

name: nginx

spec:

containers:-name: nginx

image: nginx

ports:- containerPort: 80

2、创建nginx-service-nodeport.yaml

apiVersion: v1

kind: Service

metadata:

name: nginx-service-nodeport

spec:

ports:- port: 8000targetPort:80protocol: TCP

type: NodePort

selector:

name: nginx

3、创建pod

kubectl create -f nginx-rc.yaml

4、创建service

kubectl create -f nginx-service-nodeport.yaml

5、查看pod

[root@k8s-master ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx-controller-v40nj 1/1 Running 11h

nginx-controller-zxdzh 1/1 Running 1 1h

[root@k8s-master ~]# kubectl describe pod nginx-controller-v40nj

Name: nginx-controller-v40nj

Namespace: default

Node: k8s-slave1-206/60.19.29.21Start Time: Thu,11 Aug 2016 19:02:20 -0700Labels: name=nginx

Status: Running

IP:10.0.83.3Controllers: ReplicationController/nginx-controller

Containers:

nginx:

Container ID: docker://269adc9b693aba0356ba18e4253c2b498fc7b7a8ce0af83857fcfd6b70e6ef03

Image: nginx

Image ID: docker://sha256:0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d

Port: 80/TCP

State: Running

Started: Thu,11 Aug 2016 20:49:27 -0700Last State: Terminated

Reason: Completed

Exit Code:0Started: Thu,11 Aug 2016 19:03:44 -0700Finished: Thu,11 Aug 2016 20:12:12 -0700Ready: True

Restart Count:1Environment Variables:Conditions:

Type Status

Initialized True

Ready True

PodScheduled True

No volumes.

QoS Tier: BestEffort

Events:

FirstSeen LastSeen Count From SubobjectPath Type Reason Message--------- -------- ----- ---- ------------- -------- ------ -------5m 5m1 {kubelet k8s-slave1-206} spec.containers{nginx} Normal Pulling pulling image "nginx"5m 5m2 {kubelet k8s-slave1-206} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst"policy. Falling back to DNSDefault policy.

5m 5m1 {kubelet k8s-slave1-206} spec.containers{nginx} Normal Pulled Successfully pulled image "nginx"5m 5m1 {kubelet k8s-slave1-206} spec.containers{nginx} Normal Created Created container with docker id269adc9b693a

5m 5m1 {kubelet k8s-slave1-206} spec.containers{nginx} Normal Started Started container with docker id 269adc9b693a

6、查看service

[root@k8s-master ~]# kubectl get service

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes10.254.0.1 443/TCP 16h

nginx-service-nodeport 10.254.29.72 8000/TCP 49m

[root@k8s-master ~]# kubectl describe service nginx-service-nodeport

Name: nginx-service-nodeport

Namespace: default

Labels:Selector: name=nginx

Type: NodePort

IP:10.254.29.72Port: 8000/TCP

NodePort: 31152/TCP

Endpoints:10.0.83.2:80,10.0.83.3:80Session Affinity: None

No events.

7、测试service是否好用

因为service使用的是NodePort方式,所以在任何一个节点访问31152这个端口都可以访问nginx

$ curl 10.10.20.203:31152

Welcome to nginx!

width: 35em;

margin:0auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

$ curl 10.10.20.206:31152

Welcome to nginx!

width: 35em;

margin:0auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐