使用helm部署nginx
初次使用helm,根据网上的一些教程,记录一些自己的理解。
- helm的安装
安装过程非常简单,根据以下操作即可。
wget https://get.helm.sh/helm-v3.4.1-linux-amd64.tar.gz
cd /opt/helm/
tar -xf helm-v3.4.1-linux-amd64.tar.gz
cp helm /usr/local/bin
chmod a+x /usr/local/bin/helm
(包下不来的同学可以重复执行wget,多几次就能下下来。)
- 尝试部署nginx应用
建立一个目录,目录内容结构如下:
[root@kube-master-3 nginx-hw]# tree .
.
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ └── service.yaml
└── values.yaml
- 文件讲解
[root@kube-master-3 nginx-hw]# cat Chart.yaml
apiVersion: v1
appVersion: v2.2
description: first helm
keywords:
- hw-nginx
name: nginx-hw
version: v1.0.0
(此文件是release的一些信息)
[root@kube-master-3 nginx-hw]# cat values.yaml
deployname: hw-nginx
replicaCount: 2
images:
repository: reg.harbor.com/harbor-test/nginx-node3 #这是从我自己的harbor拉取镜像
tag: v1
(此文件可以说是定义的了一些变量,可以在后面的文件进行引用)
[root@kube-master-3 nginx-hw]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.deployname }}
labels:
app: hw-nginx
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: hw-nginx
template:
metadata:
labels:
app: hw-nginx
spec:
containers:
- name: hw-nginx
image: {{ .Values.images.repository }}
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
(此文件中使用到的变量就是values.yaml定义的变量)
[root@kube-master-3 nginx-hw]# cat templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: hw-nginx
namespace: default
spec:
type: NodePort
selector:
app: hw-nginx
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30001
protocol: TCP
(建立一个svc,跟写k8s的yaml文件是一样的)
- 使用helm创建nginx服务
试运行
helm install --dry-run --debug hw-nginx-f .
【hw-nginx-f是release名字,.表示当前目录】
运行效果
[root@kube-master-3 nginx-hw]# helm install --dry-run --debug hw-nginx-f .
install.go:172: [debug] Original chart version: ""
install.go:189: [debug] CHART PATH: /opt/helm-charts/nginx-hw
NAME: hw-nginx-f
LAST DEPLOYED: Mon Mar 15 15:58:15 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
deployname: hw-nginx
images:
repository: reg.harbor.com/harbor-test/nginx-node3 #这是我自己搭建的harbor
tag: latest
replicaCoutn: 2
HOOKS:
MANIFEST:
---
# Source: nginx-hw/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: hw-nginx
namespace: default
spec:
type: NodePort
selector:
app: hw-nginx
ports:
- name: http
port: 80
targetPort: 80
nodePoer: 30001
protocol: TCP
---
# Source: nginx-hw/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hw-nginx
labels:
app: hw-nginx
spec:
replicas:
selector:
matchLabels:
app: hw-nginx
template:
metadata:
labels:
app: hw-nginx
spec:
containers:
- name: hw-nginx
image: reg.harbor.com/harbor-text/nginx-node3:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
(试运行会列出所有相关的文件)
部署并查看
[root@kube-master-3 nginx-hw]# helm install hw-nginx-f .
NAME: hw-nginx-f
LAST DEPLOYED: Mon Mar 15 15:58:55 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@kube-master-3 nginx-hw]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
hw-nginx-f default 1 2021-03-15 15:58:55.298302751 +0800 CST deployed nginx-hw-v1.0.0 v2.2
[root@kube-master-3 nginx-hw]# kubectl get po
NAME READY STATUS RESTARTS AGE
hw-nginx-85c74459b6-lkbmc 1/1 Running 0 65m
hw-nginx-85c74459b6-zhq9b 1/1 Running 0 65m
如果需要改变部署的文件,改变之后,可以用以下命令进行更新
helm upgrade hw-nginx-f .
部署完毕之后就可以根据svc提供的端口进行访问
[root@kube-master-3 nginx-hw]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hw-nginx NodePort 10.100.26.124 <none> 80:30001/TCP 123m
[root@kube-master-3 nginx-hw]# curl 192.168.186.13:30001 -I
HTTP/1.1 200 OK
Server: nginx/1.19.6
Date: Mon, 15 Mar 2021 10:02:43 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 15 Dec 2020 13:59:38 GMT
Connection: keep-alive
ETag: "5fd8c14a-264"
Accept-Ranges: bytes
初次体验helm完毕。