编写jenkins.yaml
说明:
容器跑起来后,jenkins的目录是/var/jenkins_home
存储卷用的是hostPath,这里面我们指定pod调度到k8s-master01
在k8s-master01上创建目录:mkdir /data_jenkins
创建名称空间:kubectl create ns jenkins
jenkins.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: jenkins
namespace: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
template:
metadata:
labels:
app: jenkins
spec:
nodeName: k8s-master01
containers:
- name: jenkins
image: jenkins/jenkins:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
- containerPort: 50000
name: agent
protocol: TCP
resources:
limits:
cpu: 2000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 5
failureThreshold: 12
readinessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 5
failureThreshold: 12
volumeMounts:
- name: jenkins-volume
mountPath: /var/jenkins_home
volumes:
- name: jenkins-volume
hostPath:
path: /data_jenkins
type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-svc
namespace: jenkins
labels:
app: jenkins
spec:
selector:
app: jenkins
type: NodePort
ports:
- name: web
port: 8080
targetPort: web
nodePort: 30667
- name: agent
port: 50000
targetPort: agent
另外,jenkins在容器中的uid是1000,赋予其宿主机上目录的权限:chown -R 1000.1000 /data_jenkins

创建资源
应用资源文件:kubectl apply -f jenkins.yaml
查看pod:kubectl get po -n jenkins -owide

查看svc:kubectl get svc -n jenkins

通过k8s集群任意一个节点访问:192.168.117.161:30667
进入容器查看密码:
kubectl exec -it -n jenkins po/jenkins-dd6c9cdcd-m9964 -- sh
密码是: 1e45bee7642d44e3acd5e9563f1bebf2
也可以在宿主机上查看密码:

输入密码,点击“继续”

选择“安装推荐的插件”
此步耗时较多,需要耐心等待

点击“继续”,创建第一个管理员用户


配置站点
也就是更改插件源为国内插件源,否则下载插件会很慢

URL中输入如下内容:
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

点击“立即获取”


bak005