发布时间:2023-05-07 15:00
前面已经完成了k8s集群环境的搭建部署,也实现了创建pod,service等服务,本文开始介绍如何在k8s集群中部署应用。这里以zabbix监控为例。
zabbix监控需要使用MySQL来存储数据
1.创建MySQL的部署文件
创建pv和pvc(用来将MySQL数据做持久化)
[root@server1 zabbix]# vim mysql_pv.yml
kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
创建MySQL配置文件
[root@server1 zabbix]# vim mysql-conf.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-conf
data:
custom.cnf: |
[mysqld]
default_storage_engine=innodb
skip_external_locking
skip_host_cache
skip_name_resolve
default_authentication_plugin=mysql_native_password
创建MySQL密码文件
[root@server1 zabbix]# echo -n password|base64
cGFzc3dvcmQ=
[root@server1 zabbix]# vim mysql_pwd.yml
apiVersion: v1
kind: Secret
metadata:
name: mysql-pwd
data:
mysql-root-pwd: redhat
创建MySQL部署文件
[root@server1 zabbix]# vim mysql_deploy.yml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: NodePort
ports:
- port: 3306
nodePort: 30306
protocol: TCP
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql
name: mysql
imagePullPolicy: Always
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pwd
key: mysql-root-pwd
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-conf
mountPath: /etc/mysql/conf.d/
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
- name: timezone
mountPath: /etc/localtime
volumes:
- name: mysql-conf
configMap:
name: mysql-conf
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
创建MySQL
[root@server1 zabbix]# kubectl apply -f mysql_deploy.yml
service/mysql created
deployment.apps/mysql created
[root@server1 zabbix]# kubectl apply -f mysql-conf.yml
configmap/mysql-conf created
[root@server1 zabbix]# kubectl apply -f mysql_pv.yml
persistentvolume/mysql-pv created
persistentvolumeclaim/mysql-pvc created
[root@server1 zabbix]# kubectl apply -f mysql_pwd.yml
secret/mysql-pwd created
创建zabbix-server yml文件
[root@server1 zabbix]# vim zabbix-server.yml
apiVersion: v1
kind: Service
metadata:
name: zabbix-server
spec:
type: NodePort
ports:
- port: 10051
nodePort: 30051
protocol: TCP
targetPort: 10051
selector:
app: zabbix-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zabbix-server
spec:
replicas: 1
selector:
matchLabels:
app: zabbix-server
template:
metadata:
labels:
app: zabbix-server
spec:
containers:
- name: zabbix-server
image: zabbix/zabbix-server-mysql
imagePullPolicy: Always
ports:
- containerPort: 10051
name: server
protocol: TCP
readinessProbe:
tcpSocket:
port: server
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: server
initialDelaySeconds: 15
periodSeconds: 20
env:
- name: DB_SERVER_HOST
value: "mysql"
- name: MYSQL_USER
value: "zabbix"
- name: MYSQL_PASSWORD
value: "zabbix"
- name: MYSQL_DATABASE
value: "zabbix"
- name: ZBX_CACHESIZE
value: "1024M"
- name: ZBX_TRENDCACHESIZE
value: "1024M"
- name: ZBX_HISTORYCACHESIZE
value: "2048M"
- name: ZBX_HISTORYINDEXCACHESIZE
value: "1024M"
- name: ZBX_STARTTRAPPERS
value: "5"
- name: ZBX_STARTPREPROCESSORS
value: "10"
- name: ZBX_STARTDBSYNCERS
value: "10"
- name: DB_SERVER_PORT
value: "3306"
- name: zabbix-agent
image: zabbix/zabbix-agent
imagePullPolicy: Always
ports:
- containerPort: 10050
name: zabbix-agent
env:
- name: ZBX_HOSTNAME
value: "Zabbix server"
- name: ZBX_SERVER_HOST
value: "127.0.0.1"
- name: ZBX_PASSIVE_ALLOW
value: "true"
- name: ZBX_STARTAGENTS
value: "3"
- name: ZBX_TIMEOUT
value: "10"
securityContext:
privileged: true
执行yml文件
[root@server1 zabbix]# kubectl apply -f zabbix-server.yml
service/zabbix-server created
deployment.apps/zabbix-server created
部署zabbix-web
[root@server1 zabbix]# vim zabbix-web.yml
apiVersion: v1
kind: Service
metadata:
name: zabbix-web
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
nodePort: 30080
targetPort: 80
selector:
app: zabbix-web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zabbix-web
spec:
replicas: 2
selector:
matchLabels:
app: zabbix-web
template:
metadata:
labels:
app: zabbix-web
spec:
containers:
- image: zabbix/zabbix-web-nginx-mysql
name: zabbix-web
imagePullPolicy: Always
ports:
- containerPort: 80
name: web
protocol: TCP
env:
- name: DB_SERVER_HOST
value: "mysql"
- name: ZBX_SERVER_HOST
value: "zabbixserver"
- name: MYSQL_USER
value: "zabbix"
- name: MYSQL_PASSWORD
value: "zabbix"
- name: TZ
value: "Asia/Shanghai"
执行yml文件
[root@server1 zabbix]# kubectl apply -f zabbix-web.yml
service/zabbix-web created
deployment.apps/zabbix-web created
查看刚才部署的MySQL和zabbix服务状态
[root@server1 zabbix]# kubectl get deploy,pod,svc,cm,secret,pv,pvc -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/mysql 0/1 1 0 149m mysql mysql app=mysql
deployment.apps/zabbix-server 0/1 1 0 8m40s zabbix-server,zabbix-agent zabbix/zabbix-server-mysql,zabbix/zabbix-agent app=zabbix-server
deployment.apps/zabbix-web 0/2 2 0 119s zabbix-web zabbix/zabbix-web-nginx-mysql app=zabbix-web
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/mysql-8f559dd4c-jm94m 0/1 CreateContainerConfigError 0 149m 10.244.2.8 server3.example.com <none> <none>
pod/nginx-cdk6m 1/1 Running 0 21h 10.244.1.26 server2.example.com <none> <none>
pod/nginx-pxmwd 1/1 Running 0 31d 10.244.1.25 server2.example.com <none> <none>
pod/zabbix-server-7c694c486f-2px5q 1/2 Running 2 8m39s 10.244.2.9 server3.example.com <none> <none>
pod/zabbix-web-7567556f55-66cd6 0/1 ContainerCreating 0 116s <none> server3.example.com <none> <none>
pod/zabbix-web-7567556f55-wvgsk 0/1 ContainerCreating 0 117s <none> server2.example.com <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 48d <none>
service/mysql NodePort 10.97.72.188 <none> 3306:30306/TCP 149m app=mysql
service/nginx-service NodePort 10.101.191.0 <none> 80:30001/TCP 31d app=nginx
service/zabbix-server NodePort 10.105.154.150 <none> 10051:30051/TCP 8m42s app=zabbix-server
service/zabbix-web NodePort 10.104.132.246 <none> 80:30080/TCP 2m app=zabbix-web
NAME DATA AGE
configmap/mysql-conf 1 143m
NAME TYPE DATA AGE
secret/default-token-8vnct kubernetes.io/service-account-token 3 48d
secret/mysql-pwd Opaque 1 136m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
persistentvolume/mysql-pv 1Gi RWO Retain Bound default/mysql-pvc manual 142m Filesystem
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE
persistentvolumeclaim/mysql-pvc Bound mysql-pv 1Gi RWO manual 142m Filesystem