K8s deploys services using Jenkins

Keywords: Linux jenkins Maven yum Docker


1. Install Jenkins

#Preparing the Java environment
tar xvf  jdk-8u231-linux-x64.tar.gz -C /usr/local/
cd /usr/local/
ln -s jdk1.8.0_231 java

cat  /etc/profile.d/java
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

source  /etc/profile.d/java
java  -version

yum -y install wget
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum --showduplicates list jenkins | expand    #View the version you need to install
yum -y install jenkins    #Install the latest by default

vim /etc/sysconfig/jenkins    #Modify the Jenkins configuration file to suit your needs

start-up

systemctl  start  jenkins

ss -anutlp |grep 8080    #Check to see if it's started

2. web Interface Settings


View Administrator Password:

cat  /var/lib/jenkins/secrets/initialAdminPassword

Select the recommended plug-ins here, and select them according to your needs.

Installation time is a bit long, if installation fails, click reinstall

Create your Administrator account, where I log in with the admin account, if the password is the same as the password I used to start cat.


Set admin user's password from new.

3. Install Plugins

Plugins to download are: docker-build-step, docker, Maven Integration

4. Configuring the environment

4.1 Install git

yum  -y  install  git

4.2 Install maven

wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tar xvf apache-maven-3.6.3-bin.tar.gz  -C  /usr/local
ln -s apache-maven-3.6.3 maven

cat  /etc/profile.d/maven
export MAVEN_HOME=/usr/local/maven
export PATH=$MAVEN_HOME/bin:$PATH

source  /etc/profile.d/maven
mvn  --version

4.3 Install docker

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install -y docker-ce-18.06.1.ce-3.el7
systemctl start docker
systemctl status docker

4.4 Remark: I am using a private service here and need to configure the setting s file, which is placed under / opt/

5. Configure Global Environment

5.1 Configure docker


Pull to the bottom and choose to create a cloud, set the name, and url

5.2 Configure maven, jdk, git

Save when configuration is complete!

6. Configuration Items

Choose to build a maven project


Select git

Here you need to create a user who remotely clones the code. This user must exist in your gitlab code user group and have clone code privileges

Then select the user you just created. The production environment is not configured to build automatically and needs to configure itself

Dckerfile File

FROM registry.hello.com/jdk:1.8_232    #Here is my private jdk image
ADD ./target/app.jar  app.jar
CMD  ["java  -jar app.jar  -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap"]

#Complete command, modify according to your environment, make an example, see the use of kebectl set image command

ssh root@192.16.6.89 "kubectl set image deployment/hello hello=registry.cn-hangzhou.aliyuncs.com/helloworld/hello:${BUILD_NUMBER} -n master"

Note: This is the Jenkins user who executes the command and will fail Host key verification. The Jenkins user is required to create the key

Solution:

After installing jenkins, the system generated jenkins, a normal user, but in / etc/passwd, his shell is / bin/false, so he can't log on to the system and has no home directory; first, we modified his login permissions, changed / bin/false to / bin/bash, switched to Jenkins user, su

- jenkins, whose terminal shows -bash-4.2$

jenkins:x:997:995:Jenkins Automation Server:/var/lib/jenkins:/bin/bash

Generate jenkins user key pair

[root@bogon .ssh]# su - jenkins
-bash-4.2$ ssh-keygen -t rsa
//Enter

-bash-4.2$ ssh-copy-id -i /var/lib/jenkins/.ssh/id_rsa.pub root@192.168.75.12<k8s colony master Node's ip>

Use sudo to elevate common user privileges

Use viduso to modify files, commas between commands and commands, and visudo-c to check for syntax errors.

visudo
root    ALL=(ALL)       ALL  #Original
jenkins ALL=(ALL)       NOPASSWD:/usr/bin/ssh,/usr/bin/rsync #New

Finally you can execute it in jenkins using the command command

After successful build

Posted by wiseoleweazel on Thu, 09 Jan 2020 11:03:58 -0800