目录

一、实验

1.环境

2.K8S master节点部署HELM3

3.K8S master节点安装git

4. Harbor镜像确认

5. HELM 手动完成前端项目应用发布与回滚

6.代码上传到GitLab

二、问题

1.Ingress中 path 的类型有何区别

2. HELM创建项目报错


 

 

一、实验

1.环境

(1)主机

表1  主机

主机架构版本IP备注
master1K8S master节点1.20.6192.168.204.180

jenkins slave

(从节点)

 helm3.6.0  
 git1.8.3.1  
node1K8S node节点1.20.6192.168.204.181 
node2K8S node节点1.20.6192.168.204.182 
jenkins

 jenkins主节点      

2.414.2192.168.204.15:8080

 gitlab runner

(从节点)

 

harbor私有仓库

1.2.2192.168.204.15 
gitlabgitlab 主节点     12.10.14192.168.204.8:82

jenkins slave

(从节点)

 sonarqube9.6192.168.204.8:9000 

2.K8S master节点部署HELM3

(1)Helm版本与K8S集群兼容

ee70de7fccd441c2866678f1b28ca9da.png

(2)查看K8S集群状态

# kubectl get node

980e994461ea419187ac8739bfd8c263.png

(3)策略

当前K8S 集群为1.20.6版本,HELM 3.6.x 版本可以兼容。

所以选择3.6.0版本。

(4)部署


1)安装 helm 
//下载二进制 Helm client 安装包
helm-v3.6.0-linux-amd64.tar.gz
 
tar -zxvf helm-v3.6.0-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
helm version
 
//命令补全
source <(helm completion bash)
 
2)使用 helm 安装 Chart
//添加常用的 chart 仓库,
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo add incubator https://charts.helm.sh/incubator

3) 更新 charts 列表
helm repo update
helm repo list          

安装

11241a72251a4e6e87f5ce7f5de14783.png

使用 helm 安装 Chart

ff517b7d44b541dd8a9cf3ee97d7795c.png6f3e168cb7244272ac568207c80ef910.png

 

3.K8S master节点安装git

(1) 安装

1)安装
# yum install -y git

2)查看版本
# git --version

3)全局设置(初始邮箱为admin@example.com)
# git config --global user.name "root"
# git config --global user.email "594372682@qq.com"

安装
b640f096c0fd45a6b98c99952e76a47f.png5f7570d8f9324ebda524d8bb8d38ef26.png

查看版本

9b8f10e92ebe4249bdc147f7432d85f6.png

全局设置
da065a4627534b6494dd1a898ee55348.png

 

4. Harbor镜像确认

(1)查看前端项目镜像

8b099f0c34414d53932441abc12ff585.png

(2)确认如下镜像作为helm项目使用的镜像

 192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.6

 

5. HELM 手动完成前端项目应用发布与回滚

(1)构建项目

# helm create devops03-devops-ui

525e34555c1a475ebab0db8ab6798c2c.png

(2)安装tree

# yum install -y tree

554e30074aab4cd5a16030d96383c319.pngb86ba730399d453490cbb6a25b230898.png

(3) 查看目录

# tree

222615993b6d405eb2e004127348ade1.png

(4)修改values.yaml 

# vim values.yaml 

