An overview,

Recently, I am studying to realize a set of DevOPS process based on K8S. Since it is troublesome to build a set of K8S cluster, I plan to use Minikube to realize the whole set of DevOPS process on my local Windows. Here I record the whole practice process, hoping to provide some reference for students who need it, and also for myself to refer to it in the future.

Minikube official address: minikube sigs. K8s. IO/docs/start /

Runtime environment

windows 10

Minikube 1.18.1

Kubernetes 1.20.2

Install minikube

Minikube is the local Kubernetes dedicated to making Kubernetes easy to learn and develop. All you need is a Docker (or similarly compatible) container or virtual machine environment with a single command: minikube start to start a Kubernetes cluster locally.

1. Conditions for running minikube

  • Two or more cpus
  • 2GB of available memory
  • 20GB of available disk space
  • The network connection
  • Container or virtual machine manager, such as Docker, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMWare

Description: Minikube provides cross-platform build K8S capability, support MAC, Linux, Windows platforms, each platform also support a variety of driver architecture, Windows support Docker, Hyper-V, virtualBox, etc. Since Hyper-V is already built into Win10, hyper-V is selected here.

2. Start Hyper-V in Windows

Hyper-VHyper-v is a native VM manager built into modern Microsoft Windows. It can only be started on a 64-bit version of Windows 10 Enterprise, Professional, or Education. I use Windows 10 Professional here.Open the PowerShell console as an administrator and run the following command:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
Copy the code

If Hyper-V is not previously active, it needs to be restarted. I already have it on, so as shown below, it shows online, and I don’t need to restart it.

3. Download minikube

Download and runWindows installer After the installation is complete, search CMD and open it as an administrator

4. Start minikube

Using the following command to start minikube will cause some images to fail to be pulled, then look at the successful run command below

minikube start --driver=hyperv --registry-mirror=https://registry.docker-cn.com,https://shraym0v.mirror.aliyuncs.com --embed-certs=true --image-mirror-country=cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

Copy the code

Parameter Description You can view detailed descriptions of other parameters by running minikube start –help. The preceding parameters are described here

  • minikube start: Starts a local single-node Kubernetes cluster.
  • --driver=hyperv : Specifies that the driver is Hyperv. The default is one of automatic detection (VirtualBox, VMwareFusion, HyperV, vmware, Docker, SSH).
  • --registry-mirror=https://registry.docker-cn.com: use the domestic mirror address to improve the speed of pulling mirror, you can set multiple use.Divide can.
  • --embed-certs=true: If true, the certificate will be embedded in KubeconFig. The default is false, and the certificate file will be read in KubeconFig as an absolute path.
  • --image-mirror-country=cn: Country/region code of the image that needs to be used, left blank to use the global code, for mainland Chinese users, set tocn.
  • --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers: Sets the repository used to pull the images required by the Kubernetes cluster, if inaccessiblegcr.ioYou can set it to” Auto “so that Minikube automatically selects the mirror repository that you can access. This parameter can be set for users in mainland Chinaregistry.cn-hangzhou.aliyuncs.com/google_containersBut I set this parameter and some mirrors cannot be pulled.

Description: Through the above command start minikube, there will be some mirror cannot pull problem, that is to say, mirror registry.cn-hangzhou.aliyuncs.com/google_containers warehouse many image does not exist, through constant testing, Using the following command to start and pull the mirror normally, so we do not need to set these two parameters: – image – mirror – country = cn and image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

The command for successfully starting minikube is as follows:

minikube start --driver=hyperv --registry-mirror=https://registry.docker-cn.com,https://shraym0v.mirror.aliyuncs.com --embed-certs=true
Copy the code

5. Verify minikube

Run the following command to check the status of minikube

C:\WINDOWS\system32>minikube status minikube type: Control Plane host: Running kubelet: Running apiserver: Running kubeconfig: Configured timeToStop: The kubectl tool C:\WINDOWS\system32> minikubectl get node NAME STATUS ROLES AGE VERSION minikube 11 m v1.20.2 Ready the control plane, the masterCopy the code

Deploy an Nginx and quickly experience Minikube

C:\WINDOWS\system32>kubectl create deployment nginx --image=nginx deployment.apps/nginx created C:\WINDOWS\system32>kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-z7xzh 1/1 Running 0 33s C:\WINDOWS\system32>kubectl expose deployment nginx --type=NodePort --port=80 service/nginx exposed C:\WINDOWS\system32>minikube service nginx |-----------|-------|-------------|----------------------------| | NAMESPACE | NAME | TARGET PORT | URL | |-----------|-------|-------------|----------------------------| | default | nginx | 80 | http://172.23.130.60:31593 | | -- -- -- -- -- -- -- -- -- -- - | | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | * are by default browser to open the service default/nginx... C:\WINDOWS\system32>Copy the code

The default browser will open automatically, as shown below:

Run the following command to start the DASHBOARD of K8S

