Skip to content

Kubernetes—使用Minikube搭建Nginx集群 #32

@johnnian

Description

@johnnian

目标

使用Minikube,在Kubernetes上搭建一个Nginx集群(3个节点)

备注:对于如何安装环境,请点击这里

开发环境

名称 版本号
Kubernetes V1.7
minikube v0.21.0
kubectl-Client v1.7.4
kubectl-Server v1.7.0
操作系统 MacOS
➜  ~ minikube version
minikube version: v0.21.0
➜  ~ kubectl version
Client Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.4", GitCommit:"793658f2d7ca7f064d2bdf606519f9fe1229c381", GitTreeState:"clean", BuildDate:"2017-08-17T17:03:51Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.0", GitCommit:"d3ada0119e776222f11ec7945e6d860061339aad", GitTreeState:"clean", BuildDate:"2017-07-26T00:12:31Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}

yaml配置文件

创建 nginx-controller.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

创建 nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    name: nginx-service
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30001
    targetPort: 80
  selector:
    name: nginx

创建步骤

➜  kubectl create -f nginx-controller.yaml
➜  kubectl get deployment
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           10m

➜  kubectl get pod
nginx-deployment-1969926507-cs4xb   1/1       Running   0          11m
nginx-deployment-1969926507-dc8nh   1/1       Running   0          11m
nginx-deployment-1969926507-nmtxp   1/1       Running   0          11m

➜  kubectl create -f nginx-service.yaml
➜  kubectl get service
NAME              CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
nginx-service     10.0.0.104   <nodes>       80:30001/TCP     9m

备注: 这里的 80:30001, 对应的是:service端口:主机Node端口

验证

➜  minikube ip
192.168.99.100

➜  test curl 192.168.99.100:30001
<!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>

网络拓扑图

qq20170905-195758 2x

网络映射关系:

主机Node-192.168.99.100:30001 <----> Service-10.0.0.104:80 <----> Deployment(3个Pod)-(端口:80; Label: name: nginx)

Service 通过 Label Select 找到 “name: nginx” 标签的Pod,通过内部的iptables进行路由映射,映射到对应Pod的80端口。

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions