Kubernetes(K8s)(五)——在K8s集群上搭建redis和docker的留言簿

(1).实验环境

  需要三个镜像:web前端镜像php-frontend、redis-master和redis-slave。其中web前端通过JavaScript redis api实现与redis-master交互。

(2).扩展:flannel

  flannel是K8s默认提供的网络插件。Flannel是由CoreOs团队开发社交的网络工具,CoreOS团队采用L3 Overlay模式设计flannel, 规定宿主机下各个Pod属于同一个子网,不同宿主机下的Pod属于不同的子网。

  flannel会在每一个宿主机上运行名为flanneld代理,其负责为宿主机预先分配一个子网,并为Pod分配IP地址。Flannel使用Kubernetes或etcd来存储网络配置、分配的子网和主机公共IP等信息。数据包则通过VXLAN、UDP或host-gw这些类型的后端机制进行转发。

  看一下flannel在Kubernetes中运行的整体过程:

Kubernetes(K8s)(五)——在K8s集群上搭建redis和docker的留言簿

(3).准备使用到的镜像

   在两个node节点上下载docker.io/kubeguide/guestbook-php-frontend:latest、docker.io/kubeguide/redis-master:latest、docker.io/kubeguide/guestbook-redis-slave:latest。

# docker pull docker.io/kubeguide/guestbook-php-frontend
Using default tag: latest
Trying to pull repository docker.io/kubeguide/guestbook-php-frontend ... 
latest: Pulling from docker.io/kubeguide/guestbook-php-frontend
4d2e9ae40c41: Pull complete 
a3ed95caeb02: Pull complete 
54a7f0f294dc: Pull complete 
a25fe735d07d: Pull complete 
0c906d123edf: Pull complete 
444908521f56: Pull complete 
8c642d1244eb: Pull complete 
9b221da943a2: Pull complete 
88bb2a5d51e9: Pull complete 
d74e6c5bcbaf: Pull complete 
54d6cf15da9c: Pull complete 
55f6a4aed401: Pull complete 
45539a0ff9ad: Pull complete 
023f5f1f5f40: Pull complete 
dfdabf8691ea: Pull complete 
09a6ba60b929: Pull complete 
1b0035990bdd: Pull complete 
d455473d6864: Pull complete 
7200260c49a3: Pull complete 
400be507a72c: Pull complete 
6d9e0a886b69: Pull complete 
66e7bb4a52de: Pull complete 
Digest: sha256:195181e0263bcee4ae0c3e79352bbd3487224c0042f1b9ca8543b788962188ce
Status: Downloaded newer image for docker.io/kubeguide/guestbook-php-frontend:latest
# docker pull docker.io/kubeguide/redis-master
Using default tag: latest
Trying to pull repository docker.io/kubeguide/redis-master ... 
latest: Pulling from docker.io/kubeguide/redis-master
a3ed95caeb02: Pull complete 
9cf7e98c567b: Pull complete 
d2ff49536f4d: Pull complete 
f94adccdbb9c: Pull complete 
6ead1ce47d52: Pull complete 
62a3114e0031: Pull complete 
dcb903eb752c: Pull complete 
0c2c7c7f11ef: Pull complete 
c7aa377bfeeb: Pull complete 
3d7aab9ba591: Pull complete 
85e54ffe4941: Pull complete 
4756da354f92: Pull complete 
Digest: sha256:e11eae36476b02a195693689f88a325b30540f5c15adbf531caaecceb65f5b4d
Status: Downloaded newer image for docker.io/kubeguide/redis-master:latest
# docker pull docker.io/kubeguide/guestbook-redis-slave 
Using default tag: latest
Trying to pull repository docker.io/kubeguide/guestbook-redis-slave ... 
latest: Pulling from docker.io/kubeguide/guestbook-redis-slave
70c964415e86: Pull complete 
a3ed95caeb02: Pull complete 
3229dc640f23: Pull complete 
cc7958dd97b7: Pull complete 
448093e93ac3: Pull complete 
2bbcbb97d47b: Pull complete 
87b4f6fd65c9: Pull complete 
5b712ee108fe: Pull complete 
a1a961e320bc: Pull complete 
6f37ec7ab693: Pull complete 
Digest: sha256:a36fec97659fe96b5b28750d88b5cfb84a45138bcf1397c8e237031b8855c58c
Status: Downloaded newer image for docker.io/kubeguide/guestbook-redis-slave:latest

