準備鏡像的操做在7.200
運維機上完成node
docker pull jenkins/jenkins:2.190.3 docker tag jenkins/jenkins:2.190.3 harbor.zq.com/public/jenkins:v2.190.3 docker push harbor.zq.com/public/jenkins:v2.190.3
基於官方jenkins鏡像,編寫dockerfile作個性化配置git
mkdir -p /data/dockerfile/jenkins/ cd /data/dockerfile/jenkins/
cat >/data/dockerfile/jenkins/Dockerfile <<'EOF' FROM harbor.zq.com/public/jenkins:v2.190.3 #定義啓動jenkins的用戶 USER root #修改時區爲東八區 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\ echo 'Asia/Shanghai' >/etc/timezone #加載用戶密鑰,使用ssh拉取dubbo代碼須要 ADD id_rsa /root/.ssh/id_rsa #加載運維主機的docker配置文件,裏面包含登陸harbor倉庫的認證信息。 ADD config.json /root/.docker/config.json #在jenkins容器內安裝docker客戶端,docker引擎用的是宿主機的docker引擎 ADD get-docker.sh /get-docker.sh # 跳過ssh時候輸入yes的交互步驟,並執行安裝docker RUN echo " StrictHostKeyChecking no" >/etc/ssh/ssh_config &&\ /get-docker.sh EOF
建立祕鑰對:docker
ssh-keygen -t rsa -b 2048 -C "lg@126.com" -N "" -f /root/.ssh/id_rsa cp /root/.ssh/id_rsa /data/dockerfile/jenkins/
郵箱請根據本身的郵箱自行修改
建立完成後記得把公鑰放到gitee的信任中apache
獲取docker.sh腳本:json
curl -fsSL get.docker.com -o /data/dockerfile/jenkins/get-docker.sh chmod u+x /data/dockerfile/jenkins/get-docker.sh
拷貝config.json文件:api
cp /root/.docker/config.json /data/dockerfile/jenkins/
cd /data/dockerfile/jenkins/ docker build . -t harbor.zq.com/infra/jenkins:v2.190.3 docker push harbor.zq.com/infra/jenkins:v2.190.3
建立專有名詞空間infra
的目錄是將jenkins等運維相關軟件放到同一個namespace下,便於統一管理以及和其餘資源分開跨域
kubectl create ns infra
Secret
用來保存敏感信息,例如密碼、OAuth 令牌和 ssh key等,有三種類型:瀏覽器
Opaque:
base64 編碼格式的 Secret,用來存儲密碼、密鑰等,能夠反解,加密能力弱bash
kubernetes.io/dockerconfigjson:
用來存儲私有docker registry的認證信息。app
kubernetes.io/service-account-token:
用於被serviceaccount
引用,serviceaccout 建立時Kubernetes會默認建立對應的secret
前面dashborad部分以及用過了
訪問docker的私有倉庫,必需要建立專有的secret類型,建立方法以下:
kubectl create secret docker-registry harbor \ --docker-server=harbor.zq.com \ --docker-username=admin \ --docker-password=Harbor12345 \ -n infra # 查看結果 ~]# kubectl -n infra get secrets NAME TYPE DATA AGE default-token-rkg7q kubernetes.io/service-account-token 3 19s harbor kubernetes.io/dockerconfigjson 1 12s
解釋命令:
建立一條secret,資源類型是docker-registry,名字是 harbor
並指定docker倉庫地址、訪問用戶、密碼、倉庫名
jenkins中一些數據須要持久化的,能夠使用共享存儲進行掛載:
這裏使用最簡單的NFS共享存儲,由於k8s默認支持nfs模塊
若是使用其餘類型的共享存儲
yum install nfs-utils -y echo '/data/nfs-volume 10.4.7.0/24(rw,no_root_squash)' >>/etc/exports mkdir -p /data/nfs-volume/jenkins_home systemctl start nfs systemctl enable nfs # 查看結果 ~]# showmount -e Export list for hdss7-200: /data/nfs-volume 10.4.7.0/24
yum install nfs-utils -y
mkdir /data/k8s-yaml/jenkins
有兩個須要注意的地方:
掛載了宿主機的docker.sock
使容器內的docker客戶端能夠直接與宿主機的docker引擎進行通訊
在使用私有倉庫的時候,資源清單中,必定要聲明:
imagePullSecrets: - name: harbor
cat >/data/k8s-yaml/jenkins/dp.yaml <<EOF 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: hdss7-200 path: /data/nfs-volume/jenkins_home - name: docker hostPath: path: /run/docker.sock type: '' containers: - name: jenkins image: harbor.zq.com/infra/jenkins:v2.190.3 imagePullPolicy: IfNotPresent 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: 1 revisionHistoryLimit: 7 progressDeadlineSeconds: 600 EOF
cat >/data/k8s-yaml/jenkins/svc.yaml <<EOF kind: Service apiVersion: v1 metadata: name: jenkins namespace: infra spec: ports: - protocol: TCP port: 80 targetPort: 8080 selector: app: jenkins EOF
cat >/data/k8s-yaml/jenkins/ingress.yaml <<EOF kind: Ingress apiVersion: extensions/v1beta1 metadata: name: jenkins namespace: infra spec: rules: - host: jenkins.zq.com http: paths: - path: / backend: serviceName: jenkins servicePort: 80 EOF
任意node節點
kubectl create -f http://k8s-yaml.zq.com/jenkins/dp.yaml kubectl create -f http://k8s-yaml.zq.com/jenkins/svc.yaml kubectl create -f http://k8s-yaml.zq.com/jenkins/ingress.yaml
啓動時間很長,等待結果
kubectl get pod -n infra
docker exec -it 8ff92f08e3aa /bin/bash # 查看用戶 whoami # 查看時區 date # 查看是否能用宿主機的docker引擎 docker ps # 看是否能免密訪問gitee ssh -i /root/.ssh/id_rsa -T git@gitee.com # 是否能訪問是否harbor倉庫 docker login harbor.zq.com
到運維機上查看持久化數據是否成功存放到共享存儲
~]# ll /data/nfs-volume/jenkins_home total 36 -rw-r--r-- 1 root root 1643 May 5 13:18 config.xml -rw-r--r-- 1 root root 50 May 5 13:13 copy_reference_file.log -rw-r--r-- 1 root root 156 May 5 13:14 hudson.model.UpdateCenter.xml -rw------- 1 root root 1712 May 5 13:14 identity.key.enc -rw-r--r-- 1 root root 7 May 5 13:14 jenkins.install.UpgradeWizard.state -rw-r--r-- 1 root root 171 May 5 13:14 jenkins.telemetry.Correlator.xml drwxr-xr-x 2 root root 6 May 5 13:13 jobs drwxr-xr-x 3 root root 19 May 5 13:14 logs -rw-r--r-- 1 root root 907 May 5 13:14 nodeMonitors.xml drwxr-xr-x 2 root root 6 May 5 13:14 nodes drwxr-xr-x 2 root root 6 May 5 13:13 plugins -rw-r--r-- 1 root root 64 May 5 13:13 secret.key -rw-r--r-- 1 root root 0 May 5 13:13 secret.key.not-so-secret drwx------ 4 root root 265 May 5 13:14 secrets drwxr-xr-x 2 root root 67 May 5 13:19 updates drwxr-xr-x 2 root root 24 May 5 13:14 userContent drwxr-xr-x 3 root root 56 May 5 13:14 users drwxr-xr-x 11 root root 4096 May 5 13:13 war
找到jenkins初始化的密碼
~]# cat /data/nfs-volume/jenkins_home/secrets/initialAdminPassword 02f69d78026d489e87b01332f1caa85a
cd /data/nfs-volume/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
jenkins部署成功後後,須要給他添加外網的域名解析
vi /var/named/zq.com.zone jenkins A 10.4.7.10 # 重啓服務 systemctl restart named
瀏覽器訪問http://jenkins.zq.com
,使用前面的密碼進入jenkins
進入後操做:
manage jenkins
->Configure Global Security
菜單中設置allow anonymous read access
prevent cross site request forgery exploits
blue ocean
admin:admin123
由於jenkins的數據目錄已經掛載到了NFS中作持久化,所以能夠直接將maven放到NFS目錄中,同時也就部署進了jenkins
wget https://archive.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz tar -zxf apache-maven-3.6.1-bin.tar.gz -C /data/nfs-volume/jenkins_home/ mv /data/nfs-volume/jenkins_home/{apache-,}maven-3.6.1 cd /data/nfs-volume/jenkins_home/maven-3.6.1
修改下載倉庫地址,除了<mirror>
中是新增的阿里雲倉庫地址外,其餘內容都是settings.xml
中原有的配置(只是清除了註釋內容)
cat >conf/settings.xml <<'EOF' <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <pluginGroups> </pluginGroups> <proxies> </proxies> <servers> </servers> <mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> </profiles> </settings> EOF