【Kubernetes学习之三】Kubernetes分布式集群架构

环境
  centos 7

一、Kubernetes分布式集群架构
1、Kubernetes服务注册和服务发现问题怎么解决的?
每个服务分配一个不变的虚拟IP+端口, 系统env环境变量里有每个服务的服务名称到IP的映射。


2、服务的负载均衡问题怎么解决的?
每个节点上都有一个软件实现的服务代理来实现负载均衡

 

3、服务的规模部署问题怎么解决的?
目标导向的做法:确定部署实例数,系统自动调度

4、服务运维问题如何解决的?
自动监控、自我修复

5、集中配置,并且实时配置实施生效

二、集群架构示例
1、示例
一个PHP做的留言板:3台webserver做PHP-Frontend前台,1台redis-master用来写,2台redis-slave做读。


2、集群搭建图
创建三个service:frontend Service、redis-master Service、redis-slave Service

三、搭建
1、创建redis-master Pod
(1)定义redis-master-controller.yaml文件:

[root@node106 ~]# vi redis-master-controller.yaml
apiVersion: v1 
kind: ReplicationController 
metadata: 
  name: redis-master 
  labels: 
    name: redis-master 
spec: 
  replicas: 1 
  selector: 
    name: redis-master 
  template: 
    metadata: 
      labels: 
        name: redis-master 
    spec: 
      containers: 
      - name: master 
        image: kubeguide/redis-master 
        ports: 
        - containerPort: 6379

(2)执行创建命令

[root@node106 ~]# kubectl create -f redis-master-controller.yaml 
replicationcontroller/redis-master created

(3)查看pod

[root@node106 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
redis-master-659bm       1/1     Running   0          4m46s

2、创建redis-master 服务

(1)定义redis-master-service.yaml

[root@node106 ~]# vi redis-master-service.yaml
apiVersion: v1 
kind: Service 
metadata: 
  name: redis-master 
  labels: 
    name: redis-master 
spec: 
  ports: 
  - port: 6379 
    targetPort: 6379 
  selector: 
    name: redis-master

(2)执行创建命令

[root@node106 ~]# kubectl create -f redis-master-service.yaml
service/redis-master created

(3)查看服务

[root@node106 ~]# kubectl get services 
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP        46h
redis-master      ClusterIP   10.96.187.196   <none>        6379/TCP       18s

3、创建redis-slave Pod

(1)创建redis-slave-controller.yaml

[root@node106 ~]# vi redis-slave-controller.yaml
apiVersion: v1 
kind: ReplicationController 
metadata: 
  name: redis-slave 
  labels: 
    name: redis-slave 
spec: 
  replicas: 2 
  selector: 
    name: redis-slave 
  template: 
    metadata: 
      labels: 
        name: redis-slave 
    spec: 
      containers: 
      - name: slave 
        image: kubeguide/guestbook-redis-slave 
        env: 
        - name: GET_HOSTS_FROM 
          value: env 
        ports: 
        - containerPort: 6379

(2)执行创建命令

[root@node106 ~]# kubectl create -f redis-slave-controller.yaml
replicationcontroller/redis-slave created

(3)查看pod和rc

[root@node106 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
redis-master-659bm       1/1     Running   0          16m
redis-slave-kqllg        1/1     Running   0          119s
redis-slave-rfbr8        1/1     Running   0          119s
[root@node106 ~]# kubectl get rc
NAME           DESIRED   CURRENT   READY   AGE
redis-master   1         1         1       17m
redis-slave    2         2         2       2m24s

4、创建redis-slave 服务

(1)创建redis-slave-service.yaml

[root@node106 ~]# vi redis-slave-service.yaml
apiVersion: v1 
kind: Service 
metadata: 
  name: redis-slave 
  labels: 
    name: redis-slave 
spec: 
  ports: 
  - port: 6379 
  selector: 
    name: redis-slave

(2)执行创建命令

[root@node106 ~]# kubectl create -f redis-slave-service.yaml
service/redis-slave created

(3)查看服务

[root@node106 ~]# kubectl get services 
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP        46h
redis-master      ClusterIP   10.96.187.196   <none>        6379/TCP       11m
redis-slave       ClusterIP   10.99.146.233   <none>        6379/TCP       33s

5、创建frontend Pod

(1)创建frontend-controller.yaml

[root@node106 ~]# vi frontend-controller.yaml
apiVersion: v1 
kind: ReplicationController 
metadata: 
  name: frontend 
  labels: 
    name: frontend 
spec: 
  replicas: 3 
  selector: 
    name: frontend 
  template: 
    metadata: 
      labels: 
        name: frontend 
    spec: 
      containers: 
      - name: frontend 
        image: kubeguide/guestbook-php-frontend 
        env: 
        - name: GET_HOSTS_FROM 
          value: env 
        ports: 
        - containerPort: 80

(2)执行创建命令

[root@node106 ~]# kubectl create -f frontend-controller.yaml
replicationcontroller/frontend created

(3)查看pod

[root@node106 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
curl-6bf6db5c4f-dn65h    1/1     Running   2          41h
frontend-jjpzd           1/1     Running   0          6m50s
frontend-p2jpn           1/1     Running   0          6m50s
frontend-rm4z4           1/1     Running   0          6m50s
redis-master-659bm       1/1     Running   0          30m
redis-slave-kqllg        1/1     Running   0          15m
redis-slave-rfbr8        1/1     Running   0          15m

6、创建frontend 服务

(1)创建frontend-service.yaml

[root@node106 ~]# vi frontend-service.yaml
apiVersion: v1 
kind: Service 
metadata: 
  name: frontend 
  labels: 
    name: frontend 
spec: 
  type: NodePort 
  ports: 
  - port: 80 
    nodePort: 30001 
  selector: 
    name: frontend

(2)执行创建命令

[root@node106 ~]# kubectl create -f frontend-service.yaml 
service/frontend created

(3)查看服务

[root@node106 ~]# kubectl get services 
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
frontend          NodePort    10.110.39.201   <none>        80:30001/TCP   34s
kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP        46h
redis-master      ClusterIP   10.96.187.196   <none>        6379/TCP       24m
redis-slave       ClusterIP   10.99.146.233   <none>        6379/TCP       13m

验证:

posted @ 2019-09-08 11:33  cac2020  阅读(865)  评论(0编辑  收藏  举报