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:8000
targetPort:31152
protocol: 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-v40nj1/1Running11h
nginx-controller-zxdzh1/1Running11h
[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,11Aug201619:02:20-0700Labels: name=nginx
Status: Running
IP:10.0.83.3Controllers: ReplicationController/nginx-controller
Containers:
nginx:
Container ID: docker://269adc9b693aba0356ba18e4253c2b498fc7b7a8ce0af83857fcfd6b70e6ef03Image: nginx
Image ID: docker://sha256:0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470dPort:80/TCP
State: Running
Started: Thu,11Aug201620:49:27-0700Last State: Terminated
Reason: Completed
Exit Code:0Started: Thu,11Aug201619:03:44-0700Finished: Thu,11Aug201620: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 dockerid269adc9b693a
5m 5m1{kubelet k8s-slave1-206} spec.containers{nginx} Normal Started Started container with dockerid269adc9b693a
6、查看service
[root@k8s-master ~]# kubectl get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes10.254.0.1443/TCP 16h
nginx-service-nodeport10.254.29.728000/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
$ curl10.10.20.203:31152Welcome to nginx!body {
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 youforusing nginx.
$ curl10.10.20.206:31152Welcome to nginx!body {
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 youforusing nginx.