騰訊雲下使用docker一步步虛擬出可運行的線上環境

1 騰訊雲磁盤掛載和格式化

Linux下磁盤掛載(騰訊雲掛載方式)
https://www.cnblogs.com/stulzq/p/7610100.html

格式化分區:
https://blog.csdn.net/nahancy/article/details/52201121

最終掛載磁盤信息
在這裏插入圖片描述
其中磁盤大小:

/dev/vdb1 /data/disk-one/        1024G
/dev/vdd1 /data/disk-two/        664G
/dev/vdc1 /data/disk-three/      1024G

爲了機器重啓之後能夠讓掛載的機器正常運行,需要在[root@VM_0_8_centos ~]# vim /etc/rc.local中添加如下配置文件,讓機器啓動的時候自動掛載

mount /dev/vdb1 /data/disk-one/
mount /dev/vdd1 /data/disk-two/
mount /dev/vdc1 /data/disk-three/

在這裏插入圖片描述

2 防火牆配置

新建安全組
在這裏插入圖片描述
添加所有指定端口能夠被訪問
在這裏插入圖片描述

3 使用Docker虛擬化出一個Centos7操作系統

Docker官網:https://docs.docker.com/
Docker文檔:https://docs.docker.com/engine/reference/commandline/network_rm/

3.1 Docker安裝

3.1.1 支持的操作系統

Docker支持以下的CentOS版本:
CentOS 7 (64-bit)
CentOS 6.5 (64-bit) 或更高的版本

3.1.2 前提條件

目前,CentOS 僅發行版本中的內核支持 Docker。
Docker 運行在 CentOS 7 上,要求系統爲64位、系統內核版本爲 3.10 以上。
Docker 運行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系統爲64位、系統內核版本爲 2.6.32-431 或者更高版本。

Docker要求CentOS系統的內核版本高於3.10,查看CentOS版本是否支持Docker.
通過uname –r命令查看你當前的內核版本

[root@bigdata1 ~]# uname -r
3.10.0-693.el7.x86_64

上面的Linux內核的版本大於3.10版本,可以進行安裝。

3.1.3 使用yum安裝(CentOS7下)

安裝Docker
Docker軟件包和依賴包已經包含在默認的CentOS-Extras軟件源裏,安裝命令如下:

[root@runoob ~]# yum -y install docker-io

測試運行hello-world

[root@runoob ~]#docker run hello-world

由於本地沒有hello-world這個鏡像,所以會下載一個hello-world的鏡像,並在容器內運行。

3.1.4 使用腳本安裝Docker(Centos7下)

1.使用sudo或root權限登錄Centos.
2.確保yum包更新到最新

sudo yum update

3.執行Docker安裝腳本

$ curl -fsSL https://get.docker.com/ | sh

執行這個腳本會添加docker.repo源並安裝Docker.
4.啓動Docker進程。

$ sudo service docker start

5.驗證docker是否安裝成功並在容器中執行一個測試的鏡像。

$ sudo docker run hello-world
docker ps

到此,docker在CentOS系統的安裝完成。

3.1.5 使用Centos7.5.1804 docker鏡像虛擬一個操作系統

3.1.5.1 運行一個centos7鏡像

以下是https://hub.docker.com/_/centos/給出的方式,以下不是最終運行方式
docker run --rm -d -i -t centos:centos7.5.1804 /bin/bash
注意:要加上-d,如果不加,這個當退出的時候,發現沒有docker鏡像了。

3.1.5.2 查看Linux下運行了哪些docker鏡像
[root@bigdata1 ~]# docker ps
CONTAINER ID        IMAGE                   COMMAND               CREATED             STATUS              PORTS                   NAMES
46ba1c2535fc        centos:centos7.5.1804   "/bin/bash"           6 seconds ago       Up 5 seconds                                naughty_euler
daaa23c63483        centos:centos7.5.1804   "/bin/bash"           2 minutes ago       Up 2 minutes                                keen_golick
fbb65c980d65        sshd:Dockerfile         "/usr/sbin/sshd -D"   About an hour ago   Up About an hour    0.0.0.0:10022->22/tcp   mycentos2
[root@bigdata1 ~]#
3.1.5.3 進入運行的Linux操作系統
第一步中使用的後臺靜默的方式運行,通過docker attach <CONTAINER ID >訪問該容器
[root@bigdata1 ~]# docker attach 46ba1c2535fc
[root@46ba1c2535fc /]#
3.1.5.4 ifconfig的安裝

