Search code examples
dockerhadoopweave

ssh: connect to host e121a0ef81ef(container id) port 22: Connection refused in docker


I have three hosts with docker installed on each of them. I want to have a distributed file system,HDFS, among three container. So, I have to make a hadoop cluster. I use this docker file to make a hadoop image.

FROM ubuntu_mesos
ENV HADOOP_HOME /opt/hadoop
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get update && apt-get install -y ssh rsync vim openjdk-8-jdk
# download and extract hadoop, set JAVA_HOME in hadoop-env.sh, update 
path
RUN wget https://archive.apache.org/dist/hadoop/core/hadoop- 
3.1.0/hadoop-3.1.0.tar.gz && tar -xzf hadoop-3.1.0.tar.gz && \
mv hadoop-3.1.0 $HADOOP_HOME && \
echo "export JAVA_HOME=$JAVA_HOME" >> $HADOOP_HOME/etc/hadoop/
hadoop-env.sh && \
echo "PATH=$PATH:$HADOOP_HOME/bin" >> ~/.bashrc
# create ssh keys
RUN ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa && cat ~/.ssh/id_rsa.pub 
>> ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys
ADD core-site.xml $HADOOP_HOME/etc/hadoop/
ADD hdfs-site.xml $HADOOP_HOME/etc/hadoop/
ADD mapred-site.xml $HADOOP_HOME/etc/hadoop/
ADD mapred-site.xml $HADOOP_HOME/etc/hadoop/
ADD ssh_config /root/.ssh/config
COPY start-hadoop.sh start-hadoop.sh
EXPOSE 22 9000 8088 50070 50075 50030 50060
RUN echo export HDFS_NAMENODE_USER="root" >> 
$HADOOP_HOME/etc/hadoop/hadoop-env.sh 
RUN echo export HDFS_DATANODE_USER="root" >> 
$HADOOP_HOME/etc/hadoop/hadoop-env.sh
RUN echo export HDFS_SECONDARYNAMENODE_USER="root" >> 
$HADOOP_HOME/etc/hadoop/hadoop-env.sh
RUN echo export YARN_RESOURCEMANAGER_USER="root" >> 
$HADOOP_HOME/etc/hadoop/hadoop-env.sh
RUN echo export YARN_NODEMANAGER_USER="root" >> 
$HADOOP_HOME/etc/hadoop/hadoop-env.sh

After building the docker file. I sat up docker swarm on thee hosts. I installed weave net and made a container network like this:

docker plugin install weaveworks/net-plugin:latest_release
docker network create --driver=weaveworks/net-plugin:latest_release 
--attachable mycontainernetwork

I connected three hosts with running this command on each host:

sudo weave launch <IP address of first host> <IP address of second 
host>

Then,I ran hadoop image with container network on each host like this:

 sudo docker run -it --net mycontainernetwork my-hadoop

I checked that each container could recognize other containers with running this command on each container:

 ping -c 1 -q ContainerID

But, when I want to run this command:

 ssh e121a0ef81ef

I receive this error:

ssh: connect to host e121a0ef81ef port 22: Connection refused

"e121a0ef81ef" is my container ID.

I am confused and do not know how to solve the problem. Would you please help me?

Any help would be appreciated.


Solution

  • Problem solved. I did these stages: First, I made ssh passwordless among three hosts. In three host:

     ssh-keygen
    

    Then, for each slave, I ran this command in Master:

     ssh-copy-id user@ip
    

    I tested that ssh works without password between Master and slaves. Therefore, I copied "~/.ssh" folder from host to container in each host. By the way, I entered to the container with this command:

    sudo docker run -it -v /home/user/.ssh:/root/.ssh --net mycontainernetwork
     --privileged my-hadoop
    

    I debugged ssh to understand the problem:

    ssh -v user@ip
    

    The most important points are that after entering to the container, you have to restart ssh with this command:

      /etc/init.d/ssh restart 
    

    or

      service ssh restart
    

    Also, you must give privilege to .ssh folder with this command:

    chmod 600 ~/.ssh/*
    

    After that I can run ssh command to remote container.

    Hope it was helpful for others.