Ubuntu初始化
sudo apt-get install openssh-server
sudo vim /etc/ssh/sshd_config
sudo systemctl restart sshd
设置静态IP
cd /etc/netplan
···
network:
version: 2
renderer: NetworkManager
ethernets:
ens33: # 网卡名称
dhcp4: no # 关闭dhcp
dhcp6: no
addresses: [192.168.0.108/24] # 静态ip
gateway4: 192.168.0.1 # 网关
nameservers:
addresses: [8.8.8.8, 114.114.114.114] #dns
···
sudo netplan apply
docker安装
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
#切换镜像源
sudo vim /etc/docker/daemon.json
···
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
···
sudo systemctl restart docker
#Docker中国官方镜像源
https://registry.docker-cn.com
#网易镜像源
http://hub-mirror.c.163.com
#中国科技大学镜像源
https://docker.mirrors.ustc.edu.cn
docker-compose国内下载(建议手动下载)
# 国内下载https://gitee.com/smilezgy/compose/releases/download/v2.20.2/docker-compose-linux-x86_64
# 手动上传到/usr/local/bin 目录下
sudo cp docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod -x /usr/local/bin/docker-compose
sudo docker-compose -v
安装jdk
#第一步:上传或下载安装包
cd /usr/local
jdk-8u152-linux-x64.tar.gz
#第二步:解压安装包
tar -zxvf jdk-8u152-linux-x64.tar.gz
#第三步:建立软连接
ln -s /usr/local/jdk1.8.0_152/ /usr/local/jdk
#第四步:修改环境变量
vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
#通过命令source /etc/profile让profile文件立即生效
source /etc/profile
安装maven
#第一步:上传或下载安装包
cd /usr/local
apache-maven-3.6.1-bin.tar.gz
#第二步:解压安装包
tar -zxvf apache-maven-3.6.1-bin.tar.gz
#第三步:建立软连接
ln -s /usr/local/apache-maven-3.6.1/ /usr/local/maven
#第四步:修改环境变量
vim /etc/profile
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:$MAVEN_HOME/bin
#通过命令source /etc/profile让profile文件立即生效
source /etc/profile
jenkins安装
sudo docker pull jenkins/jenkins:lts
sudo mkdir /docker
sudo mkdir /docker/jenkins_home
sudo chown -R 1000 /docker/jenkins_home/
#root该用户为启动Jenkins时的所属用户,y
sudo usermod -aG docker root
sudo docker restart jenkins
sudo chown root:docker /var/run/docker.sock
sudo chmod 666 /var/run/docker.sock
sudo docker run -di --name=jenkins -p 8080:8080 -p 9000:9000 --privileged --net=host -v /docker/jenkins_home/:/var/jenkins_home -v /usr/local/jdk:/usr/local/jdk -v /usr/local/maven:/usr/local/maven:rw -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker jenkins/jenkins:lts
cd /docker/jenkins_home/updates/
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
sudo docker rm jenkins -f
docker run -di --name=jenkins -p 8080:8080 -p 9000:9000 -v /docker/jenkins_home/:/var/jenkins_home -v /usr/local/jdk:/usr/local/jdk -v /usr/local/maven:/usr/local/maven:rw -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker jenkins/jenkins:lts
生成Jenkins容器公钥
docker exec -u root -it jenkins apt-get update
docker exec -u root -it jenkins apt-get install -y openssh-client
docker exec -u root -it jenkins bash -c "apt-get update && apt-get install -y rsync"
docker exec -u root -it jenkins ssh-keygen -t rsa
#容器内访问公钥
/var/jenkins_home/.ssh/id_rsa
#将公钥保存到远程服务器用户,~指当前用户,若是root便为:vim /root/.ssh/authorized_keys
~/.ssh/authorized_keys
jenkins构建自身脚本
#!/bin/bash
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export MAVEN_HOME=/usr/local/maven
export PATH=$MAVEN_HOME/bin:$PATH
mvn clean package
echo 'package ok!'
echo 'build start!'
project_dir=./service/service-product/
remote_dir=/opt/springboot/zx_vanx/service-product
remote_ip=101.43.154.175
remote_username=root
ssh -i /var/jenkins_home/.ssh/id_rsa $remote_username@$remote_ip "mkdir -p $remote_dir"
rsync -avz -e "ssh -i /var/jenkins_home/.ssh/id_rsa" $project_dir root@$remote_ip:$remote_dir
jenkins构建远程服务器脚本
remote_dir=/opt/springboot/zx_vanx/service-product
service_name="service-product"
service_port=8206
cd $remote_dir
#查看容器id
CID=$(docker ps | grep "$service_name" | awk '{print $1}')
echo "CID $CID"
if [ -n "$CID" ]
then
echo "exist $service_name container,CID=$CID"
#停止
docker stop $CID
#删除容器
docker rm $CID
else
echo "no exist $service_name container"
fi
#查看镜像id
IID=$(docker images | grep "$service_name" | awk '{print $3}')
echo "IID $IID"
if [ -n "$IID" ]
then
echo "exist $service_name image,IID=$IID"
#删除镜像
docker rmi -f $IID
echo "delete $service_name image"
fi
#构建
docker build -t $service_name .
echo "build $service_name image"
#启动
docker run -d --name $service_name --net=host -p $service_port:$service_port $service_name