快速创建一个pod、service

[size=large]1. 查看机器节点[/size]
# kubectl get node

[img]http://dl2.iteye.com/upload/attachment/0119/8233/e5a96a24-e165-3387-b9d9-5969489f9787.png[/img]

[size=large]2. 用命令启动一个nginx[/size]
#kubectl run my-nginx --image=nginx --replicas=2 --port=80

[img]http://dl2.iteye.com/upload/attachment/0119/8235/8213890f-c773-3539-8455-301c6b0b1d49.png[/img]

[size=large]3. 暴露服务[/size]
# kubectl expose deployment my-nginx --port=8080 --target-port=80 --external-ip=x.x.x.168

[img]http://dl2.iteye.com/upload/attachment/0119/8237/10d0ecd7-5c31-38da-bacf-836b3a9be050.png[/img]
参数说明
--port 容器的端口
--container-port和target-port是一个意思,指的是宿主机转发的端口,可以随意指定一个,也可以不指定
--external-ip 对外暴露的ip地址,一般用公网IP地址,执行那个命令过后,我们就可以在公网上访问了,但是这里有个问题就是这个IP地址必须是安装了k8s的机器的IP,如果你随便用一个IP是不能访问的,这里也给应用上造成了不便
--type=LoadBalancer

[size=large]4. 查看pod[/size]
# kubectl get po

[img]http://dl2.iteye.com/upload/attachment/0119/8239/45b9a047-7440-3a01-8da6-245dde8d920a.png[/img]

[size=large]5. 查看service[/size]
# kubectl get svc

[img]http://dl2.iteye.com/upload/attachment/0119/8241/87df139f-8d91-3f49-8312-876cb352e607.png[/img]
# kubectl get service my-nginx
[img]http://dl2.iteye.com/upload/attachment/0119/8243/66cef4b0-ba98-3093-acbd-a3b591eff5fd.png[/img]

[size=large]6. 删除[/size]
# kubectl delete deployment,service my-nginx

[img]http://dl2.iteye.com/upload/attachment/0119/8245/de354439-4fd7-33cb-89d3-a0fd851cee1a.png[/img]

[size=large]7. 查看子网[/size]
通过etcdctl查看node机器的子网分配情况
# etcdctl ls -r |grep subnets
# etcdctl get /coreos.com/network/subnets/10.0.101.0-24

[img]http://dl2.iteye.com/upload/attachment/0119/8247/7b45fb50-8a15-3fb2-887d-39fab34c8dd3.png[/img]

[size=large]8. 查询pod明细[/size]
# kubectl describe po my-nginx-2494149703-2m6sc

[img]http://dl2.iteye.com/upload/attachment/0119/8249/b86d83ec-d7ff-30af-bd3e-1a37531ce042.png[/img]
初次创建要下载镜像,可能等待的时间比较长
Pod分配到了kube-node-1,容器的ip:10.0.64.2
那么我们在kube-node-1上,测试nginx是否正常启动
# curl http://10.0.64.2

[img]http://dl2.iteye.com/upload/attachment/0119/8251/83ad6f9b-311a-35e7-b9c6-4c3c46369cde.png[/img]
至此,nginx已成功启动

[size=large]9. 查看service明细[/size]
#kubectl describe service/my-nginx

[img]http://dl2.iteye.com/upload/attachment/0119/8253/a48f1aa5-a1fa-334b-9caa-68bc5e0c4c68.png[/img]
10.254.242.11是Service的虚拟IP地址,对该虚拟地址的8080端口访问会被重定向到两个 Endpoints中的一个;这两个Endpoints就是上面创建的两个Pods.
Kubernetes中的Service使用了虚拟ip地址;该地址无法ping通过,但访问8080端口时,可以访问到对应的资源
# curl http://10.254.242.11:8080

[img]http://dl2.iteye.com/upload/attachment/0119/8255/51fb69f0-9f4d-344b-a2c0-cb30427d860a.png[/img]

[size=large]10. 查看iptables的规则[/size]
# iptables-save | grep default/my-nginx

[img]http://dl2.iteye.com/upload/attachment/0119/8257/b95340a7-69d0-3244-8cb2-1cb214370ea2.png[/img]
访问10.254.242.11/32 8080端口的请求会被重定向到10.0.64.2/32或10.0.101.2/32的80端口。这些规则是由kube-proxy生成;如果需要某台机器可以访问Service,则需要在该主机启动kube-proxy

[size=large]11. 用文件启动一个nginx[/size]
编辑nginx-pod.yaml
# cat nginx-pod.yaml               
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
labels:
app: nginx-test
spec:
containers:
- name: nginx-test
image: nginx
ports:
- containerPort: 80
#kubectl create -f nginx-pod.yaml


[size=large]12. 用文件配置一个Service[/size]
# cat nginx_svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-test
labels:
app: nginx-test
spec:
selector:
run: nginx-test
ports:
protocol: TCP
port: 8080
targetPort: 80
# kubectl create -f nginx_svc.yaml



参考:[url]http://kubernetes.io/docs/user-guide/quick-start/[/url]

你可能感兴趣的:(kubernetes)