同步发表于个人站点:http://www.panzhixiang.cn

说在前面:这篇文章是基于阳明大佬的博客写的,针对我的实际情况做了一些修改,大家可以移步大佬的博客动态jenkins slave

Deployment的yaml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins
  namespace: kube-ops-uat
spec:
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccount: jenkins
      containers:
      # 下面两个env的和jvm相关的
      - env:
        - name: LIMITS_MEMORY
          valueFrom:
            resourceFieldRef:
              divisor: 1Mi
              resource: limits.memory
        - name: JAVA_OPTS
          value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0
            -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85
            -Duser.timezone=Asia/Shanghai

        name: jenkins
        image: jenkins/jenkins:lts
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        - containerPort: 50000
          name: agent
          protocol: TCP
          #对于k8s中的pod都建议加上resource限制,防止一个pod出现故障导致整个node的资源都被耗尽
        resources:
          limits:
            cpu: 1000m
            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
        #这里做了持久化,要事先准备好pvc,下面有配置文件
        volumeMounts:
        - name: jenkinshome
          subPath: jenkins
          mountPath: /var/jenkins_home
      securityContext:
        fsGroup: 1000
      volumes:
      - name: jenkinshome
        persistentVolumeClaim:
          claimName: kube-ops-pvc-uat

service的yaml文件

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: kube-ops-uat
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  # 这里采用了ClusterIP的方式暴露服务,所以后面要通过ingress的方式将服务暴露的集群外使用
  type: ClusterIP
  ports:
  - name: web
    port: 8080
    targetPort: web
  - name: agent
    port: 50000
    targetPort: agent

rbac的yaml文件

后面正式使用jenkins的时候需要使用到一个有一定权限的ServiceAccount,这里copy了大佬的配置

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
  namespace: kube-ops-uat

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: jenkins
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/v1beta1
kind: ClusterRoleBinding
metadata:
  name: jenkins
  namespace: kube-ops-uat
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins
    namespace: kube-ops-uat

pvc的yaml文件

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: kube-ops-pvc-uat
  namespace: kube-ops-uat
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  # 这里用到了storgeclass,需要事先准备的,此处不多说明
  storageClassName: managed-nfs-storage-retain

按以下顺序创建k8s资源

  1. namespace
kubectl create namespace kube-ops-uat
  1. storageclass
  2. pvc
  3. deployment
  4. service
  5. rbac
  6. ingress(这个与jenkins无关,是k8s本身的内容,网上自行学习)

获取初始密码

上面创建好deployment后通过以下命令查看pod的日志,初始密码在日志中,

kubectl logs -f podname -n kube-ops-uat

日志中会有这么一行“Please use the following password to proceed to installation”,下面一行就是密码。
拿到密码后就可以通过页面登录,登录后第一个页面是让你安装一些插件,可以自行选择安全哪些插件或者是选择推荐安装的插件,建议新手选择推荐安装的,这一步有点耗时;再之后就是设置一个管理员账号和密码。然后就可以使用了.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