233. 微服务架构之Jenkins老头容器化部署
文章目录1. 制作Jenkins镜像2. 准备共享存储3. 准备资源配置清单4. 应用资源配置清单4. 配置DNS解析5. 浏览器访问老头1. 制作Jenkins镜像Jenkins官网:https://jenkins.io/download/Jenkins镜像:https://hub.docker.com/r/jenkins/jenkins1.准备基础镜像[root@k8s7-200.host.c
文章目录
1. 制作Jenkins镜像
Jenkins官网:https://jenkins.io/download/
Jenkins镜像:https://hub.docker.com/r/jenkins/jenkins
1.准备基础镜像
[root@k8s7-200.host.com ~]# docker load -i jenkins-with_getdocker_v2.190.3.tar
[root@k8s7-200.host.com ~]# docker tag 0a3393cb519a harbor.od.com/public/jenkins:v2.190.3
[root@k8s7-200.host.com ~]# docker push harbor.od.com/public/jenkins:v2.190.3
2.自定义Dockerfile
-制作Jenkins的Docker镜像
-设置了容器启动时使用的用户为root
-设置容器内的时区为UTC+8
-加入了ssh私钥(拉取git代码的两种方式:基于http和基于ssh)
-加入登录harbor的config文件
-修改了ssh客户端的配置
-安装docker客户端
$/data/dockerfile/jenkins/Dockerfile
FROM harbor.od.com/public/jenkins:v2.190.3
USER root
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
echo 'Asia/Shanghai' >/etc/timezone
ADD id_rsa /root/.ssh/id_rsa
ADD config.json /root/.docker/config.json
ADD get-docker.sh /get-docker.sh
RUN echo " StrictHostKeyChecking no" >> /etc/ssh/sshd_config &&\
/get-docker.sh
这个Dockerfile里我们主要做了以下几件事
设置容器用户为root
设置容器内的时区
将ssh私钥加入(使用git拉代码时要用到,配对的公钥应配置在gitlab中)
加入了登录自建harbor仓库的config文件
修改了ssh客户端的
安装一个docker的客户端
$生成ssh密钥对
~]# ssh-keygen -t rsa -b 2048 -C "xxx@qq.com" -N "" -f /root/.ssh/id_rsa
$get-docker.sh
[root@k8s7-200.host.com /data/dockerfile/jenkins]# cp /root/.ssh/id_rsa .
[root@k8s7-200.host.com /data/dockerfile/jenkins]# cp /root/.docker/config.json .
[root@k8s7-200.host.com /data/dockerfile/jenkins]# curl -fsSl get.docker.com -o get-docker.sh
[root@k8s7-200.host.com /data/dockerfile/jenkins]# chmod +x get-docker.sh
# infra 是harbor的一个私有仓库,是infrastructure(基础设置)的缩写
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker build . -t harbor.od.com/infra/jenkins:v2.190.3
出现successfully之后,就推送至私有仓库
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker push harbor.od.com/infra/jenkins:v2.190.3
测试:
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker run --rm harbor.od.com/infra/jenkins:v2.190.3 ssh -i /root/.ssh/id_rsa -T git@gitee.com
2. 准备共享存储
NFS共享存储放在 k8s7-200 上,用于存储Jenkins持久化文件。所有Node和k8s7-200都需要安装
[root@k8s7-200.host.com /data/dockerfile/jenkins]# yum install nfs-utils -y
[root@k8s7-200.host.com /data/dockerfile/jenkins]# cat /etc/exports
/data/nfs-volume 10.4.7.0/24(rw,sync,no_root_squash)
[root@k8s7-200.host.com /data/dockerfile/jenkins]# mkdir /data/nfs-volume
[root@k8s7-200.host.com /data/k8s-yaml/jenkins]# mkdir /data/nfs-volume/jenkins_home
[root@k8s7-200.host.com /data/dockerfile/jenkins]# systemctl start nfs
[root@k8s7-200.host.com /data/dockerfile/jenkins]# systemctl enable nfs
3. 准备资源配置清单
[root@k8s7-200 k8s-yaml]# mkdir /data/k8s-yaml/jenkins && mkdir /data/nfs-volume/jenkins_home && cd /data/k8s-yaml/jenkins
1.deployment.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: jenkins
namespace: infra
labels:
name: jenkins
spec:
replicas: 1
selector:
matchLabels:
name: jenkins
template:
metadata:
labels:
app: jenkins
name: jenkins
spec:
volumes:
- name: data
nfs:
server: k8s7-200
path: /data/nfs-volume/jenkins_home
- name: docker
hostPath:
path: /run/docker.sock
type: ''
containers:
- name: jenkins
image: harbor.od.com/infra/jenkins:v2.190.3
imagePullPolicy: IfNotPresent #镜像拉取策略:https://blog.csdn.net/lanwp5302/article/details/87348132
ports:
- containerPort: 8080
protocol: TCP
env:
- name: JAVA_OPTS
value: -Xmx512m -Xms512m
volumeMounts:
- name: data
mountPath: /var/jenkins_home
- name: docker
mountPath: /run/docker.sock
imagePullSecrets:
- name: harbor #私有仓库名称
securityContext:
runAsUser: 0
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 0
revisionHistoryLimit: 7
progressDeadlineSeconds: 600
2.service.yaml
kind: Service
apiVersion: v1
metadata:
name: jenkins
namespace: infra
spec:
ports:
- protocol: TCP
port: 80 #集群网络端口,和ingress要匹配
targetPort: 8080 #容器的端口
selector:
app: jenkins
3.ingress.yaml
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: jenkins
namespace: infra
spec:
rules:
- host: jenkins.od.com
http:
paths:
- path: /
backend:
serviceName: jenkins
servicePort: 80
4. 应用资源配置清单
# kubectl create ns infra
# kubectl create secret docker-registry harbor --docker-server=harbor.od.com --docker-username=admin --docker-password=Harbor12345 -n infra
[root@k8s7-21.host.com ~]# kubectl apply -f http://k8s-yaml.od.com/jenkins/deployment.yaml
deployment.extensions/jenkins created
[root@k8s7-21.host.com ~]# kubectl apply -f http://k8s-yaml.od.com/jenkins/service.yaml
service/jenkins created
[root@k8s7-21.host.com ~]# kubectl apply -f http://k8s-yaml.od.com/jenkins/ingress.yaml
ingress.extensions/jenkins created
4. 配置DNS解析
[root@k8s7-11.host.name ~]# tail /var/named/od.com.zone
$TTL 60 ; 1 minute
dns A 10.4.7.11
harbor A 10.4.7.200
k8s-yaml A 10.4.7.200
traefik A 10.4.7.10
dashboard A 10.4.7.10
zk1 A 10.4.7.11
zk2 A 10.4.7.12
zk3 A 10.4.7.21
jenkins A 10.4.7.10
[root@k8s7-11.host.name ~]# dig -t A jenkins.od.com @10.4.7.11 +short
10.4.7.10
5. 浏览器访问老头
管理员密码
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# cat secrets/initialAdminPassword
18b6da491e754470991fd6aec03979xx
配置安全策略
配置插件加速地址:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
修改下载地址
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home/updates]# sed -i 's#http://updates.jenkins-ci.org/download#https://mirrors.tuna.tsinghua.edu.cn/jenkins#g;s#http://www.google.com#https://www.baidu.com#g' default.json
插件必须要安装好。。。。。。。
6. 检查jenkins环境
7. 二进制部署maven
Maven官方地址:http://maven.apache.org/docs/history.html
Maven是提供给Jenkins使用,需要放到Jenkins的持久化目录中,直接将二进制包形式的Maven拷贝到Jenkins目录最方便。因此本次安装直接在 k8s7-200 操作。
在公司中,不同的项目对编译的JDK版本和Maven可能不同,可能需要多个版本的JDK和Maven组合使用,因此Maven目录名称就使用 maven- m a v e n v e r s i n − {maven_versin}- mavenversin−{jdk_version}格式。Maven的 bin/mvn 文件中可以定义 JAVA_HOME环境变量的值,不同的Maven可以使用不同的 JAVA_HOME 值。
场景一
当Maven需求的jdk版本和jenkins一致时,不需要定义 bin/mvn 中JAVA_HOME。
[root@k8s7-21 ~]# kubectl exec jenkins-54b8469cf9-v8g28 -n infra -- java -version # 查看jenkins中jdk版本
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
[root@k8s7-200 src]# wget https://archive.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.2-bin.tar.gz
[root@k8s7-200.host.com /opt/src]# mkdir -p /data/nfs-volume/jenkins_home/maven-3.6.1-8u232
[root@k8s7-200.host.com /opt/src]# tar xf apache-maven-3.6.1-bin.tar.gz -C /data/nfs-volume/jenkins_home/maven-3.6.1-8u232/
[root@k8s7-200.host.com /opt/src]# cd /data/nfs-volume/jenkins_home/maven-3.6.1-8u232/
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home/maven-3.6.1-8u232]# mv apache-maven-3.6.1/* .
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home/maven-3.6.1-8u232]# rm -rf apache-maven-3.6.1/
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home/maven-3.6.1-8u232]# pwd
/data/nfs-volume/jenkins_home/maven-3.6.1-8u232
# settings.xml 中 <mirrors></mirrors>标签中添加国内源
[root@k8s7-200 ~]# vim /data/nfs-volume/jenkins_home/maven-3.6.1-8u232/conf/settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
场景二
当Maven需要 oracle jdk-8u241 时:
[root@k8s7-200 src]# mkdir /data/nfs-volume/jenkins_home/jdk_versions
[root@k8s7-200 src]# tar -xf jdk-8u241-linux-x64.tar.gz -C /data/nfs-volume/jenkins_home/jdk_versions/
[root@k8s7-200 src]# cp -r /data/nfs-volume/jenkins_home/maven-3.6.1-8u232 /data/nfs-volume/jenkins_home/maven-3.6.2-8u241
[root@k8s7-200 src]# vim /data/nfs-volume/jenkins_home/maven-3.6.1-8u241/bin/mvn # 使用jenkins中绝对路径
JAVA_HOME='/var/jenkins_home/jdk_versions/jdk1.8.0_241'
8. 制作dubbo微服务的底包镜像
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker load -i jenkins-v2.176.2-with-docker.tar
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker image tag a25e4f7b2896 harbor.od.com/public/jenkins:v2.176.2
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker push harbor.od.com/public/jenkins:v2.176.2
[root@k8s7-200.host.com /data/dockerfile/jenkins]# docker build . -t harbor.od.com/infra/jenkins:v2.176.2
Sending build context to Docker daemon 989.4MB
Step 1/6 : FROM harbor.od.com/public/jenkins:v2.176.2
---> a25e4f7b2896
Step 2/6 : USER root
---> Using cache
---> 0dd75acbbb3f
Step 3/6 : RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
---> Using cache
---> 5898f805a668
Step 4/6 : ADD id_rsa /root/.ssh/id_rsa
---> Using cache
---> 8a7476c6dd6c
Step 5/6 : ADD config.json /root/.docker/config.json
---> Using cache
---> 4fac0a3f29e6
Step 6/6 : RUN echo " StrictHostKeyChecking no" >> /etc/ssh/sshd_config
---> Running in 7f70c4c4d20d
Removing intermediate container 7f70c4c4d20d
---> 667cbf0a78bf
Successfully built 667cbf0a78bf
Successfully tagged harbor.od.com/infra/jenkins:v2.176.2
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# tar xf jdk-7u80-linux-x64.tar.gz -C ./
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# rm -rf jdk*
当前的底包只是一个最基础的底包,实际生成环境中,根据需要调整,或者根据当前的底包制作新的底包都可以。
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# docker pull docker.io/stanleyws/jre8:8u112
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# docker images|grep jre
stanleyws/jre8 8u112 fa3a085d6ef1 3 years ago 363MB
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# docker tag fa3a085d6ef1 harbor.od.com/public/jre:8u112
[root@k8s7-200.host.com /data/nfs-volume/jenkins_home]# docker push harbor.od.com/public/jre:8u112
[root@k8s7-200.host.com /data/dockerfile/jre8]# cat Dockerfile
FROM harbor.od.com/public/jre:8u112
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
echo 'Asia/Shanghai' >/etc/timezone
ADD config.yml /opt/prom/config.yml
ADD jmx_javaagent-0.3.1.jar /opt/prom/
WORKDIR /opt/project_dir
ADD entrypoint.sh /entrypoint.sh
CMD ["/entrypoint.sh"]
[root@k8s7-200.host.com /data/dockerfile/jre8]# wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar -O jmx_javaagent-0.3.1.jar
[root@k8s7-200.host.com /data/dockerfile/jre8]# cat config.yml
---
rules:
- pattern: '.*'
[root@k8s7-200.host.com /data/dockerfile/jre8]# cat entrypoint.sh
#!/bin/sh
# C_OPTS 和 JAR_BALL 由环境变量注入
M_OPTS="-Duser.timezone=Asia/Shanghai -javaagent:/opt/prom/jmx_javaagent-0.3.1.jar=$(hostname -i):${M_PORT:-"12346"}:/opt/prom/config.yml"
C_OPTS=${C_OPTS}
JAR_BALL=${JAR_BALL}
exec java -jar ${M_OPTS} ${C_OPTS} ${JAR_BALL}
[root@k8s7-200.host.com /data/dockerfile/jre8]# chmod +x entrypoint.sh
登录harbor创建一个base项目
[root@k8s7-200.host.com /data/dockerfile/jre8]# docker build . -t harbor.od.com/base/jre8:8u112
[root@k8s7-200.host.com /data/dockerfile/jre8]# docker push harbor.od.com/base/jre8:8u112
更多推荐
所有评论(0)