(4).创建配置文件

  创建一个专门存放YAML配置文件的目录

[root@kube-master ~]# mkdir -p /etc/kubernetes/yaml

 1)redis master deployment配置文件

[root@kube-master ~]# vim /etc/kubernetes/yaml/redis-master-deployment.yaml
apiVersion: extensions/v1beta1  #api版本
kind: Deployment  #类型
metadata:  #deployment元数据(属性)
  name: redis-master  #deployment名称
# 如果没有设置标签,这些标签可以从Pod模板中应用(获取?) # these labels can be applied automatically from the labels in the pod template if not set # labels: # app: redis # role: master # tier: backend spec:  #deployment详细定义
# 此副本值是默认值,请根据需要修改 # this replicas value is default modify it according to your case replicas: 1  #副本期望值
# 如果没有设置选择器,选择器可以从Pod模板中的标签中应用(获取?) # selector can be applied automatically from the labels in the pod template if not set # selector:   # matchLabels: # app: guestbook # role: master # tier: backend template:  #模板 metadata:  #Pod元数据(属性) labels:  #自定义标签 app: redis role: master tier: backend spec:  #Pod详细定义 containers:  #容器 - name: master  #容器名称的一部分 image: docker.io/kubeguide/redis-master:latest  #采用镜像 imagePullPolicy: IfNotPresent  #镜像拉取策略 resources:  #容器运行资源限制 requests:  #运行下限 cpu: 100m  #0.1核 memory: 100Mi  #100Mb(1024单位) ports:  #对外开放端口列表 - containerPort: 6379  #容器对外开放端口

 2)redis master service配置文件

[root@kube-master ~]# vim /etc/kubernetes/yaml/redis-master-service.yaml
apiVersion: v1  #api版本
kind: Service  #类型
metadata:  #service元数据(属性)
  name: redis-master  #service名称
  labels:  #自定义标签
    app: redis
    role: master
    tier: backend
spec:  #service详细定义
  ports:  #服务对外开放端口列表
    # the port that this service should serve on
  - port: 6379  #提供给内部Pod访问使用的端口
    targetPort: 6379  #Pod内服务的端口
  selector:  #service作用范围,此处是同时具备以下标签时,采用当前service
    app: redis
    role: master
    tier: backend

 3)redis slave deployment配置文件

[root@kube-master ~]# vim /etc/kubernetes/yaml/redis-slave-deployment.yaml
apiVersion: extensions/v1beta1  #api版本
kind: Deployment  #类型
metadata:  #deployment元数据(属性)
  name: redis-slave  #deployment名称
  # 如果没有设置标签,这些标签可以从Pod模板中应用(获取?)
  # these labels can be applied automatically from the labels in the pod template if not set
  # labels:
  #   app: redis
  #   role: slave
  #   tier: backend
