kubernetes(k8s)第五部分之K8s部署nginx

1】创建nginx-rc.yaml文件

[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: registry:5000/nginx
          imagePullPolicy: Never
          ports:
            - containerPort: 80

2】创建nginx-service.yaml文件

[root@k8s-master ~]# cat nginx-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  ports:
    - port: 8000
      targetPort: 80
      protocol: TCP
  type: NodePort
  selector:
name: nginx

3】创建pod和service

注:所有节点都要有registry:5000/nginx镜像

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

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

4】查看pod

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

[root@k8s-master ~]# kubectl get po
NAME                     READY     STATUS    RESTARTS   AGE
nginx-controller-pbkm6   1/1       Running   0          56s
nginx-controller-zcvdk   1/1       Running   0          56s

5】查看service

[root@k8s-master ~]# kubectl get svc
NAME                     CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes               10.254.0.1       <none>        443/TCP          1h
nginx-service-nodeport   10.254.224.123   <nodes>       8000:30473/TCP   1m

6】查看service详细信息

[root@k8s-master ~]# kubectl describe svc nginx-service-nodeport
Name:			nginx-service-nodeport
Namespace:		default
Labels:			<none>
Selector:		name=nginx
Type:			NodePort
IP:			10.254.224.123
Port:			<unset>	8000/TCP
NodePort:		<unset>	30473/TCP
Endpoints:		10.0.25.2:80,10.0.56.2:80
Session Affinity:	None
No events.

7】从上面的描述可以看出在node节点访问30473即可访问nginx

[root@k8s-node1 ~]# curl 172.25.37.12:30473
<!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@k8s-node2 ~]# curl 172.25.37.13:30473
<!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@k8s-master ~]# kubectl delete  ReplicationController nginx-controller

详细内容可以参考:https://blog.csdn.net/xinghun_4/article/details/50492041



  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值