K8s Network之Ingress
PDF路径:
链接:https://pan.baidu.com/s/17DxUD8KN7pU1UKIR1Ejemg
提取码:dwf5一、如果项目需要修改某些代码,怎么办?
(1)重新打成jar包
(2)重新制作Dockerfile文件
(3)重新push image
(4)重新拉取镜像运行
二、准备环境
1.安装配置Git
(1)下载安装git[略]
(2)配置并生成ssh_key
git config --global user.name "hiourearth"
git config --global user.email "hiourearth@gmail.com"
ssh-keygen -t rsa -C "hiourearth@gmail.com"
(3)打开C:\Users\Jack\.ssh,查看id_rsa.pub文件中的值
2.搭建并配置Gitlab
refer:https://about.gitlab.com/install/#centos-7 下载安装极狐GitLab 免费版
(1)说明
安装gitlab的机器至少要有4G的内存,因为gitlab比较消耗内存
(2)安装必要的依赖并配置
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld
(3)添加gitlab仓库地址
# 创建gitlab-ce.repo文件
cd /etc/yum.repos.d/
vim gitlab-ce.repo
# 文件内容为
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
(4)设置gitlab域名并安装
sudo EXTERNAL_URL="https://gitlab.mars.com" yum install -y gitlab-ce
(5)配置windows的hosts文件
(6)访问:https://gitlab.mars.com
(7)查看root账号的密码,后面可以修改一下
cat /etc/gitlab/initial_root_password
(8)gitlab常见操作
(1)vim /etc/gitlab/gitlab.rb 找到对应修改内容
(2)gitlab-ctl reconfigure 重新加载一下配置
(3)gitlab-ctl status 查看gitlab状态
(4)sudo gitlab-ctl stop 停止gitlab
(5)sudo gitlab-ctl start 开启gitlab
3.项目关联Gitlab
(1)将公钥上传到gitlab:右上角->Preferences->SSH Keys
(2)在gitlab上创建名称为“springboot-demo”的project
(3)将本地项目push到gitlab上
# 使用terminal打开项目的根目录
git init
git add .
git commit -m "first commit"
git remote add jack-gitlab git@gitlab.jack.com:root/springboot-demo.git
git remote -v
git push -u jack-gitlab master
4.安装配置Jenkins
官网:https://jenkins.io/ 下载最新版需要jdk环境jdk11 选用节点:192.168.0.53
(1)下载jenkins.war
(2)运行jenkins
nohup java -jar jenkins.war --httpPort=8888 & lsof -i:8888 tail -f nohup.out
(3)浏览器访问:192.168.0.53:8888
密码: cat /root/.jenkins/secrets/initialAdminPassword
然后会等待一会,包括镜像拉取以及插件安装[选择推荐的插件]
(4)新建用户
(5)配置Jenkins URL
(6) 配置java maven和git等
Dashboard-> Manage Jenkins-> Global Tool Configuration
# 配置一下w2这台机器的git信息
git config --global user.name "hiourearth"
git config --global user.email "hiourearth@gmail.com"
ssh-keygen -t rsa -C "hiourearth@gmail.com"
cat /root/.ssh/id_rsa.pub
# 将公钥上传到gitlab上
5.准备Docker Hub和K8s集群
(1)Docker Hub:直接使用阿里容器镜像仓库,在w2这台机器上登录一下
sudo docker login --username=hiourearth registry.cn-hangzhou.aliyuncs.com
(2)K8s集群:就用之前搭建的3台机器组成的K8s集群
6.梳理一下目前的环境
Gitlab: git@gitlab.jack.com:root/springboot-demo.git
Jenkins: 192.168.2.53:8888
Docker hub: hiourearth
K8s cluster: 192.168.0.51 52 53
7.Jenkins拉取项目
(1)创建Jenkins的Task
(2)在w2上配置hosts: vim /etc/hosts
192.168.0.8 gitlab.jack.com
# 测试git是否配置成功
ssh -T git@gitlab.jack.com
(3)编写拉取代码的脚本,然后点击“保存”
node { def mvnHome stage('Git Pull') { // for display purposes
git 'git@gitlab.jack.com:root/springboot-demo.git'
} }
(4)来到“Dashboard”,并点击执行按钮
(5)到w2这台机器上,观察/root/.jenkins/workspace目录,看看项目是否拉取成功
cat /root/.jenkins/workspace/springboot-
demo/src/main/java/com/jack/springbootdemo/SpringbootDemoApplication.java
8.Jenkins Maven打包
(1)新增一个脚本语句
node { def mvnHome
stage('Git Pull') { // for display purposes
git 'git@gitlab.jack.com:root/springboot-demo.git'
}
stage('Maven Build') {
sh "mvn clean package"
}
}
(2)观察worspace目录下是否生成target文件
9.构建和push镜像
(1)来到w2上的workspace目录:cd /root/.jenkins/workspace
(2)在scripts目录下准备一个文件,名称为springboot-demo-build-image.sh
mkdir /root/.jenkins/workspace/scripts/
vim /root/.jenkins/workspace/scripts/springboot-demo-build-image.sh
(3)编辑sh文件
# 进入到kubernetes-demo目录
cd ../springboot-demo
# 编写Dockerfile文件
cat Dockerfile FROM openjdk:8-jre-alpine COPY target/springboot-demo-0.0.1- SNAPSHOT.jar springboot-demo.jar ENTRYPOINT ["java","-jar","springboot-demo.jar"] EOF
echo "Dockerfile created successfully!"
# 基于指定目录下的Dockerfile构建镜像
docker build -t registry.cn-hangzhou.aliyuncs.com/jack-kubernetes/springbootdemo- image .
# push镜像,这边需要阿里云镜像仓库登录,记得提前在w2上登录一下
docker push registry.cn-hangzhou.aliyuncs.com/jack-kubernetes/springboot-demoimage
(4)踩坑经验
# 01 文件权限
/root/.jenkins/workspace/scripts/springboot-demo-build-image.sh: Permission denied # 解决
chmod +x /root/.jenkins/workspace/scripts/springboot-demo-build-image.sh
# 02 docker没有运行
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# 解决
systemctl start docker systemctl enable docker
# 03 push权限
docker login --username=hiourearth registry.cn-hangzhou.aliyuncs.com
(5)jenkins上添加脚本
node { def mvnHome
stage('Git Pull') { // for display purposes
git 'git@gitlab.jack.com:root/dockerfile-demo.git'
}
stage('Maven Build') {
sh "mvn clean package"
}
stage('Build Image') {
sh "/root/.jenkins/workspace/scripts/springboot-demo-build-image.sh"
}
}
10.K8s拉取镜像并运行
(1)在/root/.jenkins/workspace/scripts/目录下创建springboot-demo.yaml
# 以Deployment部署Pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-demo
spec:
selector:
matchLabels:
app: springboot-demo
replicas: 1
template:
metadata:
labels:
app: springboot-demo
spec:
containers: - name: springboot-demo
image: registry.cn-hangzhou.aliyuncs.com/jack-kubernetes/springbootdemo-image
ports: - containerPort: 8080
---
# 创建Pod的Service
apiVersion: v1
kind: Service
metadata:
name: springboot-service
spec:
ports: - port: 80
protocol: TCP
targetPort: 8080
selector:
app: springboot-demo
---
# 创建Ingress,定义访问规则,一定要记得提前创建好nginx ingress controller
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: springboot-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules: - host: springboot.jack.com
http:
paths: - pathType: Prefix
path: /
backend:
service:
name: springboot-service
port:
number: 80
(2)在/root/.jenkins/workspace/scripts/目录创建K8s-deploy-springboot-demo.sh
kubectl delete -f /root/.jenkins/workspace/scripts/springboot-demo.yaml
kubectl apply -f /root/.jenkins/workspace/scripts/springboot-demo.yaml
echo "K8s deploy success!"
(3)踩坑经历
# 01 权限
/root/.jenkins/workspace/scripts/K8s-deploy-springboot-demo.sh: Permission denied
# 解决
chmod +x /root/.jenkins/workspace/scripts/K8s-deploy-springboot-demo.sh
# 02 w2执行不了kubectl
切换到master上,cd ~ ---> cat .kube/config --->复制内容
切换到worker02上 cd ~ ---> 新建文件 .kube/config --->粘贴内容
(4)增加Jenkins中的脚本
node { def mvnHome
stage('Git Pull') { // for display purposes
git 'git@gitlab.jack.com:root/springboot-demo.git'
}
stage('Maven Build') {
sh "mvn clean package"
}
stage('Build Image') {
sh "/root/.jenkins/workspace/scripts/springboot-demo-build-image.sh"
}
stage('K8S Deploy') {
sh "/root/.jenkins/workspace/scripts/K8s-deploy-springboot-demo.sh"
}
}
(5)jenkins构建,并查看K8s集群中image和资源
kubectl get pods
kubectl get deploy
kubectl get service
(6)浏览器访问
http://springboot.mars.com/K8s