<p>Step1:finish installing etcd and kubernetes with YUM in CentOS7 and shutdown firewall</p>

<p>Step2:modify related configuration item in /etc/sysconfig/docker</p>

<blockquote>
  <p>OPTIONS='--selinux-enabled=false --insecure-registry gcr.io'</p>
</blockquote>

<p>Step3:modify related configuration item in /etc/kubernetes/apiserver</p>

<p>remove </p>

<blockquote>
  <p>ServiceAccount</p>
</blockquote>

<p>in KUBE_ADMISSION_CONTROL configuration item</p>

<p>Step4:start all the related services of etcd and kubernetes</p>

<p>Step5:start ReplicationController for mysql db</p>

<blockquote>
  <p>kubectl create -f mysql-rc.yaml</p>
</blockquote>

<pre><code>apiVersion: v1
kind: ReplicationController  
metadata:
  name: mysql
spec:
  replicas: 1 
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: hub.c.163.com/library/mysql
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
</code></pre>

<p>Step6:start related mysql db service</p>

<blockquote>
  <p>kubectl create -f mysql-svc.yaml</p>
</blockquote>

<pre><code>kind: Service   
metadata: 
  name: mysql 
spec:
  ports:
    - port: 3306
  selector:     
    app: mysql
</code></pre>

<p>Step7:start ReplicationController for myweb</p>

<blockquote>
  <p>kubectl create -f myweb-rc.yaml</p>
</blockquote>

<pre><code>apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 3  
  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"
</code></pre>

<p>Step8:start related tomcat service</p>

<blockquote>
  <p>kubectl create -f myweb-svc.yaml</p>
</blockquote>

<pre><code>apiVersion: v1
kind: Service
metadata: 
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb
</code></pre>

<p>When I visit from browser with nodeport(30001),I get the following Exception:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<hr>

<blockquote>
  <p>kubectl get ep</p>
</blockquote>

<pre><code>NAME         ENDPOINTS                                         AGE

kubernetes   192.168.57.129:6443                               1d

mysql        172.17.0.2:3306                                   1d

myweb        172.17.0.3:8080,172.17.0.4:8080,172.17.0.5:8080   1d
</code></pre>

<blockquote>
  <p>kubectl get svc</p>
</blockquote>

<pre><code>NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE

kubernetes   10.254.0.1     &lt;none&gt;        443/TCP          1d

mysql        10.254.0.5     &lt;none&gt;        3306/TCP         1d

myweb        10.254.220.2   &lt;nodes&gt;       8080:30001/TCP   1d
</code></pre>

<p>From the interior of any tomcat container I can see the mysql env and the related mysql link code in JSP is as below:</p>

<pre><code>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...");

conn = java.sql.DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"?useUnicode=true&amp;characterEncoding=UTF-8", "root","123456");
</code></pre>

<p>[root@promote ~]# docker exec -it 1470cfaa1b1c /bin/bash</p>

<p>root@myweb-xswfb:/usr/local/tomcat# env |grep MYSQL_SERVICE </p>

<p>MYSQL_SERVICE_PORT=3306</p>

<p>MYSQL_SERVICE_HOST=mysql</p>

<p>root@myweb-xswfb:/usr/local/tomcat# ping mysql</p>

<p>ping: unknown host</p>

<p><strong>Can someone tell me why I could not ping mysqldb hostname from inner tomcat server?Or how to locate the problem further?</strong></p>

<p><a href="https://i.stack.imgur.com/miZ1h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/miZ1h.png" alt="enter image description here"></a></p>

<p>I know the reason, it's the DNS problems. The web server cannot find the IP address of the mysql server. so it failed. Temp solution is change the web server's IP to the mysql db server. Hope can help you. Thank you. </p>

<p><a href="https://i.stack.imgur.com/IWOkt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IWOkt.png" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/26NSX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/26NSX.png" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/zEQ1V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zEQ1V.png" alt="enter image description here"></a></p>
