k8s 權威教程第一章(報錯解決)

1.虛擬機無法telnet 30001端口

1)關閉防火牆

systemctl status firewalld

手動關閉

systemctl stop firewalld
systemctl disable firewalld

重啓kubernaes相關服務

3)執行命令

iptables -P FORWARD ACCEPT

之後就可以telnet 30001端口了

2.訪問報錯

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'

問題解決!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章