C:\WINDOWS\ System32 > Minikube Dashboard - Using image kubernetesui/dashboard: v2.1.0 - Using image kubernetesui/metrics - scraper: 1.0.4 * is verified on the operation of the dashboard... * Launching proxy ... * Verifying proxy health... * Opening in http://127.0.0.1:61589/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ your default browser...Copy the code

As shown below:

3. Install Jenkins

Here I use YAML to deploy Jenkins, and create PV and PVC to persist Jenkins data, so create three files:

  • jenkins-pvc.yaml: Sets the Jenkins data persistence mode.
  • jenkins-rbac.yaml: Set Jenkins user access permissions.
  • jenkins-deploy.yaml: Create Jenkins’ Deployment and service.

The contents of the jenkins-PVC. yaml file are as follows:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pvc
  namespace: devops
spec:
  accessModes:
  - ReadWriteMany
  If there is a default storageClass in the cluster that meets the requirements, you do not need to configure storageClass
  storageClassName: standard
  resources:
    requests:
      storage: 5Gi
Copy the code

Note: The K8S cluster created using minikube has created a hostPath-based storageClass by default. Run the following command to view the storageClass

C:\windows\system32>kubectl get sc
NAME                 PROVISIONER                RECLAIMPOLICY   VOLUMEBINDINGMODE   >ALLOWVOLUMEEXPANSION   AGE
standard (default)   k8s.io/minikube-hostpath   Delete          Immediate           false                  98m
Copy the code

StorageClass automatically creates PVS and binds PVCS to PVCS, so we do not need to create PVS ourselves.

The contents of the jenkins-rbac.yaml file are as follows:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-sa
  namespace: devops

---
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: devops

Copy the code

The contents of the Jenkins -deploy.yaml file are as follows:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: devops 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccount: jenkins-sa
      containers:
      - name: jenkins
        image: jenkins/jenkins:latest
        imagePullPolicy: IfNotPresent
        env:
        - name: JAVA_OPTS
          value: -Duser.timezone=Asia/Shanghai
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        - containerPort: 50000
          name: agent
          protocol: TCP
        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
        volumeMounts:
          - name: jenkinshome
            mountPath: /var/jenkins_home
      volumes:
        - name: jenkinshome
          persistentVolumeClaim:
            claimName: jenkins-pvc

---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: devops 
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: NodePort
  ports:
  - name: web
    port: 8080
    targetPort: web
    
    
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-agent
  namespace: devops 
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: ClusterIP
  ports:
  - name: agent
    port: 50000
    targetPort: agent

Copy the code

Deploy Jenkins using the following command

  1. Create a Devops namespace
C:\WINDOWS\system32>kubectl create namespace devops
namespace/devops created
Copy the code
  1. Execute the following command to start Jenkins
kubectl apply -f jenkins-pvc.yaml
kubectl apply -f jenkins-rbac.yaml
kubectl apply -f jenkins-deploy.yaml
Copy the code
  1. Use the minikube service command to provide the browser access address
C:\WINDOWS\system32>minikube service jenkins -n devops |-----------|---------|-------------|----------------------------| | NAMESPACE | NAME | TARGET PORT | URL | |-----------|---------|-------------|----------------------------| | devops | jenkins | web/8080 | http://172.23.130.60:30002 | | -- -- -- -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | * are by default browser to open the service devops/jenkins...Copy the code

4. Run the following command to view the initial password for logging in to Jenkins

C:\WINDOWS\system32>kubectl get pod -n devops
NAME                       READY   STATUS    RESTARTS   AGE
jenkins-6bb66dcf88-2c4tv   1/1     Running   0          9m59s

C:\WINDOWS\system32>kubectl logs -f jenkins-6bb66dcf88-2c4tv -n devops
Copy the code

Check Jenkins initial password as shown below:Choose custom plug-in installation, because go back to the official website to download plug-ins, the download is slow, and many plug-ins we do not needDeselect the recommended plug-inCreate an administrator accountStart using Jenkins

Jenkins download plugin set domestic source

Install the following plug-ins

4. Configure K8S in Jenkins to achieve CI/CD

1. Configure k8S

  • Select [Node Management] -> [Configure Clouds]

  • After installing the Kubernetes plugin, you can see the image below, adding a Kubernetes Cloud

  • Configure the API Server that connects to K8S

  • Create a pod template

  • Add the first container JNLP (jnLP-slave)

  • Add a second container docker (since the docker client is needed to build the image in the pipeline, this image provides the Docker client)

  • Add a third container, Maven (the image used here is: registry.cn-beijing.aliyuncs.com/acs-sample/jenkins-slave-maven:3.3.9-jdk-8-alpine, or you can use a custom Maven image.)

  • The data volume Settings for the POD template are shown below:

Create docker push image to secret

Command is as follows: kubectl create secret generic my – secret – from – the file = / root/docker/config. Json

