首页 > 其他分享 >k8s部署jenkins

k8s部署jenkins

时间:2022-12-31 11:55:06浏览次数:41  
标签:kubectl 部署 创建 yaml pvc Jenkins jenkins k8s

1. 部署

  • Jenkins版本:2.375.1

创建命名空间:

kubectl create namespace kube-ops

创建PVC,为Jenkins提供数据持久化:

mkdir -p /root/jenkins_install && cd /root/jenkins_install
cat > jenkins_pvc.yaml <<EOF
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pvc
  namespace: kube-ops
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
EOF

这里我们使用的是默认的storageclasses.storage.k8s.io去创建pv,本地是用的nfs方式挂载本地目录:/nfs

创建pvc:

kubectl apply -f jenkins_pvc.yaml

kubectl get pvc -n kube-ops
NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
jenkins-pvc   Bound    pvc-027cb835-6aed-4d60-afa2-08e8842c7eb3   20Gi       RWX            nfs-storage    31s

创建角色授权:

cat > jenkins_account.yaml <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-sa
  namespace: kube-ops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: jenkins-cr
rules:
  - apiGroups: ["extensions", "apps"]
    resources: ["deployments"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["services"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["create","delete","get","list","patch","update","watch"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create","delete","get","list","patch","update","watch"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: jenkins-crd
roleRef:
  kind: ClusterRole
  name: jenkins-cr
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: jenkins-sa
  namespace: kube-ops
EOF

创建:

kubectl apply -f jenkins_account.yaml

在Kubernetes中部署Jenkins,新建Deployment,jenkins-deploy.yaml:

cat > jenkins_Deployment.yaml <<EOF
kind: Deployment
apiVersion: apps/v1
metadata:
  name: jenkins
  namespace: kube-ops
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 8.8.8.8
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        imagePullPolicy: IfNotPresent
        env:
        - name: JAVA_OPTS
          value: -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai
        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: jenkinshome
          mountPath: /var/jenkins_home
      securityContext:
        fsGroup: 1000
      volumes:
      - name: jenkinshome
        persistentVolumeClaim:
          claimName: jenkins-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: kube-ops
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: NodePort
  ports:
  - name: web
    port: 8080
    targetPort: web
    nodePort: 30002
  - name: agent
    port: 50000
    targetPort: agent
EOF

这里之前遇到过访问不到网络的问题,单独配置了dns:8.8.8.8给解决了。

创建:

kubectl apply -f jenkins_Deployment.yaml

访问svc:

kubectl get svc -n kube-ops
NAME      TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)                          AGE
jenkins   NodePort   10.98.187.64   <none>        8080:30002/TCP,50000:30865/TCP   3h31m

链接:http://192.168.80.45:30002

查看密码:

kubectl logs -n kube-ops jenkins-7d7994fff5-92jvd
2022-12-31 03:43:19.145+0000 [id=31]    INFO    jenkins.install.SetupWizard#init:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

580f0d4a0c8047a1b054b17c57dd907b

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2. 安装插件

 

标签:kubectl,部署,创建,yaml,pvc,Jenkins,jenkins,k8s
From: https://www.cnblogs.com/Mercury-linux/p/17016397.html

相关文章

  • k8s教程(22)-pod调度总结
    文章目录​​01概述​​​​1.1Pod调度控制器分类​​​​1.2RC到Deployment的发展​​​​1.2.1ReplicaSet​​​​1.3Pod调度​​​​1.3.1情景​​​​1.3.2存在......
  • k8s教程(25)-pod扩缩容
    文章目录​​01引言​​​​02手动扩缩容机制​​​​03自动扩缩容机制​​​​3.1HPA控制器​​​​3.2指标的类型​​​​3.3扩缩容算法​​​​3.4HorizontalPodA......
  • k8s教程(21)-pod之容灾调度
    文章目录​​01引言​​​​02如何实现?​​​​03举例​​​​04文末​​01引言声明:本文为《Kubernetes权威指南:从Docker到Kubernetes实践全接触(第5版)》的读书笔记我们......
  • 使用MicroK8s部署第一个应用程序
    Kubernetes具有挑战性。对此,没有任何争议。不仅在将容器部署到Kubernetes集群中有很多移动部件,而且在这一过程中可能会出现很多问题。更为复杂的是,部署Kubernetes集群可能是......
  • 【云原生】K8s PSP 和 securityContext 介绍与使用
    目录一、概述二、PodSecurityPolicy的发展1)以前为什么需要PodSecurityPolicy?2)现在为什么PodSecurityPolicy要消失?三、PSP简单使用1)开启PSP2)示例演示1、没有PSP场景测......
  • Docker部署redis
    1、拉取镜像dockerpullredis:6.2.82、创建宿主机文件挂载,创建data文件夹和redis.conf文件  3、redis.conf,可根据自己需要进行编辑#bind127.0.0.1protecte......
  • docker部署nacos集群
    1、拉取镜像dockerpullnacos/nacos-server2、单机部署​单机版部署很简单,直接一条命令即可完成,通过 MODE 来设置使用单机模式;注意如果是多网卡的话需要配置......
  • Docker部署nginx并挂载配置
    1、下载镜像并启动一个容器dockerpullnginx创建容器并运行dockerrun-d--namemynginx-p80:80 2、将nginx配置文件挂载到宿主机在宿主机创建目录: ......
  • python创建config.xml文件,调用Jenkins API接口创建带参数的Job
    文章目录​​一、要求​​​​二、pipeline模板xml的内容可以通过如下方式获取​​​​三、代码​​​​四、效果​​​​五、参考文章​​一、要求将hudson.model.StringPa......
  • python 部署django项目到公网 无法连接
    https://blog.csdn.net/xiongzaiabc/article/details/108448390 服务器后台运行:https://www.jianshu.com/p/4041c4e6e1b0......