kubernetes应用部署

kubernetes应用部署  

http://blog.sina.com.cn/s/blog_6e05157f0102wqf1.html

一. 根据安装kubernetes集群环境

二. 如果本地没有docker registry,要确保节点能访问互联网

三. 确保管理节点能够ssh访问所有minion节点

部署nginx应用

kubernetes应用部署

首先,我们用复制器启动一个2个备份的nginx-pod.然后在前面挂载Service,一个service只能被集群内部访问,一个能被集群外的节点访问.下面所有的命令都是在管理节点上面运行的.

一 、 部署nginx pod 和 复制器

配置文件如下:

[root@k8s-master ~]# 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-rc.yaml定义了一个nginx pod复制器,复制人数为2,使用的是nginx docker镜像

创建nginx pod复制器:

[root@k8s-master ~]# kubectl create -f nginx-rc.yaml

replicationcontroller "nginx-controller" created

#由于kubernetes要去下载nginx镜像,所以创建pod时需要等待一些时间才能处理running状态

#查看pods状态

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

NAME                    READY    STATUS    RESTARTS  AGE

nginx-controller-2emjo  1/1      Running  0          15m

nginx-controller-2zvdj  1/1      Running  0          15m

#查看pods详细状态

[root@k8s-master ~]# kubectl get pod -o wide

NAME                    READY    STATUS    RESTARTS  AGE      NODE

nginx-controller-2emjo  1/1      Running  0          16m      172.16.116.160

nginx-controller-2zvdj  1/1      Running  0          16m      172.16.116.161

二 、部署节点内部可访问的nginx service

Server的type有ClusterIP和 NodePort之分,缺少是ClusterIp,这种类型的service只能在集群内部访问

配置文件如下:

[root@k8s-master ~]# 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

[root@k8s-master ~]# kubectl create -f nginx-service-clusterip.yaml

service "nginx-service-clusterip" created

#查看Service状态

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

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

kubernetes                10.254.0.1              443/TCP    1d

nginx-service-clusterip  10.254.241.63          8001/TCP  23s

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

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

kubernetes                10.254.0.1              443/TCP    1d

nginx-service-clusterip  10.254.241.63          8001/TCP  28s      name=nginx

##通过查看Service状态可以获取Service ClusterIP是10.254.241.63,端口是8001

#验证nginx Cluster IP

curl -s 10.254.241.63:8001  注:在minion节点执行

[root@k8s-slave2 ~]# curl -s 10.254.241.63:8001

body {

width: 35em;

margin: 0 auto;

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 to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

三、部署外部可访问的nginx service

创建NodePort类型的Service,这种类型的Service在集群外部是可以访问

[root@k8s-master ~]# 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

创建serverice

[root@k8s-master ~]# 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:32111) to serve traffic.

See http://releases.k8s.io/release-1.2/docs/user-guide/services-firewalls.md for more details.

service "nginx-service-nodeport" created

查看Service

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

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

kubernetes                10.254.0.1              443/TCP    1d

nginx-service-nodeport    10.254.116.49  nodes        8000/TCP  26s

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

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

kubernetes                10.254.0.1              443/TCP    1d

nginx-service-nodeport    10.254.116.49  nodes        8000/TCP  1m        name=nginx

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

Name:  nginx-service-nodeport

Namespace:  default

Labels:

Selector:  name=nginx

Type:  NodePort

IP:  10.254.116.49

Port:    8000/TCP

NodePort:  32111/TCP                  注:Service节点级别端口是32111

Endpoints:  172.17.0.2:80,172.17.79.2:80

Session Affinity: None

No events.

验证Service可访问性

curl 10.254.116.49:32111

你可能感兴趣的:(kubernetes应用部署)