進入Centos之後,發現沒有ifconfig,接下來配置ifconfig

yum search ifconfig
yum install net-tools.x86_64

上面運行完之後,可以執行ifconfig進行查看ip了

3.1.5.5 安裝wget

在很多時候,需要通過wget下載資源

[root@9b291665f9af /]# yum -y install wget
3.1.5.6 更新安裝vim,安裝vim增強包
[root@5ec004281336 /]#yum -y install vim-enhanced
3.1.5.7 修改docker容器虛擬出來的Linux系統的用戶名和密碼
passwd root

然後按照提示進行密碼的修改,密碼改成123456

3.1.5.8 安裝sshd

[root@b5926410fe60 /]# yum install passwd openssl openssh-server -y

啓動sshd(這步驟可以跳過):

# /usr/sbin/sshd -D

這時報以下錯誤:

[root@ b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key

執行以下命令解決:

ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''  
ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N '' 

然後,修改 /etc/ssh/sshd_config 配置信息:
爲了讓服務能夠被遠程工具連接,配置/etc/ssh/sshd_config中監聽的端口和ip地址

vim /etc/ssh/sshd_config

Port 22 (這個可以不設置,默認22端口)
PermitRootLogin yes
PasswordAuthentication yes  (這裏發現有兩處,一處註釋了,一處沒有註釋,這裏不改)

vi /etc/ssh/sshd_config(或下面的命令)
sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config

修改完後,重新啓動sshd

[root@b5926410fe60 /]# /usr/sbin/sshd -D

3.1.5.9 解決docker容器時間與本地時間不一致

進入容器(也可以xshell遠程進入)

docker exec -t -i b5926410fe60 /bin/bash    // b5926410fe60爲容器id

在/usr/share/zoneinfo目錄下找上海時區

cd  /usr/share/zoneinfo/Asia

3)複製上海時區到 /etc 重命名localtime文件

cp -i Shanghai /etc/localtime

完成後的效果:
在這裏插入圖片描述

程序運行過程中,docker容器中的時間比實際容器中的時間慢8小時,解決辦法是在容器中執行:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

3.1.5.10 將當前容器保存爲鏡像

要注意的是,要在宿主機器上另外開啓一個終端,然後在執行下面的命令,或者使用以下命令:
#退出,但不停止容器 (筆者電腦上執行,發現下面的命令不給力,建議另外開一個終端替代下面的快捷鍵)

Ctrl+P+Q

[root@bigdata2 ~]# docker ps -all
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
9a1e3ceb3768       centos:centos7.5.1804   "/bin/bash"         12 minutes ago      Up 12 minutes                           hopeful_hypatia

使用docker commit <CONTAINER ID> REPOSITORY
[root@bigdata2 ~]# docker commit 9a1e3ceb3768 centos7.5.1804.v0.1
sha256:bbb85bb5c0cdefb070162c9791cbec85942c4e25ac4714b19da060ecf91ab03e
[root@bigdata2 ~]# 

