k8s实例(MySQL实例,tomcat实例)

MySQL实例

定义描述文件

apiVersion: v1
kind: ReplicationController                            #副本控制器RC
metadata:
  name: mysql                                          #RC的名称,全局唯一
spec:
  replicas: 2                                         #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"

创建RC

创建mysql-rc.yaml完成后,在master节点使用kubectl命令发布到K8S集群中

kubectl create -f mysql-re.yaml

查看启动状态

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

NAME          READY   STATUS    RESTARTS   AGE
mysql-s952r   1/1     Running   1          22h
mysql-zlmwg   1/1     Running   1          22h

网络异常解决方案(未出现问题可以跳过,你懂我的意思把)

出现了ContainerCreating状态,并且长时间没有创建成功
通过kubectl describe pods mysql查看pods详细状态

Name:               mysql-tscrh
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node1/192.168.236.178
Start Time:         Mon, 17 Jun 2019 09:10:35 +0000
Labels:             app=mysql
Annotations:        <none>
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:  <none>
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.

根据报错信息可以判断出是flannel网络插件的问题

解决办法:配置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
    

在此运行就没有报错了

tomcat实例

定义描述文件

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 5                                       #Pod副本期待数量为5
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: docker.io/kubeguide/tomcat-app:v1
        ports: 
        - containerPort: 8080
        env:
        - name: MYSQL_SERVICE_HOST
          value: "mysql"
        - name: MYSQL_SERVICE_PORT
          value: "3306"

加载RC副本描述文件

$ kubectl create -f myweb-rc.yaml
replicationcontroller/myweb created
$ kubectl get rc
NAME    DESIRED   CURRENT   READY   AGE
mysql   1         1         0       4m22s
myweb   5         5         0       7s
$ kubectl get pods
NAME          READY   STATUS              RESTARTS   AGE
mysql-tp69s   1/1     Running             0          21s
myweb-266mz   0/1     ContainerCreating   0          2s
myweb-j7nl7   0/1     ContainerCreating   0          2s
myweb-s7drm   0/1     ContainerCreating   0          2s
myweb-sq4ns   0/1     ContainerCreating   0          2s
myweb-t4vlf   0/1     ContainerCreating   0          2s
$ 

注意mysql实例 状态 Running

myweb实例状态 ContainerCreating

过几分钟myweb实例状态变成 Running

创建服务副本

在master服务器

  1. 使用root用户登录,并切换到working目录 cd /root/working
  2. 创建描述服务描述文件myweb-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb 

部署服务

$kubectl create -f myweb-svc.yaml

验证

$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
mysql-tp69s   1/1     Running   0          15m
myweb-266mz   1/1     Running   0          14m
myweb-j7nl7   1/1     Running   0          14m
myweb-s7drm   1/1     Running   0          14m
myweb-sq4ns   1/1     Running   0          14m
myweb-t4vlf   1/1     Running   0          14m
itcast@master:~/working$ kubectl get service     
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          19h
myweb        NodePort    10.109.234.197   <none>        8080:30001/TCP   25m

已经看到已经有一个myweb服务已经启动

itcast@master:~/working$ kubectl describe service myweb
Name:                     myweb
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=myweb
Type:                     NodePort
IP:                       10.109.234.197
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30001/TCP
Endpoints:                10.244.0.6:8080,10.244.0.6:8080,10.244.0.7:8080 + 2 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

service详情这里的IP就是CLUSTER-IP. CLUSTER-IP是和service绑定的。

service详情这里的Port就是Service的端口号。

service详情这里的NodePort就是Node的真实端口号。

service详情这里的Endpoints就是容器的IP和port。

验证端口号

$ netstat -tlp|grep 30001
tcp6       0      0 [::]:30001              [::]:*                  LISTEN      4333/kube-proxy
$

我们可以打开浏览器,输入master/node1/node2任何一个地址家30001端口都可以,访问tomcat服务。

http://192.168.80.144:30001

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值