kubernetes部署jenkins
文章目录
提供者:MappleZF
版本:1.0.0
一、准备jenkin镜像
1.1 下载镜像
官网:https://www.jenkins.io/
阿里云镜像站:https://developer.aliyun.com/mirror/jenkins?spm=a2c6h.13651102.0.0.3e221b11BmNsCI
[root@k8smaster01:/root]# docker pull jenkins/jenkins:lts-centos7
[root@k8smaster01:/data/yaml/jenkins]# docker tag docker.io/jenkins/jenkins:lts-centos7 harbor.iot.com/public/jenkins:lts-centos7
[root@k8smaster01:/data/yaml/jenkins]# docker push harbor.iot.com/public/jenkins:lts-centos7
1.2 生成密钥
生产SSH密钥对:
root@k8smaster01:/root]# ssh-keygen -t rsa -b 2048 -C "524120@qq.com" -N "" -f /root/.ssh/id_rsa
[root@k8smaster01.host.com:/root/.ssh]# ssh-keygen -t rsa -b 2048 -C "kubernetes@lowaniot.com" -N "" -f /root/.ssh/id_kubernetes_rsa
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_kubernetes_rsa.
Your public key has been saved in /root/.ssh/id_kubernetes_rsa.pub.
The key fingerprint is:
SHA256:qUo6BuRJoaenkNuWgFg5GPaFL4S4PI11O4LBZyhb7ko kubernetes@lowaniot.com
The key's randomart image is:
+---[RSA 2048]----+
|o o . |
|+B.* o |
|=B%.+ . |
|=B*= + . |
|**o.o . S |
|OEo . |
|oO .. . |
|+ *o . |
| o... |
+----[SHA256]-----+
1.3 自定义Dockerfile
编辑Dockerfile
[root@k8smaster01:/data/yaml/jenkins]# vim Dockerfile
FROM harbor.iot.com/public/jenkins:lts-centos7
USER root
ADD id_kubernetes_rsa /root/.ssh/id_rsa
ADD CentOS-Base.repo /etc/yum.repos.d/
ADD config.json /root/.docker/config.json
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo 'Asia/Shanghai' > /etc/timezone && \
echo " StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
yum makecache fast && yum -y install docker-ce && \
yum install -y nfs-utils curl yum-utils device-mapper-persistent-data lvm2 net-tools \
yum install -y conntrack-tools wget sysstat vim-enhanced bash-completion psmisc traceroute iproute* tree libseccomp libtool-ltdl \
yum clean all && rm -rf /var/cache/yum/*
1.4 准备构建镜像的文件
准备文件:
[root@k8smaster01:/data/Dockerfile/jenkins]# cp -p /root/.ssh/id_rsa .
[root@k8smaster01:/data/Dockerfile/jenkins]# cp -p /root/.docker/config.json .
[root@k8smaster01:/data/Dockerfile/jenkins]# ll
总用量 28
-rw------- 1 root root 253 8月 6 09:54 config.json
-rw-r--r-- 1 root root 345 8月 11 22:51 Dockerfile
-rw------- 1 root root 1675 7月 25 17:58 id_rsa
1.5 构建镜像
构建镜像
[root@k8smaster01:/data/Dockerfile/jenkins]# docker build -t harbor.iot.com/public/jenkins:v2.235.4 ./
[root@k8smaster01:/data/yaml/jenkins]# docker push harbor.iot.com/public/jenkins:v2.235.4
检验:kubernetes-applekubernetes
ssh -i /root/.ssh/id_kubernetes_rsa -T gogs@go.lowaniot.com
docker run --rm harbor.iot.com/public/jenkins:v2.235.4 ssh -i /root/.ssh/id_rsa -T gogs@go.lowaniot.com
补充:jenkins默认安装路径
/usr/lib/jenkins/jenkins.war war包
/etc/sysconfig/jenkins jenkins配置文件
/var/lib/jenkins/ 默认的JENKINS_HOME目录
/var/log/jenkins/jenkins.log Jenkins日志文件
二、部署jenkins
2.1 创建资源配置清单
1.创建名称空间
[root@k8smaster01:/data/yaml/jenkins]# vim namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: devops
spec: {}
status: {}
root@k8smaster01:/data/yaml/jenkins]# kubectl apply -f namespace.yaml
--------分割线------------------------分割线----------------------------------分割线-----------------------分割线---------
2.创建secret
root@k8smaster01:/data/yaml/jenkins]# kubectl create secret docker-registry harbor --docker-server=harbor.iot.com --docker-username=admin --docker-password=appleMysql -n devops
或者
[root@k8smaster01:/data/yaml/jenkins]# vim secret.yaml
apiVersion: v1
data:
.dockerconfigjson: eyJhdXRocyI6eyJoYXJib3IuaW90LmNvbSI6eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJhcHBsZU15c3FsIiwiYXV0aCI6IllXUnRhVzQ2WVhCd2JHVk5lWE54YkE9PSJ9fX0=
kind: Secret
metadata:
creationTimestamp: null
name: harbor
namespace: devops
type: kubernetes.io/dockerconfigjson
~
[root@k8smaster01:/data/yaml/jenkins]# kubectl apply -f secret.yaml
--------分割线------------------------分割线----------------------------------分割线-----------------------分割线---------
3.创建ceph-devops-secret
[root@k8smaster01:/data/yaml/jenkins]# vim ceph-devops-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: ceph-devops-secret
namespace: devops
data:
key: QVFES04yQmZ6WnhwQVJBQTBCUDZ1ck9QOWFTdjdNK2RCeW9CK0E9PQ==
[root@k8smaster01:/data/yaml/jenkins]# kubectl apply -f ceph-devops-secret.yaml
--------分割线------------------------分割线----------------------------------分割线-----------------------分割线---------
4.创建deployment
[root@k8smaster01:/data/yaml/jenkins]# vim deployment.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: cephfs-pvjenkins
labels:
pv: cephfs-pv
spec:
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 350Gi
volumeMode: Filesystem
cephfs:
monitors:
- 192.168.13.101:6789
- 192.168.13.102:6789
- 192.168.13.103:6789
path: /cephfs-pvjenkins
readOnly: false
user: admin
secretRef:
name: ceph-devops-secret
persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cephfs-pvcjenkins
namespace: devops
spec:
volumeName: cephfs-pvjenkins
accessModes:
- ReadWriteMany
resources:
requests:
storage: 350Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
namespace: devops
labels:
name: jenkins
spec:
replicas: 1
selector:
matchLabels:
name: jenkins
template:
metadata:
labels:
app: jenkins
name: jenkins
spec:
containers:
- name: jenkins
image: harbor.iot.com/public/jenkins:v2.235.4
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
protocol: TCP
env:
- name: JAVA_OPTS
value: -Xmx512m -Xms512m
volumeMounts:
- name: jenkins-home
mountPath: "/var/jenkins_home"
- name: docker
mountPath: "/run/docker.sock"
volumes:
- name: jenkins-home
persistentVolumeClaim:
claimName: cephfs-pvcjenkins
readOnly: false
- name: docker
hostPath:
path: "/run/docker.sock"
type: ''
imagePullSecrets:
- name: harbor
securityContext:
runAsUser: 0
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
revisionHistoryLimit: 7
progressDeadlineSeconds: 300
~
[root@k8smaster01:/data/yaml/jenkins]# kubectl apply -f deployment.yaml
--------分割线------------------------分割线----------------------------------分割线-----------------------分割线---------
6.创建svc
[root@k8smaster01:/data/yaml/jenkins]# vim svc.yaml
apiVersion: v1
kind: Service
metadata:
name: jenkins
namespace: devops
spec:
ports:
- protocol: TCP
##port 80 表示监听在clusterIP上的port
port: 80
##targetport 8080 表示Pod中运行的port
targetPort: 8080
selector:
app: jenkins
-
[root@k8smaster01:/data/yaml/jenkins]# kubectl apply -f svc.yaml
--------分割线------------------------分割线----------------------------------分割线-----------------------分割线---------
7.创建ingressroute 此处我们全用ingressroute
[root@k8smaster01.host.com:/data/yaml/jenkins]# vim ingressroute.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: jenkins
namespace: devops
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
kubernetes.io/ingress.class: "traefik"
spec:
entryPoints:
- web
routes:
- match: Host(`jenkins.lowan.com`) && PathPrefix(`/`)
kind: Rule
services:
- name: jenkins
port: 80
或者创建ingress
[root@k8smaster01:/data/yaml/jenkins]# vim ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: jenkins
namespace: devops
spec:
rules:
- host: jenkins.lowan.com
http:
paths:
- path: /
backend:
serviceName: jenkins
servicePort: 80
-
[root@k8smaster01:/data/yaml/jenkins]# kubectl apply -f ingress.yaml (暂不使用)
2.2 测试POD运行
[root@k8smaster01.host.com:/root]# kubectl exec -it jenkins-66d9dc8df8-cqjvb -n devops -- /bin/sh
sh-4.2# docker login harbor.iot.com
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
sh-4.2# ssh -i /root/.ssh/id_kubernetes_rsa -T gogs@go.lowaniot.com
Warning: Identity file /root/.ssh/id_kubernetes_rsa not accessible: No such file or directory.
Warning: Permanently added 'go.lowaniot.com,192.168.20.105' (ECDSA) to the list of known hosts.
Hi there, You've successfully authenticated, but Gogs does not provide shell access.
If this is unexpected, please log in with password and setup Gogs under another user.
注:如果出现ssh: Could not resolve hostname go.lowaniot.com: Name or service not known的问题
解决方式可以这样:sh-4.2# vi /etc/resolv.conf
添加: nameserver 192.168.13.99
nameserver 8.8.8.8
三、DNS服务器配置
[root@lb03.host.com:/var/named]# vim lowaniot.com.zone
jenkins A 192.168.13.100
注:配置负载均衡调度后,直接用VIP
systemctl restart named
[root@lb03.host.com:/var/named]# dig -t A jenkins.lowan.com @192.168.13.99 +short
192.168.13.100
[root@lb03.host.com:/var/named]# dig -t A go.lowaniot.com @192.168.13.99 +short
192.168.20.105
四、测试页面
浏览器访问:http://jenkins.lowan.com/
创建用户:devops-k8s applejenkins kubernetes@lowaniot.com
后续参考(集群):
01 kubernetes二进制部署
02 kubernetes辅助环境设置
03 K8S集群网络ACL规则
04 Ceph集群部署
05 部署zookeeper和kafka集群
06 部署日志系统
07 部署Indluxdb-telegraf
08 部署jenkins
09 部署k3s和Helm-Rancher
10 部署maven软件