kubernetes 创建nginx 容器
参考:http://blog.csdn.net/qq1010885678/article/details/48832067一个简单的nginx服务器
先决条件:你需要拥有的是一个部署完毕并可以正常运行的k8s集群。
1.拉 nginx 镜像,上传到私服
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105161914466-1529857366.png
方案一:使用界面 k8s dashboard 创建
1.创建rc :
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105165939779-879747959.png
创建完后:
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105165959310-253613025.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105170022373-993348711.png
两个同时创建.
因为创建rc的时候 选择了内部服务。
如图:https://images2017.cnblogs.com/blog/802205/201711/802205-20171105170108388-357562625.png
我们尝试在 minion节点上 访问
nginx
方式一: Cluster IP+port注意配置的是 targetPort 即容器内的端口是80
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105170251810-358822373.png
可以访问!
方式二:通过 pod的 Cluster IP+containerPort
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105170356904-1143660353.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105170507998-220841035.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105170519998-2000537290.png
实践证明 :都可以!
注意:2个都叫Cluster IP ,但是实际上 后面的是 容器的ip,签名的应该 是 另外的一个ip
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105171523951-2066717388.png
2个ip的区别 未来需要继续弄清楚。
另外:这三个IP都不是 minion的节点的 IP,192.168.179.135
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105171722841-2115194934.png
节点的IP 80 或81端口都不行的。
据此:尝试创建 一个外部服务
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172029232-2104139100.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172047623-83596708.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172104951-1526567257.png
测试:
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172317716-1406559508.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172146451-876416341.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172239232-1078361040.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172251388-386465893.png
以上三个IP都可以。
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172533451-1925248438.png
重点:minion节点的IP+ 32108 端口居然可以访问
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105172735420-445694745.png
方案二:通过yaml文件创建rc和svc 即rc和service 本方法没验证,但是yaml是从 界面里拷出来的。理论上可以的
1.创建rc
https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gifhttps://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif
Replication Controller {5}
kind : ReplicationController
apiVersion : v1
metadata {8}
name : nginx-hui
namespace : default
selfLink : /api/v1/namespaces/default/replicationcontrollers/nginx-hui
uid : 80f386f8-c206-11e7-859d-000c29e983de
resourceVersion : 55282
generation : 1
creationTimestamp : 2017-11-05T08:51:24Z
labels {1}
app : nginx-hui
spec {3}
replicas : 2
selector {1}
app : nginx-hui
template {2}
metadata {3}
name : nginx-hui
creationTimestamp : null
labels {1}
app : nginx-hui
spec {5}
containers
0 {6}
name : nginx-hui
image : 192.168.179.133:80/nginx
resources {0}
(empty object)
terminationMessagePath : /dev/termination-log
imagePullPolicy : Always
securityContext {1}
privileged : false
restartPolicy : Always
terminationGracePeriodSeconds : 30
dnsPolicy : ClusterFirst
securityContext {0}
(empty object)
status {5}
replicas : 2
fullyLabeledReplicas : 2
readyReplicas : 2
availableReplicas : 2
observedGeneration : 1
View Code 2.创建svc
https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gifhttps://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif
Service {5}
kind : Service
apiVersion : v1
metadata {7}
name : nginx-hui
namespace : default
selfLink : /api/v1/namespaces/default/services/nginx-hui
uid : 80fe1ae1-c206-11e7-859d-000c29e983de
resourceVersion : 55241
creationTimestamp : 2017-11-05T08:51:24Z
labels {1}
app : nginx-hui
spec {5}
ports
0 {4}
name : tcp-81-80-8yb4e
protocol : TCP
port : 81
targetPort : 80
selector {1}
app : nginx-hui
clusterIP : 10.254.239.9
type : ClusterIP
sessionAffinity : None
status {1}
loadBalancer {0}
(empty object)
View Code 实际是在 服务器上master上 创建2个yaml文件
rc.yaml和svc.yaml
然后用
kubectl create -f xx.yaml分别创建 rc和service
方案三:下面是采用命令方式创建:(未成功)
2. 在Master节点上使用kubectl命令来启动一个运行着nginx服务器的容器:
kubectl run my-nginx --image=192.168.179.133:80/nginx --replicas=2 --port=80
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105162026060-1901622171.png
注:
以上命令会让节点上的Docker从nginx这个image上启动一个容器监听80端口,此为一个pod。
而replicas=2则表示会起两个一模一样的pod。
k8s会确保你的应用是一直运行的,当容器运行失败时,k8s会自动重启容器,当整个节点失败时,会在另外一个健康的节点启动这个容器。
界面查看 是2个pods
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105162322232-1019991755.png
https://images2017.cnblogs.com/blog/802205/201711/802205-20171105162742357-852937910.png 3.通过端口将应用连接到Internet上
以下命令将上一步骤中的nginx容器连接到公网中:
kubectl expose rc my-nginx --port=80 --type=LoadBalancer
注:rc即Replication Controller,上一步骤中的命令其实会自动创建一个名为my-nginx的rc来确保pod的数量维持在2个。
实际测试中没有这个rc叫my-nginx
使用以下命令来查看rc:
$ kubectl get rc 也可以界面:
页:
[1]