spec:  #deployment详细定义
  # 此副本值是默认值,请根据需要修改
  # this replicas value is default modify it according to your case
  replicas: 2  #副本期望值
  # 如果没有设置选择器,选择器可以从Pod模板中的标签中应用(获取?)
  # selector can be applied automatically from the labels in the pod template if not set
  # selector:
  #   matchLabels:
  #     app: guestbook
  #     role: slave
  #     tier: backend
  template:  #模板
    metadata:  #Pod元数据(属性)
      labels:  #自定义标签
        app: redis
        role: slave
        tier: backend
    spec:  #Pod详细定义
      containers:  #容器
      - name: slave  #容器名称的一部分
        image: docker.io/kubeguide/guestbook-redis-slave:latest  #采用镜像
        imagePullPolicy: IfNotPresent  #镜像拉取策略
        resources:  #容器运行资源限制
          requests:  #运行下限
            cpu: 100m  #0.1核
            memory: 100Mi  #100Mb(1024单位)
        env:  #环境变量设置
        - name: GET_HOSTS_FROM  #环境变量名称
          value: env  #环境变量值
          # 如果集群配置不包括DNS服务,则要访问环境变量来查找主服务的主机,请注释掉上面的'value: dns',并取消注释下面的行
          # If your cluster config does not include a dns service, then to
          # instead access an environment variable to find the master
          # service's host, comment out the 'value: dns' line above, and
          # uncomment the line below.
          # value: env
        ports:  #对外开放端口列表
        - containerPort: 6379  #容器对外开放端口

 4)redis slave service配置文件

[root@kube-master ~]# vim /etc/kubernetes/yaml/redis-slave-service.yaml
apiVersion: v1  #api版本
kind: Service  #类型
metadata:  #service元数据(属性)
  name: redis-slave  #service名称
  labels:  #自定义标签
    app: redis
    role: slave
    tier: backend
spec:  #service详细定义
  ports:  #服务对外开放端口列表
    # the port that this service should serve on
  - port: 6379  #提供给内部Pod访问使用的端口
  selector:  #service作用范围,此处同时具备以下标签时,采用当前service
    app: redis
    role: slave
    tier: backend

 5)php frontend deployment配置文件

[root@kube-master ~]# vim /etc/kubernetes/yaml/frontend-deployment.yaml
apiVersion: extensions/v1beta1  #api版本
kind: Deployment  #类型
metadata:  #deployment元数据(属性)
  name: frontend  #deployment名称
  # 如果没有设置标签,这些标签可以从Pod模板中应用(获取?)
  # these labels can be applied automatically from the labels in the pod template if not set
  # labels:
  #   app: guestbook
  #   tier: frontend
spec:  #deployment详细定义
  # 此副本值是默认值,请根据需要修改
  # this replicas value is default modify it according to your case
  replicas: 3  #副本期望值
  # 如果没有设置选择器,选择器可以从Pod模板中的标签中应用(获取?)
  # selector can be applied automatically from the labels in the pod template if not set
  # selector:
  #   matchLabels:
  #     app: guestbook
  #     tier: frontend
  template:  #模板
    metadata:  #Pod元数据(属性)
      labels:  #自定义标签
        app: guestbook
        tier: frontend
    spec:  #Pod详细定义
      containers:  #容器
      - name: php-redis  #容器名称的一部分
        image: docker.io/kubeguide/guestbook-php-frontend:latest  #采用镜像
        imagePullPolicy: IfNotPresent  #镜像拉取策略
        resources:  #容器运行资源限制
          requests:  #下限
            cpu: 100m  #0.1核
            memory: 100Mi  #100Mb(1024单位)
        env:  #环境变量设置
        - name: GET_HOSTS_FROM  #环境变量名称
          value: env  #环境变量值
          # 如果集群配置不包含DNS服务,则要访问环境变量来查找主服务的主机,请注释掉上面的'value: dns',并取消注释下面的行
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:  #对外开放端口列表
        - containerPort: 80  #容器对外开放端口

 6)php frontend service配置文件

[root@kube-master ~]# vim /etc/kubernetes/yaml/frontend-service.yaml
apiVersion: v1  #api版本
kind: Service  #类型
metadata:  #service元数据(属性)
  name: frontend  #service名称
  labels:  #自定义标签
    app: guestbook
    tier: frontend
spec:  #service详细定义
  # if your cluster supports it, uncomment the following to automatically creat
e
  # an external load-balanced IP for the frontend service.
  # type: LoadBalancer
  type: NodePort  #服务访问方式
  ports:  #服务对外开放端口列表
    # the port that this service should serve on
  - port: 80  #提供给内部Pod访问使用的端口
    nodePort: 30001  #提供给外部访问服务的节点上的端口
  selector:  #service作用范围,此处同时具备以下标签时,采用当前service
    app: guestbook
    tier: frontend

