目录
一、新建namespace
1、创建 namespace
kubectl create namespace lhynamespace
2、查看 namespace
kubectl get namespace
3、删除 namespace
kubectl delete namespace lhynamespace
二、新建deployment
1、生成yaml文件
kubectl create deployment mysql8 --image=mysql:8 --dry-run -o yaml > mysql8_deployment.yaml
2、修改yaml文件
指定 namespace
metadata:
namespace: lhynamespace
3、创建 deployment
kubectl apply -f mysql8_deployment.yaml
4、查看 lhynamespace 命名空间下的 deployment
kubectl get deployment -n lhynamespace
kubectl get deployment --all-namespaces //查看所有namespace
5、删除 lhynamespace 命名空间下的 mysql8 的 deployment ,删除deployment 之后 pod 自动删除 通过yaml创建的,通过yaml文件删除
kubectl delete -f mysql8_deployment.yaml
kubectl delete deployment mysql8 -n lhynamespace //命令删除
三、新建service
kubectl expose -f mysql8_deployment.yaml --port=3306 --target-port=3306 --type=NodePort -n lhynamespace -o yaml > mysql8_service.yaml
集群内部访问端口 3306
物理机映射端口 可以查看生成的yaml文件的 nodePort
也可以根据生成的yaml 修改配置重新部署service
四、部署用到的yaml文件
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mysql8
name: mysql8
namespace: lhynamespace
spec:
replicas: 2
selector:
matchLabels:
app: mysql8
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mysql8
spec:
containers:
- image: mysql:8
name: mysql8
resources: {}
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
ports:
- name: mysql-3306
containerPort: 3306
protocol: TCP
status: {}
service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2022-04-22T11:22:53Z"
labels:
app: mysql8
name: mysql8
namespace: lhynamespace
resourceVersion: "11201"
uid: ea56fbb1-4e20-4c4e-8990-c3859e2cb9fc
spec:
clusterIP: 10.105.91.138
clusterIPs:
- 10.105.91.138
externalTrafficPolicy: Cluster
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- nodePort: 30001
port: 3306
protocol: TCP
targetPort: 3306
selector:
app: mysql8
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}