指定镜像仓库、镜像版本为RELEASE-1.1.6、开启ingress


  1 # Default values for devops03-devops-ui.
  2 # This is a YAML-formatted file.
  3 # Declare variables to be passed into your templates.
  4 
  5 replicaCount: 2
  6 
  7 image:
  8   repository: 192.168.204.15/devops03/devops03-devops-ui
  9   pullPolicy: IfNotPresent
 10   # Overrides the image tag whose default is the chart appVersion.
 11   tag: "RELEASE-1.1.6"
 12 
 13 imagePullSecrets: []
 14 nameOverride: ""
 15 fullnameOverride: ""
 16 
 17 serviceAccount:
 18   # Specifies whether a service account should be created
 19   create: true
 20   # Annotations to add to the service account
 21   annotations: {}
 22   # The name of the service account to use.
 23   # If not set and create is true, a name is generated using the fullname template
 24   name: ""
 25 
 26 podAnnotations: {}
 27 
 28 podSecurityContext: {}
 29   # fsGroup: 2000
 30 
 31 securityContext: {}
 32   # capabilities:
 33   #   drop:
 34   #   - ALL
 35   # readOnlyRootFilesystem: true
 36   # runAsNonRoot: true
 37   # runAsUser: 1000
 38 
 39 service:
 40   type: ClusterIP
 41   port: 80
 42 
 43 ingress:
 44   enabled: true
 45   className: ""
 46   annotations:
 47     kubernetes.io/ingress.class: nginx
 48     # kubernetes.io/tls-acme: "true"
 49   hosts:
 50     - host: devops03-devops-ui.devops.com
 51       paths:
 52         - path: /
 53           pathType: ImplementationSpecific
 54   tls: []
 55   #  - secretName: chart-example-tls
 56   #    hosts:
 57   #      - chart-example.local
 58 
 59 resources: {}
 60   # We usually recommend not to specify default resources and to leave this as a conscious
 61   # choice for the user. This also increases chances charts run on environments with little
 62   # resources, such as Minikube. If you do want to specify resources, uncomment the following
 63   # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
 64   # limits:
 65   #   cpu: 100m
 66   #   memory: 128Mi
 67   # requests:
 68   #   cpu: 100m
 69   #   memory: 128Mi
 70 
 71 autoscaling:
 72   enabled: false
 73   minReplicas: 1
 74   maxReplicas: 100
 75   targetCPUUtilizationPercentage: 80
 76   # targetMemoryUtilizationPercentage: 80
 77 
 78 nodeSelector: {}
 79 
 80 tolerations: []
 81 
 82 affinity: {}

(5) 另开一个终端用watch命令观察pod变化

# watch -n 1 "kubectl get pod -n devops03"

ba807433ad4d4e6595b7f743a006a154.png

1c4229ba19854f56b8274ae582ebe903.png

(6)外部测试访问(当前版本为1.1.6)

# curl http://devops03-devops-ui.devops.com:31291

bf9081c1d872440eb5ee2cf888f5a80f.png

 (7)删除已有命名空间

# kubectl delete ns devops03

f7be2b68bf7846b98f12ee39433ff6ee.png

(8)观察pod变化b79da2332628480da53ed7685b52abf9.png

7dffc5a376034fdc9e57b4b4176bbc01.png

(9)重新创建命名空间并查看资源

# kubectl create ns devops03

# kubectl get all -n devops03

45ab26810dc74e189d56c53d6177c3df.png

bdd460b360864dedbb7d37228427483c.png

(7)运行项目

# helm install devops03-devops-ui -n devops03 devops03-devops-ui/

296b935623f14c7c93a3958f3b397e2d.png

(8)观察pod变化

4c8db84159e74520a7f19e056ab36bfa.png

af629e3adff145968197b6dad2b4b324.png

817fb95075cc4ccab5d86d8bc42bc25e.png

(9)外部测试访问(当前版本为1.1.6)

# curl http://devops03-devops-ui.devops.com:31291

4cfd3cf26b964d1186e0a31bac74f35d.png

(10)修改values.yaml 

# vim values.yaml 

指定镜像仓库、镜像版本为RELEASE-1.1.5、开启ingress


  1 # Default values for devops03-devops-ui.
  2 # This is a YAML-formatted file.
  3 # Declare variables to be passed into your templates.
  4 
  5 replicaCount: 2
  6 
  7 image:
  8   repository: 192.168.204.15/devops03/devops03-devops-ui
  9   pullPolicy: IfNotPresent
 10   # Overrides the image tag whose default is the chart appVersion.
 11   tag: "RELEASE-1.1.6"
 12 
 13 imagePullSecrets: []
 14 nameOverride: ""
 15 fullnameOverride: ""
 16 
 17 serviceAccount:
 18   # Specifies whether a service account should be created
 19   create: true
 20   # Annotations to add to the service account
 21   annotations: {}
 22   # The name of the service account to use.
 23   # If not set and create is true, a name is generated using the fullname template
 24   name: ""
 25 
 26 podAnnotations: {}
 27 
 28 podSecurityContext: {}
 29   # fsGroup: 2000
 30 
 31 securityContext: {}
 32   # capabilities:
 33   #   drop:
 34   #   - ALL
 35   # readOnlyRootFilesystem: true
 36   # runAsNonRoot: true
 37   # runAsUser: 1000
 38 
 39 service:
 40   type: ClusterIP
 41   port: 80
 42 
 43 ingress:
 44   enabled: true
 45   className: ""
 46   annotations:
 47     kubernetes.io/ingress.class: nginx
 48     # kubernetes.io/tls-acme: "true"
 49   hosts:
 50     - host: devops03-devops-ui.devops.com
 51       paths:
 52         - path: /
 53           pathType: ImplementationSpecific
 54   tls: []
 55   #  - secretName: chart-example-tls
 56   #    hosts:
 57   #      - chart-example.local
 58 
 59 resources: {}
 60   # We usually recommend not to specify default resources and to leave this as a conscious
 61   # choice for the user. This also increases chances charts run on environments with little
 62   # resources, such as Minikube. If you do want to specify resources, uncomment the following
 63   # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
 64   # limits:
 65   #   cpu: 100m
 66   #   memory: 128Mi
 67   # requests:
 68   #   cpu: 100m
 69   #   memory: 128Mi
 70 
 71 autoscaling:
 72   enabled: false
 73   minReplicas: 1
 74   maxReplicas: 100
 75   targetCPUUtilizationPercentage: 80
 76   # targetMemoryUtilizationPercentage: 80
 77 
 78 nodeSelector: {}
 79 
 80 tolerations: []
 81 
 82 affinity: {}