docker images查看是否有新的docker鏡像
[root@bigdata1 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
centos/centos7.5.1804.v0.1   latest              1fef90b99396        33 seconds ago      358 MB
docker.io/centos             centos7.5.1804      76d6bc25b8a5        3 weeks ago         200 MB
[root@VM_0_8_centos ~]# 
[root@bigdata1 ~]#

4 docker網絡創建

安裝brctl命令包

yum install bridge-utils

刪除舊的網絡

[root@VM_0_8_centos test]# ifconfig docker0 down
[root@VM_0_8_centos test]# brctl delbr docker0

[root@VM_0_8_centos test]# service docker restart

由於騰訊雲本身的網絡ip地址是172.17.0.0/16段的,所有這裏不能再配置成17段,這裏我配置成了19段

docker network create --subnet=172.19.0.0/16 zjnet

如果創建過程中出現:

Error response from daemon: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:  (iptables failed: iptables --wait -t nat -I DOCKER -i br-3559d3701627 -j RETURN: iptables: No chain/target/match by that name.
 (exit status 1))
解決辦法是重啓docker網絡,然後再創建即可
[root@VM_0_8_centos ~]# service docker restart

5 Centos7基於lxcfs增強Docker隔離能力(docker free顯示問題)

參考博文:https://blog.csdn.net/shida_csdn/article/details/79196258
在宿主操作系統爲Centos7上安裝lxcfs軟件

wget https://copr-be.cloud.fedoraproject.org/results/ganto/lxd/epel-7-x86_64/00486278-lxcfs/lxcfs-2.0.5-3.el7.centos.x86_64.rpm yum install -y lxcfs-2.0.5-3.el7.centos.x86_64.rpm
  lxcfs-2.0.5-3.el7.centos.x86_64.rpm(下載地址:https://copr-be.cloud.fedoraproject.org/results/ganto/lxd/epel-7-x86_64/00486278-lxcfs/)

將文件放到:/root目錄下
lxcfs-2.0.5-3.el7.centos.x86_64.rpm
在這裏插入圖片描述
在這裏插入圖片描述

啓動lxcfs

systemctl start lxcfs

或者直接執行啓動命令:

lxcfs /var/lib/lxcfs &

配置容器啓動參數
-v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw

-v /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw

 -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw

-v /var/lib/lxcfs/proc/stat:/proc/stat:rw

-v /var/lib/lxcfs/proc/swaps:/proc/swaps:rw

-v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw

只要容器啓動時映射了宿主機的這些文件,即可修正 free、top等命令的錯誤顯示

6 用新鏡像在宿主機上創建新的容器

--先查看有哪些鏡像
[root@VM_0_8_centos lost+found]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
centos/centos7.5.1804.v0.1   latest              1fef90b99396        About an hour ago   358 MB
docker.io/centos             centos7.5.1804      76d6bc25b8a5        3 weeks ago   

以鏡像centos/centos7.5.1804.v0.1進行安裝
例:
在這裏插入圖片描述
其中命令爲(下面使用了8個CPU核心):

docker run -itd -m 40g --memory-swap=40g --cpus=8 --cpuset-cpus="0,1,2,3,20,21,22,23" --restart=always --name bigdata2 -v /data/disk-three/lost+found/bigdata2:/data  -v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw -v /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw -v /var/lib/lxcfs/proc/stat:/proc/stat:rw -v /var/lib/lxcfs/proc/swaps:/proc/swaps:rw -v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw -v /etc/localtime:/etc/localtime:ro --privileged=true --net zjnet --ip 172.19.0.2 -p 10022:22 centos7.5.1804.v0.2:latest /usr/sbin/sshd -D

7 docker 容器中設置中文語言包的問題

參考博文:https://blog.csdn.net/hnmpf/article/details/81478972

由於使用鏡像安裝的Centos7沒有中文語言包,所以在查看日誌等的時候發現是亂碼,爲了解決這個問題,需要安裝中文語言包。
進入虛擬的Linux操作系統之後,採用”locale”查看,發現沒有配置語言LANG
在這裏插入圖片描述
可以採用”locale -a”,查看系統語言包,會發現沒有中文包

[root@9f117c915dea software]# locale -a
C
POSIX
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
en_BW
en_BW.iso88591
en_BW.utf8
en_CA
en_CA.iso88591
en_CA.utf8
en_DK
en_DK.iso88591
en_DK.utf8
en_GB
en_GB.iso88591
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.iso88591
en_HK.utf8
en_IE
en_IE.iso88591
en_IE.iso885915@euro
en_IE.utf8
en_IE@euro
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ
en_NZ.iso88591
en_NZ.utf8
en_PH
en_PH.iso88591
en_PH.utf8
en_SG
en_SG.iso88591
en_SG.utf8
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
en_ZA
en_ZA.iso88591
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW
en_ZW.iso88591
en_ZW.utf8
[root@9f117c915dea software]

解決辦法:

1、yum -y install kde-l10n-Chinese 安裝語言包(針對centos 7)
2、yum -y reinstall glibc-common 更新gitbc 包(因爲該鏡像已閹割了該包的部分功能,所以需要更新)

3、localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 (設置系統語言包),執行完成之後,再查看語言,結果如下:
在這裏插入圖片描述
然後在/etc/profile中配置:

export LC_ALL=zh_CN.utf8

在這裏插入圖片描述
4、ENV LC_ALL zh_CN.UTF-8 通過設置環境變量的方式設置(可以採用直接修改/etc/locale.conf 文件來實現,不過需要reboot),這裏通過修改/etc/locale.conf的方式實現,最終修改的結果爲(經過上面步驟之後,經過驗證,此步不要也可以了):

LANG="zh_CN.utf8"     (其中,原始的配置是:LANG="en_US.UTF-8")

由於要reboot,這裏重啓docker容器的方式進行實現(以下命令在宿主機器上執行)。

[root@VM_0_8_centos ~]# systemctl restart docker

8、mysql安裝

騰訊雲上運行mysql

docker run -itd -m 2g --memory-swap=2g --name mysql -p 13306:3306 -v /data/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=fasdfr343asfrs --net zjnet --ip 172.19.0.7 mysql:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci

參數說明:

--name 容器名稱
-p 端口映射
-v 配置文件映射
-e MYSQL_ROOT_PASSWORD   設置root密碼
-d 守護進程模式運行
-character-set-server --collation-server 字符集設置

如果想調整mysql的內存

docker update -m 4g --memory-swap=4g mysql

8.1.1 修改root用戶名、密碼、授權遠程登錄

docker exec -it mysql bash 通過這種方式進入mysql這個容器

root@e05c4083b391:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> update user set authentication_string = PASSWORD('fasdfr343asfrs') where user = 'root';
中間略:
mysql> grant all privileges on *.* to 'root'@'%' identified by 'fasdfr343asfrs' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

至此,mysql安裝成功

設置騰訊雲安全組
在這裏插入圖片描述

9 整個過程重啓hadoop環境的腳本

hadoop

cd $HADOOP_HOME 
sbin/stop-all.sh 
cd $SPARK_HOME
sbin/stop-all.sh 
cd $HBASE_HOME 
bin/stop-hbase.sh
cd $KYLIN_HOME
bin/kylin.sh stop
kill -9 `ps -ef | grep RunJar | grep -v grep | awk '{print $2}'`
kill -9 `ps -ef | grep YarnChild | grep -v grep | awk '{print $2}'`
kill -9 `ps -ef | grep metastore | grep -v grep | awk '{print $2}'`
kill -9 `ps -ef | grep hiveserver2 | grep -v grep | awk '{print $2}'`
kill -9 `ps -ef | grep JobHistoryServer | grep -v grep | awk '{print $2}'`



cd $HADOOP_HOME 
sbin/start-all.sh 
cd $SPARK_HOME
sbin/start-all.sh 
cd $HBASE_HOME 
bin/start-hbase.sh


cd $HIVE_HOME/bin
nohup hive --service metastore &
nohup  hive --service hiveserver2 &
$HADOOP_HOME/sbin/mr-jobhistory-daemon.sh start historyserver

cd $KYLIN_HOME
bin/kylin.sh start

10 、給已經虛擬化好的容器添加端口映射,示例

iptables -t nat -A  DOCKER -p tcp --dport 10041 -j DNAT --to-destination 172.19.0.2:8088

iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp --source 172.19.0.2 --destination 172.19.0.2 --dport 8088

iptables -A DOCKER -j ACCEPT -p tcp --destination 172.19.0.2 --dport 8088
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章