有一段時間沒寫了莹汤,這個坑一直在這怪難受的決定把它填了。
大體的搭建流程和先前寫到的 Git Server 大同小異,但細(xì)節(jié)部分不盡相同隘道,有興趣的同僚亦可移步至 這里。
關(guān)于 Kubernetes 的配置請移步至 這里郎笆。
作為我近日工作的 Microservices framework 中的重要一環(huán)谭梗,Jenkins 的搭建自然成為了重中之重。
二話不說先上我的 Dockerfile:
FROM jenkins:alpine
MAINTAINER Ralph Wang
# 因為在公司工作的關(guān)系內(nèi)部網(wǎng)絡(luò)經(jīng)常會阻礙 Container 與外部的連結(jié)宛蚓,遂加下文以設(shè)置代理激捏。
ENV http_proxy ${proxy_address}:${proxy_port}
ENV https_proxy ${proxy_address}:${proxy_port}
USER root
# Download and config kubectl(-s:Silent or quiet mode;-O:Download凄吏;-S:Show error)
RUN curl -L -O https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl; chmod +x ./kubectl; mv ./kubectl /usr/local/bin/kubectl
# Download and config docker
RUN curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && tar -xvzf docker-latest.tgz; mv docker/* /usr/bin/ && rm docker-latest.tgz
上述兩個 curl command 分別 download / config 了 kubectl 和 docker 兩個命令远舅,docker 是用來在 Jenkins 的任務(wù)過程中創(chuàng)建 Image闰蛔,kubectl 是用來在 Jenkins 的任務(wù)中實(shí)現(xiàn) Deployment 和 Service 的創(chuàng)建。
完成 Dockerfile 之后图柏,我們需要建立此 Image序六,我將其命名為 jenkinserver。
再來我們需要用一個 config 文件來實(shí)現(xiàn)創(chuàng)建 Kubernetes Deployment爆办,下面就是我所編寫的 jenkins-deployment.yaml(切記 .yaml 文件不能使用 Tab):
apiVersion: apps/v1
kind: Deployment
metadata:
? name: jenkins
? labels:
? ? app: jenkins
spec:
? replicas: 1 # 我目前只需要一個 Instance 所以就設(shè)置為1难咕。
? selector:
? ? matchLabels:
? ? ? app: jenkins
? template: # This is the pod template。
? ? metadata:
? ? ? labels:
? ? ? ? app: jenkins
? ? spec:
? ? ? containers:
? ? ? - name: jenkins
? ? ? ? image: jenkinserver # Image 的名字距辆。
? ? ? ? imagePullPolicy: Never # 迫使 Kubernetes 使用本地鏡像余佃。
? ? ? ? ports:
? ? ? ? - containerPort: 8080 # 8080 作為 Web Console 的 Port。
? ? ? ? ? name: web
? ? ? ? ? protocol: TCP
? ? ? ? - containerPort: 50000
? ? ? ? ? name: agent
? ? ? ? ? protocol: TCP
? ? ? ? volumeMounts:
? ? ? ? - mountPath: /var/run/docker.sock # 首先掛載了本地 docker 的接口跨算,以便在 Jenkins Container 中訪問 docker爆土。
? ? ? ? ? name: docker-sock-volume
? ? ? ? - mountPath: /var/jenkins_home # 其次掛載了 jenkins_home 以便在 Container 被摧毀是不丟失 Jenkins Server 中的內(nèi)容。
? ? ? ? ? name: jenkins-volume
? ? ? ? - mountPath: /var/.kube/config # 最后掛載了外部 Kubernetes 的配置文件诸蚕,以確辈绞疲可以在 Jenkins Container 內(nèi)部對外部的 Kubernetes 進(jìn)行修改。
? ? ? ? ? name: kube-config
? ? ? volumes:
? ? ? - name: docker-sock-volume # Docker sock 卷背犯。
? ? ? ? hostPath:
? ? ? ? ? path: /var/run/docker.sock
? ? ? - name: jenkins-volume # Jenkins 卷坏瘩。
? ? ? ? hostPath:
? ? ? ? ? path: /c/Users/ralph.wang/.jenkins
? ? ? - name: kube-config # Kubernetes config 卷。
? ? ? ? hostPath:
? ? ? ? ? path: /c/Users/ralph.wang/.kube/config
By running:
kubectl create -f jenkins-deployment.yaml
便可以實(shí)現(xiàn)創(chuàng)建 Deployment漠魏,隨后可以通過
kubectl get deployment jenkins
進(jìn)行確認(rèn)倔矾。
建立完 Deployment 之后,需要搭載 Service 來確敝拢可以在外部進(jìn)行連結(jié)哪自,下面是我創(chuàng)建的 jenkins-service.yaml:
apiVersion: v1
kind: Service
metadata:
? labels:
? ? app: jenkins
? name: jenkins
spec:
? type: NodePort # 和筆記之三的 GitServer 一樣采用 NodePort 的形式直接曝露 Service。
? ports:
? - port: 8080
? ? targetPort: 8080
? ? nodePort: 32200
? ? name: web
? selector:
? ? app: jenkins
隨后我們可以使用
kubectl create -f jenkins-service.yaml
進(jìn)行服務(wù)的創(chuàng)建禁熏,并且使用
kubectl get service jenkins
進(jìn)行服務(wù)的再確認(rèn)壤巷。
初始 Jenkins 的時候會需要在 console 里輸入初始密碼,這個密碼可以在?/var/jenkins_home/secrets/initialAdminPassword 中找到(當(dāng)然因為我們在上文中使用了 Volume瞧毙,所以自然可以在本地中瀏覽該文件胧华,即范例中的 /c/Users/ralph.wang/.jenkins/secrets/initialAdminPassword)。
配置完成之后即可開始使用啦宙彪!
希望能對大家有所幫助撑柔。文章的不足之處也請留言告知,感謝您访!