k8s(Kubernetes)快速部署捷径(6)-集群mysql应用实例

【目录】
(1)k8s快速介绍
(2)硬件虚拟机环境Ubuntu18.10-server安装
(3)Docker-CE安装及配置
(4)Kubernetes 安装及部署
(5)创建企业Kubernetes多主机集群环境
(6)Kubernetes集群mysql应用实例
(7)Kubernetes集群tomcat应用集群实例
(8)Kubernetes集群beego新闻发布web系统集群实例
【资源下载】
完整教程markdown下载 提取码: 92hy
kubernetes(k8s)2小时快速部署教程视频 提取码:dk4i
kubernetes-Master-Node虚拟机完整镜像文件 提取码: 2wf8
课程相关其他相关安装包及镜像 提取码: nkfj


六、应用实例

6.1 创建MySQL实例

6.1.1 定义描述文件

apiVersion: v1
kind: ReplicationController                            #副本控制器RC
metadata:
  name: mysql                                          #RC的名称,全局唯一
spec:
  replicas: 1                                          #Pod副本的期待数量
  selector:
    app: mysql                                         #符合目标的Pod拥有此标签
  template:                                            #根据此模板创建Pod的副本(实例)
    metadata:
      labels:
        app: mysql                                     #Pod副本拥有的标签,对应RC的Selector
    spec:
      containers:                                      #Pod内容器的定义部分
      - name: mysql                                    #容器的名称
        image: hub.c.163.com/library/mysql              #容器对应的Docker image
        ports: 
        - containerPort: 3306                          #容器应用监听的端口号
        env:                                           #注入容器内的环境变量
        - name: MYSQL_ROOT_PASSWORD 
          value: "123456"

6.1.2 加载ReplicationController副本控制器描述文件

创建好mysql-rc.yaml后,在master节点使用kubectl命令将它发布到k8s集群中。

kubectl create -f mysql-rc.yaml

6.1.3 查看启动状态

通过查看当前的pods列表,是否已经启动成功:

6.1.4 网络异常解决方案(未出现问题可直接跳过)

注意:如果这里出现了ContainerCreating状态,那么可以尝试如下解决办法:

itcast@master:~/working$ kubectl get pods
NAME          READY   STATUS              RESTARTS   AGE
mysql-tscrh   0/1     ContainerCreating   0          17m

目前mysql-tscrh 描述文件 已经创建,但是没有启动成功. 状态是ContainerCreating 没有启动起来.

通过kubectl describe pods来查看pods的详细状态

itcast@master:~/working$ kubectl describe pods mysql
Name:               mysql-tscrh
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node1/192.168.236.178
Start Time:         Mon, 17 Jun 2019 09:10:35 +0000
Labels:             app=mysql
Annotations:        
Status:             Pending
IP:                 
Controlled By:      ReplicationController/mysql
Containers:
  mysql:
    Container ID:   
    Image:          hub.c.163.com/library/mysql
    Image ID:       
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:
      MYSQL_ROOT_PASSWORD:  123456
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-q6ggq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-q6ggq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-q6ggq
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason                  Age                     From               Message
  ----     ------                  ----                    ----               -------
  Normal   Scheduled               22m                     default-scheduler  Successfully assigned default/mysql-tscrh to node1
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "58d143373e767c40610587624c667d8e20dd77fa397952406a085f2ae1dc38e6" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "81d12726dbe075c64af48142638e98863231e8201ccc292f0dda1fccfa7fdaec" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "8bd77ee0176d369435ead7bd3c2675b7bbcbdc2b052cf34c784f04053a7d5288" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "0733fc209e085e96f5823a2280b012a609dace41fda967c5ae951005a8699ce6" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "91f337eb334612b2e7426d820ba4b15a9c9c549050517d459508832b69780b5f" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "48cbba9896068a774d9128a6864394e8726a0d857bae61036421ad73f5d6e3dd" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "d259778aa19418a9a429b3175b66afae3d8ffb7324ec9df492b2947dbc153460" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "3d01c00ac9eaac7b2e983402be38c1cf60c4bbd1d454aa45329ce0ca0c2bf792" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  22m                     kubelet, node1     Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "98879cbb0405dac3a24753dd1986070a53fe9ee9f1b819996a903c19286a2bb7" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  7m49s (x838 over 22m)   kubelet, node1     (combined from similar events): Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "60923335b41c2d0412b279bdb6150f9b9b7eae20c4a02549e35509083c01384b" network for pod "mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory
  Normal   SandboxChanged          2m49s (x1127 over 22m)  kubelet, node1     Pod sandbox changed, it will be killed and re-created.

会看到一个错误:

"mysql-tscrh": NetworkPlugin cni failed to set up pod "mysql-tscrh_default" network: open /run/flannel/subnet.env: no such file or directory

这是缺少/run/flannel/subnet.env文件

解决办法:配置flannel网络

分别在三台服务器上执行如下步骤

  1. 创建目录

    sudo mkdir -p /run/flannel
    
  2. 创建环境描述文件

    $ sudo tee /run/flannel/subnet.env <<-'EOF'
    FLANNEL_NETWORK=10.244.0.0/16
    FLANNEL_SUBNET=10.244.0.1/24
    FLANNEL_MTU=1450
    FLANNEL_IPMASQ=true
    EOF
    

6.1.5 查看mysql实例集群状态

itcast@master:~/working$ kubectl get pods
NAME          READY   STATUS              RESTARTS   AGE
mysql-tscrh   0/1     Running   0          17m

如果为Running状态,则为mysql集群启动成功。


关于作者:

作者:Aceld(刘丹冰)

号:IT无崖子

mail: [email protected]

github: https://github.com/aceld

原创书籍gitbook: http://legacy.gitbook.com/@aceld

原创声明:未经作者允许请勿转载, 如果转载请注明出处

你可能感兴趣的:(k8s(Kubernetes)快速部署捷径(6)-集群mysql应用实例)