1.在172.19.2.51上部署node
mkdir -pv /opt/git git clone http://172.19.2.140:18080/lvqingshan/gcgj.git cd /opt/git/gcgj scp app@172.19.2.1:/home/app/portal-tomcat/webapps/portal.war ./ scp app@192.168.37.34:/home/app/portal-tomcat/conf/server.xml ./ vim Dockerfile FROM tomcat:7.0.77-jre8 #這裏是基礎鏡像 ADD server.xml /usr/local/tomcat/conf #server.xml文件要和Dockerfile再同一目錄,這裏是替換文件 RUN rm -rf /usr/local/tomcat/webapps/* COPY portal.war /usr/local/tomcat/webapps/ROOT.war #portal.war文件要和Dockerfile再同一目錄,這裏是複製文件 EXPOSE 8080 #對外開放端口 CMD ["/usr/local/tomcat/bin/catalina.sh","run"] #啓動鏡像時執行的命令
2.測試dockerfile是否能正常工做git
docker build -t gcgj/portal . docker run -p 38080:8080 -idt gcgj/portal:latest git add -A git commit git push -u origin master gitlab帳號lvqingshan 密碼abcd1234
1.在192.168.13.45上配置倉庫私鑰web
mkdir -pv /etc/docker/certs.d/172.19.2.139/ vim /etc/docker/certs.d/172.19.2.139/ca.crt -----BEGIN CERTIFICATE----- MIIDvjCCAqagAwIBAgIUQzFZBuFh7EZLOzWUYZ10QokL+BUwDQYJKoZIhvcNAQEL BQAwZTELMAkGA1UEBhMCQ04xEDAOBgNVBAgTB0JlaUppbmcxEDAOBgNVBAcTB0Jl aUppbmcxDDAKBgNVBAoTA2s4czEPMA0GA1UECxMGU3lzdGVtMRMwEQYDVQQDEwpr dWJlcm5ldGVzMB4XDTE3MDcwNDA4NTMwMFoXDTIyMDcwMzA4NTMwMFowZTELMAkG A1UEBhMCQ04xEDAOBgNVBAgTB0JlaUppbmcxEDAOBgNVBAcTB0JlaUppbmcxDDAK BgNVBAoTA2s4czEPMA0GA1UECxMGU3lzdGVtMRMwEQYDVQQDEwprdWJlcm5ldGVz MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyWgHFV6Cnbgxcs7X7ujj APnnMmotzNnnTRhygJLCMpCZUaWYrdBkFE4T/HGpbYi1R5AykSPA7FCffFHpJIf8 Gs5DAZHmpY/uRsLSrqeP7/D8sYlyCpggVUeQJviV/a8L7PkCyGq9DSiU/MUBg4CV Dw07OT46vFJH0lzTaZJNSz7E5QsekLyzRb61tZiBN0CJvSOxXy7wvdqK0610OEFM T6AN8WfafTH4qmKWulFBJN1LjHTSYfTZzCL6kfTSG1M3kqG0W4B2o2+TkNLVmC9n gEKdeh/yQmQWfraRkuWiCorJZGxte27xpjgu7u62sRyCm92xQRNgp5RiGHxP913+ HQIDAQABo2YwZDAOBgNVHQ8BAf8EBAMCAQYwEgYDVR0TAQH/BAgwBgEB/wIBAjAd BgNVHQ4EFgQUDFiYOhMMWkuq93iNBoC1Udr9wLIwHwYDVR0jBBgwFoAUDFiYOhMM Wkuq93iNBoC1Udr9wLIwDQYJKoZIhvcNAQELBQADggEBADTAW0FPhfrJQ6oT/WBe iWTv6kCaFoSuWrIHiB9fzlOTUsicrYn6iBf+XzcuReZ6qBILghYGPWPpOmnap1dt 8UVl0Shdj+hyMbHzxR0XzX12Ya78Lxe1GFg+63XbxNwOURssd9DalJixKcyj2BW6 F6JG1aBQhdgGSBhsCDvG1zawqgZX/h4VWG55Kv752PYBrQOtUH8CS93NfeB5Q7bE FOuyvGVd1iO40JQLoFIkZuyxNh0okGjfmT66dia7g+bC0v1SCMiE/UJ9uvHvfPYe qLkSRjIHH7FH1lQ/AKqjl9qrpZe7lHplskQ/jynEWHcb60QRcAWPyd94OPrpLrTU 64g= -----END CERTIFICATE-----
2.登陸倉庫docker
docker login 172.19.2.139 Username: admin Password: Cmcc@1ot
3.上傳鏡像測試shell
在habor上建立gcgj倉庫後才能pushvim
docker tag gcgj/portal:latest 172.19.2.139/gcgj/portal docker login -p admin -u Cmcc@1ot -e 172.19.2.139 docker push 172.19.2.139/gcgj/portal
在kubernetes主節點172.19.2.50上配置api
vim /opt/kube-portal/portal-rc1.yaml apiVersion: v1 kind: ReplicationController metadata: name: gcgj-portal spec: replicas: 2 selector: app: portal template: metadata: labels: app: portal #加標籤 spec: containers: - image: 172.19.2.139/gcgj/portal:latest #要發佈的鏡像 name: portal resources: limits: cpu: "2" #pod佔用的cpu資源 memory: 2Gi #pod佔用的內存資源 ports: - containerPort: 8080 #pod提供的端口 volumeMounts: - mountPath: /usr/local/tomcat/logs #鏡像內要掛載的目錄 name: portal-logs volumes: - name: portal-logs hostPath: path: /opt/logs/portal #映射到本地的目錄 vim /opt/kube-portal/portal-svc1.yaml apiVersion: v1 kind: Service metadata: name: gcgj-portal spec: ports: - name: portal-svc port: 8080 targetPort: 8080 nodePort: 30088 #proxy映射出來的端口 selector: app: portal type: NodePort #端口類型
1.General中配置參數化構建過程tomcat
新增String Parameter 名字:VERSION 默認值:[空] 描述:請輸入版本號
2.源碼管理Git設置app
Repository URL 爲http://172.19.2.140:18080/lvqingshan/gcgj.git
3.設置Gitlab出現變動自動觸發構建webapp
一分鐘檢測一次gitlab項目是否有變化
*/1 * * * *
4.Execute shell設置
兩種控制版本的方式,當自動觸發構建或者版本號爲空時使用時間戳做爲版本,當填入版本號時使用填入的版本號
imagesid=`docker images | grep -i gcgj | awk '{print $3}'| head -1` project=/var/lib/jenkins/jobs/build-docker-router-portal/workspace if [ -z "$VERSION" ];then VERSION=`date +%Y%m%d%H%M` fi echo $VERSION if docker ps -a|grep -i gcgj;then docker rm -f gcgj fi if [ -z "$imagesid" ];then echo $imagesid "is null" else docker rmi -f $imagesid fi docker build -t gcgj/portal:$VERSION $project docker tag gcgj/portal:$VERSION 172.19.2.139/gcgj/portal:$VERSION docker tag gcgj/portal:$VERSION 172.19.2.139/gcgj/portal:latest docker login -u admin -p Cmcc@1ot 172.19.2.139 docker push 172.19.2.139/gcgj/portal:$VERSION docker push 172.19.2.139/gcgj/portal:latest
5.ansible-playbook配置
在ansible主機192.168.13.45上配置
vim /home/app/ansible/playbooks/opstest/portal.yaml - hosts: 172.19.2.50 remote_user: app sudo: yes tasks: - name: 關閉原有pod shell: kubectl delete -f /opt/kube-portal ignore_errors: yes - name: 啓動新pod shell: kubectl create -f /opt/kube-portal