Note: Kubectl create secret docker-registry my-secret –docker-server=DOCKER_REGISTRY_SERVER –docker-username=DOCKER_USER –docker-password=DOCKER_PASSWORD –docker-email=DOCKER_EMAIL An error was reported indicating that docker did not log in to the private repository. The reason was found because I used docker-Registry to create my-secret. When I changed it to generic, it was ok.

  • [Manage Credentials] -> [Jenkins] -> [Global Credentials] -> [Add Credentials]

Configure kubeconFig to access K8S, used in pipelinekubernetesDeploy“Will be used

  • [Manage Credentials] -> [Jenkins] -> [Global Credentials] -> [Add Credentials]

Set the user name and password for logging in to the private mirror vault

5. Use Jenkins CI/CD demo

  • Create a new task, Devops-demo

  • The devops-Demo task configuration is as follows:

1, specify docker private repository address2. Specify a project name3. Specify the namespace to deploy4. Select the branch to build

5. Task pipelining configuration

Note: The sample repository code addresses used here are as follows: gitee.com/peterwd/dev…

Three files need to be added to the repository to implement this Devops process: Jenkinsfile, Dockerfile, and deployment.yaml files, the contents of which can be viewed by clicking on the repository.

  • Save the configuration and build as shown below

  • The effects of a successful build are as follows

The packaging phase takes a long time because dependencies need to be downloaded at each build, because they will be destroyed when the slave is finished. The default downloaded dependencies are in the slave container and will disappear when the slave container is destroyed, so we should persist the downloaded dependencies. Here is how to do this:

1. Persist downloaded dependencies to the host using the following configuration: In the Maven container, dependency packages are stored by default/root/.m2/repositoryDirectory, so we can mount the host’s specified directory into this directory:You can log in to the host machine to see that the downloaded dependencies have been persisted to the corresponding directory on the host machine:

# login to minikube node in C: \ Windows \ system32 > minikube SSH _ _ _ _ () () ___ ___ ___ (_) (_) | | / ') _ _ | | _ __ / '_ ` _ ` \ | | /' _ ` \ | | |. < () () | '_ ` \ /' __ ` \ | () () | | | | () | | | | | \ ` \ | (_) | | | _) (___)/(_) (_) (_) (_) (_) (_) (_) (_) (_)`\___/'(_,__/'`\____) $ cd /tmp/maven/repository/ $ ls -al total 0 drwxr-xr-x 16 root root 320 Mar 15 10:25 . drwxr-xr-x 3 root root 60 Mar 15 10:21 .. drwxr-xr-x 3 root root 60 Mar 15 10:24 backport-util-concurrent drwxr-xr-x 3 root root 60 Mar 15 10:22 ch drwxr-xr-x 3 root root 60 Mar 15 10:23 classworlds drwxr-xr-x 5 root root 100 Mar 15 10:24 com drwxr-xr-x 3 root root 60 Mar 15 10:23  commons-cli drwxr-xr-x 3 root root 60 Mar 15 10:25 commons-codec drwxr-xr-x 3 root root 60 Mar 15 10:25 commons-lang drwxr-xr-x 4 root root 80 Mar 15 10:25 commons-logging drwxr-xr-x 8 root root 160 Mar 15 10:22 io drwxr-xr-x 3 root root  60 Mar 15 10:22 jakarta drwxr-xr-x 3 root root 60 Mar 15 10:23 junit drwxr-xr-x 3 root root 60 Mar 15 10:24 log4j drwxr-xr-x 3 root root 60 Mar 15 10:25 net drwxr-xr-x 13 root root 260 Mar 15 10:23 org $Copy the code

2. Use THE PVC persistent MAVE dependency package to perform the configuration as shown in the figure below:

  • To persist maven dependencies, re-build takes as follows:

Problems encountered

1. The minikube startup fails

* Preparing Kubernetes V1.20.2 in Docker 20.10.3... | E0313 13:19:52. 379165, 33644 start. Go: 99] Unable to get the host IP: No virtual switch found X Exiting due to GUEST_START: Failed to setup kubeconfig: No virtual switch foundCopy the code

As shown below: The cause of the errorThe first time the installation failed due to insufficient memory on the computer, this error is reported when starting minikube again.The solutionRun the following command to delete all the minikube clusters and restart the cluster.

minikube delete --all
Copy the code

2. Jenkins plug-in download failed

Some plug-ins fail to load due to lack of dependencies. To restore the functionality provided by these plug-ins, you need to fix the problems and restart Jenkins. Dependency Errors: SSH Credentials Plugin (1.18.2) Jenkins (2.282) or higher required These plug-ins cannot be loaded due to one or more of the above errors. The plugin will load again after the fix. The solutionUpgrade Jenkins to the suggested version and can be directly modifiedjenkins-deploy.yamlIn the fileimage: jenkins/jenkins:latestforImage: Jenkins/Jenkins: 2.283

See article Minikube for quick construction of K8S

Deploy Jenkins in Kubernetes and use it simply

Jenkins and Kubernetes – mysterious agents in the cloud

K8s Study notes StorageClass+NFS