修改前:

b9e27d1af0c341edbd551a24c5fb9c93.png

修改后:

9456ec598178466d8948f267c713cd3f.png

(11)更新项目

# helm upgrade devops03-devops-ui -n devops03 devops03-devops-ui/

0533c2a2bd0146958f0d12458fba6174.png

(12)观察pod变化

584ac02b96484ef38fc0cfbc072699bf.png

1f1423073d3b4bbd9383abba2aebea6a.png

a1fa364efb3f4c03bf248dde6b19def6.png

22529ac20a1846e1be91f06604b7b86a.png

5996e347953043f98f36430f91c16a08.png

(13)外部测试访问(当前版本为1.1.5)

# curl http://devops03-devops-ui.devops.com:31291

0a2b649d311944a98e132a90a15cb3b8.png

(14)查看历史版本

# helm history devops03-devops-ui -n devops03

a619675ed16c44db949de1fff4d0ad0d.png

(15)回滚指定版本

# helm rollback devops03-devops-ui 1 -n devops03

7712da6b6b6348fb92e17adf1a7ab95c.png

(16)观察pod变化

d9937cabf85a4a4bad75a3186a765602.png

80ae516b79b041e8b5eb3cbccdef903e.png

5b3b0dcc47554391a83f559d7e35f517.png

d6df60a242be4a64b29c0752ff626550.png

d23125e09397406d80c1b94788a7a0a0.png

(17)外部测试访问(当前版本为1.1.6)

# curl http://devops03-devops-ui.devops.com:31291

acb0d800a34147da8ab6ebe349628b51.png

 

6.代码上传到GitLab

(1) 将项目克隆到服务器

# git clone http://192.168.204.8:82/devops03/devops03-helm-service.git

99ddaf60094141b589edb21517b20cc2.png

(2)切换目录,移动HELM项目

# mv devops03-devops-ui/ devops03-helm-service/

7264242ea34749c1b5615edc7275732b.png

(3)将代码上传到工作区

# git add devops03-devops-ui/

259862b802b745a48f61fe7b305ed2af.png

(4)提交代码申明

# git commit -m "devops03 app"

9b53b020ede7409fba4e8d41a6f3baa5.png

(5) 将代码推送到GitLab

# git push origin master

dd4fa580e2974c0a949154671ef4725b.png

(6)GitLab查看项目

b991229c3b37407c8d252bc6009d2cfb.png

(7)目录结构

4a1ad908ba4c4b14a60b8707f92a696d.png

 

二、问题

1.Ingress中 path 的类型有何区别

(1)区别

exact: 精确匹配路径。如 path: /foo,只匹配请求路径与之完全相同的 /foo。

prefix: 前缀匹配路径。如 path: /foo,匹配请求路径以 /foo 开始的所有路径,如 /foo、/foo/bar 等。

ImplementationSpecific: 特定 Ingress controller 的匹配方式。如 Nginx Ingress controller 忽略路径类型,作为前缀匹配路径处理。

2. HELM创建项目报错

(1)报错

ae45ad93cf904bb2b9a78113c5500a41.png

(2)原因分析

格式转换错误。

(3)解决方法

修改配置文件。

修改前:

34854e7121684042a168205fed49807b.png

修改后:

a7e02f164e274e6c878b73e9e7a5604f.png

成功:

cc61d48bd8b1418ab8c86066b4c724eb.png

 

 

Logo

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

更多推荐