(5).通过YAML文件启动

  启动deployment和service

[root@kube-master ~]# kubectl create -f /etc/kubernetes/yaml/redis-master-deployment.yaml
deployment "redis-master" created
[root@kube-master ~]# kubectl create -f /etc/kubernetes/yaml/redis-master-service.yaml
service "redis-master" created
[root@kube-master ~]# kubectl create -f /etc/kubernetes/yaml/redis-slave-deployment.yaml
deployment "redis-slave" created
[root@kube-master ~]# kubectl create -f /etc/kubernetes/yaml/redis-slave-service.yaml
service "redis-slave" created
[root@kube-master ~]# kubectl create -f /etc/kubernetes/yaml/frontend-deployment.yaml
deployment "frontend" created
[root@kube-master ~]# kubectl create -f /etc/kubernetes/yaml/frontend-service.yaml
service "frontend" created

  查看deployment和service,下方数据为清理掉非相关内容后的数据。

[root@kube-master ~]# kubectl get deployment -o wide
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
frontend       3         3         3            3           6m
redis-master   1         1         1            1           6m
redis-slave    2         2         2            2           6m         
[root@kube-master ~]# kubectl get replicaset -o wide
NAME                      DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                                            SELECTOR
frontend-1186687533       3         3         3         6m        php-redis      docker.io/kubeguide/guestbook-php-frontend:latest   app=guestbook,pod-template-hash=1186687533,tier=frontend
redis-master-3671804942   1         1         1         7m        master         docker.io/kubeguide/redis-master:latest             app=redis,pod-template-hash=3671804942,role=master,tier=backend
redis-slave-2377017994    2         2         2         7m        slave          docker.io/kubeguide/guestbook-redis-slave:latest    app=redis,pod-template-hash=2377017994,role=slave,tier=backend
[root@kube-master ~]# kubectl get pod -o wide                       
NAME                            READY     STATUS    RESTARTS   AGE       IP            NODE
frontend-1186687533-4jns2       1/1       Running   0          7m        10.255.31.8   kube-node2
frontend-1186687533-dqcdj       1/1       Running   0          7m        10.255.39.4   kube-node1
frontend-1186687533-vg8fw       1/1       Running   0          7m        10.255.39.5   kube-node1
redis-master-3671804942-8tq84   1/1       Running   0          8m        10.255.39.2   kube-node1
redis-slave-2377017994-1zctb    1/1       Running   0          8m        10.255.31.7   kube-node2
redis-slave-2377017994-rqr4m    1/1       Running   0          8m        10.255.39.3   kube-node1
[root@kube-master ~]# kubectl get service -o wide
NAME           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE       SELECTOR
frontend       10.254.186.13   <nodes>       80:30001/TCP     8m        app=guestbook,tier=frontend
kubernetes     10.254.0.1      <none>        443/TCP          42d       <none>
redis-master   10.254.137.74   <none>        6379/TCP         8m        app=redis,role=master,tier=backend
redis-slave    10.254.72.23    <none>        6379/TCP         8m        app=redis,role=slave,tier=backend

(6).开启node节点上的路由转发功能

  临时开启方法一:

# iptables -P FORWARD ACCEPT

  临时开启方法二:

# echo "1" > /proc/sys/net/ipv4/ip_forward

  永久开启

# vim /etc/sysctl.conf
//添加或修改如下行
net.ipv4.ip_forward = 1
# sysctl -p    //刷新使参数生效
net.ipv4.ip_forward = 1

(7).通过浏览器访问

Kubernetes(K8s)(五)——在K8s集群上搭建redis和docker的留言簿Kubernetes(K8s)(五)——在K8s集群上搭建redis和docker的留言簿

 

上一篇:(33)[ICLR16] TOWARDS AI-COMPLETE QUESTION ANSWERING: A SET OF PREREQUISITE TOY TASKS


下一篇:A Complete Tutorial on Time Series Modeling in R