CentOS 7.2基於Kubernetes部署簡單應用示例

上一篇咱們部署了Kubernetes集羣,接下來會在這個集羣上運行一個簡單的應用。 html

如下面的圖來安裝一個簡單的靜態內容的nginx應用: node

首先,咱們用複製器啓動一個2個備份的nginx Pod。而後在前面掛Service,一個service只能被集羣內部訪問,一個能被集羣外的節點訪問。 nginx

1. 部署nginx pod 和複製器 web

#cat nginx-rc.yaml 
apiVersion: v1 
kind: ReplicationController 
metadata: 
  name: nginx-controller 
spec: 
  replicas: 2 
  selector: 
    name: nginx 
  template: 
    metadata: 
      labels: 
        name: nginx 
    spec: 
      containers: 
        - name: nginx 
          image: nginx 
          ports: 
            - containerPort: 80

咱們定義了一個nginx pod複製器,複製份數爲2,咱們使用nginx docker鏡像。 docker

執行下面的操做建立nginx pod複製器: shell

[root@master test]# kubectl create -f nginx-rc.yaml 
replicationcontrollers/nginx-controller
記得先去下載gcr.io鏡像,而後更名,不然會提示失敗。因爲還會下載nginx鏡像,因此所建立的Pod須要等待一些時間才能處於running狀態。
[root@master test]# kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx                    1/1       Running   0          1d
nginx-controller-dkl3v   1/1       Running   0          14s
nginx-controller-hxcq8   1/1       Running   0          14s

咱們可使用describe 命令查看pod的相關信息: api

[root@master test]# kubectl describe pod nginx-controller-dkl3v
Name:				nginx-controller-dkl3v
Namespace:			default
Image(s):			nginx
Node:				192.168.32.17/192.168.32.17
Labels:				name=nginx
Status:				Running
Reason:				
Message:			
IP:				172.17.67.2
Replication Controllers:	nginx-controller (2/2 replicas created)
Containers:
  nginx:
    Image:		nginx
    State:		Running
      Started:		Wed, 30 Dec 2015 02:03:19 -0500
    Ready:		True
    Restart Count:	0
Conditions:
  Type		Status
  Ready 	True 
Events:
  FirstSeen				LastSeen			Count	From			SubobjectPath			Reason		Message
  Wed, 30 Dec 2015 02:03:14 -0500	Wed, 30 Dec 2015 02:03:14 -0500	1	{scheduler }						scheduled	Successfully assigned nginx-controller-dkl3v to 192.168.32.17
  Wed, 30 Dec 2015 02:03:15 -0500	Wed, 30 Dec 2015 02:03:15 -0500	1	{kubelet 192.168.32.17}	implicitly required container POD	pulled		Pod container image "kubernetes/pause" already present on machine
  Wed, 30 Dec 2015 02:03:16 -0500	Wed, 30 Dec 2015 02:03:16 -0500	1	{kubelet 192.168.32.17}	implicitly required container POD	created		Created with docker id e88dffe46a28
  Wed, 30 Dec 2015 02:03:17 -0500	Wed, 30 Dec 2015 02:03:17 -0500	1	{kubelet 192.168.32.17}	implicitly required container POD	started		Started with docker id e88dffe46a28
  Wed, 30 Dec 2015 02:03:18 -0500	Wed, 30 Dec 2015 02:03:18 -0500	1	{kubelet 192.168.32.17}	spec.containers{nginx}		created		Created with docker id 25fcb6b4ce09
  Wed, 30 Dec 2015 02:03:19 -0500	Wed, 30 Dec 2015 02:03:19 -0500	1	{kubelet 192.168.32.17}	spec.containers{nginx}		started		Started with docker id 25fcb6b4ce09
2. 部署節點內部可訪問的nginx service

Service的type有ClusterIP和NodePort之分,缺省是ClusterIP,這種類型的Service只能在集羣內部訪問。配置文件以下: curl

#cat nginx-service-clusterip.yaml 
apiVersion: v1 
kind: Service 
metadata: 
  name: nginx-service-clusterip 
spec: 
  ports: 
    - port: 8001 
      targetPort: 80 
      protocol: TCP 
  selector: 
    name: nginx

執行下面的命令建立service: tcp

[root@master test]# kubectl create -f ./nginx-service-clusterip.yaml 
services/nginx-service-clusterip
查看所建立的service:
[root@master test]# kubectl get service
NAME                      LABELS                                    SELECTOR     IP(S)            PORT(S)
kubernetes                component=apiserver,provider=kubernetes   <none>       10.254.0.1       443/TCP
nginx-service-clusterip   <none>                                    name=nginx   10.254.234.255   8001/TCP
上面的輸出告訴咱們這個 Service的Cluster IP是10.254.234.255,端口是8001。下面咱們驗證這個PortalNet IP的工做狀況:

在192.168.32.16上執行如下命令: ide

[root@minion1 ~]# curl -s 10.254.234.255:8001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

從前面部署複製器的部分咱們知道nginx Pod運行在17節點上。上面咱們特地從16代理節點上訪問咱們的服務來體現Service Cluster IP在全部集羣代理節點的可到達性。

3. 部署外部可訪問的nginx service

下面咱們建立NodePort類型的Service,這種類型的Service在集羣外部是能夠訪問。配置文件以下:

cat nginx-service-nodeport.yaml 
apiVersion: v1 
kind: Service 
metadata: 
  name: nginx-service-nodeport 
spec: 
  ports: 
    - port: 8000
      targetPort: 80 
      protocol: TCP 
  type: NodePort
  selector: 
    name: nginx
執行以下命令建立service並進行查看:
[root@master test]# kubectl create -f ./nginx-service-nodeport.yaml 
You have exposed your service on an external port on all nodes in your
cluster.  If you want to expose this service to the external internet, you may
need to set up firewall rules for the service port(s) (tcp:31000) to serve traffic.

See http://releases.k8s.io/HEAD/docs/user-guide/services-firewalls.md for more details.
services/nginx-service-nodeport

[root@master test]# kubectl get service
NAME                      LABELS                                    SELECTOR     IP(S)            PORT(S)
kubernetes                component=apiserver,provider=kubernetes   <none>       10.254.0.1       443/TCP
nginx-service-clusterip   <none>                                    name=nginx   10.254.234.255   8001/TCP
nginx-service-nodeport    <none>                                    name=nginx   10.254.210.68    8000/TCP
建立service時提示須要設置firewall rules,不用去管,不影響後續操做。

查看建立的service:

[root@master test]# kubectl describe service nginx-service-nodeport
Name:			nginx-service-nodeport
Namespace:		default
Labels:			<none>
Selector:		name=nginx
Type:			NodePort
IP:			10.254.210.68
Port:			<unnamed>	8000/TCP
NodePort:		<unnamed>	31000/TCP
Endpoints:		172.17.67.2:80,172.17.67.3:80
Session Affinity:	None
No events.
這個 Service的節點級別端口是31000。下面咱們驗證這個 Service的工做狀況:
[root@master test]# curl -s 192.168.32.16:31000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

[root@master test]# curl -s 192.168.32.17:31000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
無論是從16仍是17,都能訪問到咱們的服務。

4. 總結

本文只是一個簡單的應用,在應用部署時,瞭解Kubernetes的應用模型是很是重要的。還須要對Kubernetes進行更深刻的研究。

相關文章
相關標籤/搜索