Kubernetes - 使用Helm 从Docker 镜像到K8s Pod操作示例
前面我们介绍到使用xxx.yaml文件,使用kubectl create -f xxx.yaml来创建一个新的Pod,但是现在大多数的kubernetes集群,是使用helm来进行镜像包管理的;
Helm架构由Helm客户端、Tiller服务器端和Chart仓库所组成;Tiller部署在Kubernetes中,Helm客户端从Chart仓库中获取Chart安装包,并将其安装部署到Kubernetes集群中。
Helm是管理Kubernetes包的工具,Helm能提供以下能力:
- 创建新的charts
- 将charts打包成tgz文件
- 与chart仓库交互
- 安装和卸载Kubernetes的应用
- 管理使用Helm安装的charts的生命周期
在Helm中,有三个需要了解的重要概念:
- chart:是创建Kubernetes应用实例的信息集合
- config:创建发布对象的chart的配置信息
- release:chart的运行实例,包含特定的config
在Helm中,有四个需要了解的重要命令:
- helm version
- helm list
- helm install -n demohelm demohelm
- helm delete --purge demohelm
简单熟悉下helm的概念和基础命令,下面我们一步一步的来创建我们的Pod;
1、我们在镜像仓库已经有我们事先push上去的images包备用,而且输入helm version 有以下正确返回:
[root ~]# helm version
Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
2、先看一下我们要准备的文件:
tree demohelm/ demohelm/ ├── Chart.yaml ├── templates │ └── demohelm.yaml └── values.yaml
①cat Chart.yaml
[root ~]# cat Chart.yaml
name: demohelm
version: "v1.0"
description: demohelm
②cat templates/demohelm.yaml
[root demohelm]# cat templates/demohelm.yaml
apiVersion: v1
kind: Service
metadata:
name: demohelm
labels:
app: demohelm
spec:
type: NodePort
sessionAffinity: ClientIP
ports:
- port: 12222
name: demohelm-port
nodePort: 31222
selector:
app: demohelm
---
apiVersion: v1
kind: ReplicationController
metadata:
name: demohelm
spec:
replicas: {{.Values.core.replicas}}
selector:
app: demohelm
template:
metadata:
name: demohelm
labels:
app: demohelm
spec:
containers:
- name: demohelm
image: {{.Values.docker.registry}}/http-server:{{.Values.demohelm.version}}
imagePullPolicy: {{.Values.docker.pullPolicy}}
# resources:
# limits:
# memory: "128Mi"
# requests:
# memory: "64Mi"
volumeMounts:
- mountPath: /home/test/daopinz
name: config-volume
# envFrom:
# - configMapRef:
# name: private-config
# - secretRef:
# name: private-secret
volumes:
- name: config-volume
emptyDir: {}
③cat values.yaml
[root demohelm]# cat values.yaml
docker:
registry: xxx-xxx:5000/docker.io
pullPolicy: Always
core:
replicas: 1
demohelm:
version: "v1.0"
3、这些文件准备好后,跳出demohelm/目录,执行helm install -n demohelm demohelm命令即可;
[root daopinz]# helm install -n demohelm demohelm
NAME: demohelm
LAST DEPLOYED: Wed Jun 12 06:26:28 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
demohelm-npwd7 0/1 ContainerCreating 0 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
demohelm NodePort xx.xxx.xx.xx <none> 12222:31222/TCP 0s
==> v1/ReplicationController
NAME DESIRED CURRENT READY AGE
demohelm 1 1 0 0s
执行helm list 可以看到新建的helm,如demohelm;
4、删除helm创建的pod;helm delete --purge demohelm
[root daopinz]# helm delete --purge demohelm
release "demohelm" deleted
over ...