k8s 学习(报错解决)

1.虚拟机无法telnet 30001端口

1)关闭防火墙

systemctl status firewalld

手动关闭

systemctl stop firewalld
systemctl disable firewalld

重启kubernaes相关服务

3)执行命令

iptables -P FORWARD ACCEPT

之后就可以telnet 30001端口了

2. 创建rc报错

如下:

Error creating: No API token found for service account "default", retry after the token is automatically created and added to the service account

解决:

修改/etc/kubernetes/apiserver

修改KUBE_ADMISSION_CONTROL为:

KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

3. 创建pod报错

报错信息如下:

Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

分析: 

无法下载国外镜像registry.access.redhat.com/rhel7/pod-infrastructure:latest

docker 搜索pod-infrastructure

docker search pod-infrastructure

docker pull docker.io/w564791/pod-infrastructure

修改/etc/kubernetes/kubelet的KUBELET_POD_INFRA_CONTAINER修改为

KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=docker.io/w564791/pod-infrastructure:latest"

4.访问报错

Error:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

关资源rc、pod、service、ep都创建成功,但是myweb的pods无法访问到mysql提供的数据库服务。

[root@server /data/tomcat]# kubectl get all
NAME       DESIRED   CURRENT   READY     AGE
rc/mysql   1         1         1         47m
rc/myweb   1         1         1         8m

NAME             CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
svc/kubernetes   10.254.0.1      <none>        443/TCP          5d
svc/mysql        10.254.190.45   <none>        3306/TCP         1d
svc/myweb        10.254.96.238   <nodes>       8080:30001/TCP   20h

NAME             READY     STATUS    RESTARTS   AGE
po/mysql-cc20c   1/1       Running   1          47m
po/myweb-vg6hk   1/1       Running   0          8m

分析

查看源代码
既然无法建立连接,那先看下是如何建立连接的。登录到myweb的docker容器里面,查看index.jsp文件,主要内容如下:

java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.Statement stmt=null;
java.sql.ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
try{
      Class.forName("com.mysql.jdbc.Driver");
       String ip=System.getenv("MYSQL_SERVICE_HOST");
       String port=System.getenv("MYSQL_SERVICE_PORT");
       ip=(ip==null)?"localhost":ip;
       port=(port==null)?"3306":port;
      System.out.println("Connecting to database...");

      System.out.println("jdbc:mysql://"+ip+":"+port+"?useUnicode=true&characterEncoding=UTF-8");
      conn = java.sql.DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"?useUnicode=true&characterEncoding=UTF-8", "root","123456");

      stmt = conn.createStatement();
}catch(Exception ex){
  ...
}

就是用jsp创建了一个连接,连接的地址通过ENV方式注入。即在myweb-rc.yaml中配置的MYSQL_SERVICE_HOSTMYSQL_SERVICE_PORT环境变量指定。
登陆myweb应用查看这两个环境变量是否有问题:

[root@server /data/tomcat]# kubectl exec -it myweb-r7cft -- /bin/bash
root@myweb-r7cft:/usr/local/tomcat# ls 
LICENSE  RELEASE-NOTES	bin   include  logs  webapps
NOTICE	 RUNNING.txt	conf  lib      temp  work
root@myweb-r7cft:/usr/local/tomcat# echo $MYSQL_SERVICE_HOST
mysql
root@myweb-r7cft:/usr/local/tomcat# echo $MYSQL_SERVICE_PORT
3306
root@myweb-r7cft:/usr/local/tomcat# exit
exit

查看mysql容器的IP

[root@server ~]# kubectl describe pod mysql-cc20c
Name:		mysql-cc20c
Namespace:	default
Node:		127.0.0.1/127.0.0.1
Start Time:	Tue, 17 Sep 2019 14:57:14 +0800
Labels:		app=mysql
Status:		Running
IP:		172.17.0.2
Controllers:	ReplicationController/mysql
Containers:
  mysql:
    Container ID:	docker://70332b78a6ca3a25bf0c4410d5b83416fea4b02ba891391b77edb89695b91921
    Image:		mysql:5.7
    Image ID:		docker-pullable://docker.io/mysql@sha256:f7985e36c668bb862a0e506f4ef9acdd1254cdf690469816f99633898895f7fa
    Port:		3306/TCP
    State:		Running
      Started:		Tue, 17 Sep 2019 15:14:07 +0800
    Last State:		Terminated
      Reason:		Completed
      Exit Code:	0
      Started:		Tue, 17 Sep 2019 14:57:39 +0800
      Finished:		Tue, 17 Sep 2019 15:14:06 +0800
    Ready:		True
    Restart Count:	1
    Volume Mounts:	<none>
    Environment Variables:
      MYSQL_ROOT_PASSWORD:	123456
Conditions:
  Type		Status
  Initialized 	True 
  Ready 	True 
  PodScheduled 	True 
No volumes.
QoS Class:	BestEffort
Tolerations:	<none>
No events.

解决方法

修改myweb-rc定义的MYSQL_SERVICE_HOST,修搞成mysql的ip

kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: kubeguide/tomcat-app:v1
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: '172.17.0.2'
          - name: MYSQL_SERVICE_PORT
            value: '3306'

问题解决!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值