0、创建namespace
kubectl create namespace demo
1、创建configmap.yaml文件
cat configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myblog
namespace: demo
data:
MYSQL_HOST: "172.21.32.6" #数据库主机地址
MYSQL_PORT: "3306" #数据库端口
2、应用configmap文件
kubectl apply -f configmap.yaml
3、创建secret.yaml文件(方法1)
cat secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: myblog
namespace: demo
type: Opaque
data:
MYSQL_USER: cm9vdA== #注意加-n参数, echo -n root|base64 #数据库用户
MYSQL_PASSWD: MTIzNDU2 #数据库密码 echo -n 123456|base64
4、应用secret.yaml文件
kubectl apply -f secret.yaml
5、创建secret.yaml文件(方法2)
说明:使用文件的方式创建
cat secret.txt
MYSQL_USER=root
MYSQL_PASSWD=123456
kubectl -n demo create secret generic myblog --from-env-file=secret.txt
6、创建mysql-deployment文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: demo
spec:
replicas: 1 #指定Pod副本数
selector: #指定Pod的选择器
matchLabels:
app: mysql
template:
metadata:
labels: #给Pod打label
app: mysql
spec:
#hostNetwork: true # 声明pod的网络模式为host模式,效果通docker run --net=host
volumes:
- name: mysql-data
hostPath:
path: /opt/mysql/data
nodeSelector: # 使用节点选择器将Pod调度到指定label的节点
component: mysql
containers:
- name: mysql
image: mysql:5.7
args:
- "--character-set-server=utf8" #
- "--collation-server=utf8_general_ci" # 指定字符编码
ports:
- containerPort: 3306
env:
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: myblog
key: MYSQL_USER
- name: MYSQL_PASSWD
valueFrom:
secretKeyRef:
name: myblog
key: MYSQL_PASSWD
- name: MYSQL_DATABASE
value: "myblog"
resources:
requests:
memory: 100Mi
cpu: 50m
limits:
memory: 500Mi
cpu: 100m
readinessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 15
periodSeconds: 20
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: demo
spec:
ports:
type: NodePort
- port: 3306
protocol: TCP
targetPort: 3306
nodePort:9001
selector:
app: mysql
6.1、应用mysql-deployment
kubectl label node k8s-slave1 component=mysql #给指定节点创建label
kubectl apply -f mysql-deployment
7、创建demo-myblog.yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: myblog
namespace: demo
spec:
replicas: 1 #指定Pod副本数
selector: #指定Pod的选择器
matchLabels:
app: myblog
template:
metadata:
labels: #给Pod打label
app: myblog
spec:
containers:
- name: myblog
image: liujixiao/myblog:latest
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_HOST
valueFrom:
configMapKeyRef:
name: myblog
key: MYSQL_HOST
- name: MYSQL_PORT
valueFrom:
configMapKeyRef:
name: myblog
key: MYSQL_PORT
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: myblog
key: MYSQL_USER
- name: MYSQL_PASSWD
valueFrom:
secretKeyRef:
name: myblog
key: MYSQL_PASSWD
ports:
- containerPort: 8002
resources:
requests:
memory: 100Mi
cpu: 50m
limits:
memory: 500Mi
cpu: 100m
livenessProbe:
httpGet:
path: /blog/index/
port: 8002
scheme: HTTP
initialDelaySeconds: 10 # 容器启动后第一次执行探测是需要等待多少秒
periodSeconds: 15 # 执行探测的频率
timeoutSeconds: 2 # 探测超时时间
readinessProbe:
httpGet:
path: /blog/index/
port: 8002
scheme: HTTP
initialDelaySeconds: 10
timeoutSeconds: 2
periodSeconds: 15
---
apiVersion: v1
kind: Service
metadata:
name: myblog
namespace: demo
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8002
nodePort: 9002
selector:
app: myblog
type: NodePort
7.1、应用demo-myblog.yaml文件
kubectle apply -f demo-myblog.yaml