k8s学习: 创建 mysql 任务
参考:手把手教你搞定K8S集群的安装部署1、创建MySql 容器创建MySql RC(Replication Controller)文件mkdir test-mysql && cd test-mysql/[root@centos7-189 test-mysql]# vim mysql-rc.yamlapiVersion: v1kind: ReplicationController#
1、创建MySql 容器
创建MySql RC(Replication Controller)文件
mkdir test-mysql && cd test-mysql/
[root@centos7-189 test-mysql]# vim mysql-rc.yaml
apiVersion: v1
kind: ReplicationController #副本控制器RC
metadata:
name: mysql #RC的名称,全局唯一
spec:
replicas: 1 #Pod副本的期待数量
selector:
app: mysql #符合目标的Pod拥有此标签
template: #根据模板创建Pod的副本(实例)
metadata:
labels:
app: mysql #Pod副本拥有的标签,对应RC的Selector
spec:
containers: #Pod内容器的定义部分
- name: mysql #容器名称
image: docker.io/library/mysql:5.7 #容器对应的Docker Image
ports:
- containerPort: 3306 #容器应用监听的端口号
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
创建 mysql-rc.
[root@centos7-189 test-mysql]# kubectl create -f mysql-rc.yaml
replicationcontroller/mysql created
查看刚创建的RC
[root@centos7-189 test-mysql]# kubectl get rc
NAME DESIRED CURRENT READY AGE
mysql 1 1 0 15s
查看刚创建的Pod
[root@centos7-189 test-mysql]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-tpxn2 1/1 Running 0 96s
查看刚创建的Pod详情
[root@centos7-189 test-mysql]# kubectl describe pod mysql-tpxn2
Name: mysql-tpxn2
Namespace: default
Priority: 0
Node: centos7-186/192.168.1.186
Start Time: Mon, 15 Nov 2021 14:59:05 +0800
Labels: app=mysql
Annotations: <none>
Status: Running
IP: 10.244.1.2
IPs:
IP: 10.244.1.2
Controlled By: ReplicationController/mysql
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m50s default-scheduler Successfully assigned default/mysql-tpxn2 to centos7-186
Normal Pulling 2m48s kubelet Pulling image "docker.io/library/mysql:5.7"
Normal Pulled 85s kubelet Successfully pulled image "docker.io/library/mysql:5.7" in 1m23.052534372s
Normal Created 85s kubelet Created container mysql
Normal Started 85s kubelet Started container mysql
从这里可以看到容器创建在节点 Node: centos7-186/192.168.1.186
ssh 到 192.168.1.186 验证查看
[root@centos7-186 ~]# docker ps |grep mysql
10c4682b4161 mysql "docker-entrypoint.s…" 54 minutes ago Up 54 minutes k8s_mysql_mysql-tpxn2_default_b4dd68ab-2fb3-4ada-9069-cdfad8f5287c_0
7643b47c8a8a registry.aliyuncs.com/google_containers/pause:3.5 "/pause" 56 minutes ago Up 56 minutes k8s_POD_mysql-tpxn2_default_b4dd68ab-2fb3-4ada-9069-cdfad8f5287c_0
2、 创建关联的Kubernets Service
创建 service 配置文件
[root@centos7-189 test-mysql]# vim mysql-svc.yaml
apiVersion: v1
kind: Service #表名是Kubernetes Service
metadata:
name: mysql #Service的全局唯一名称
spec:
type: NodePort
ports:
- port: 3306 #Service提供服务器的端口号
nodePort: 30001 #堆外暴露端口
selector: #Service对应的Pod拥有这里定义的标签
app: mysql
创建 service
[root@centos7-189 test-mysql]# kubectl create -f mysql-svc.yaml
service/mysql created
查看刚创建的Service
[root@centos7-189 test-mysql]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 30h
mysql NodePort 10.102.141.170 <none> 3306:30001/TCP 6s
3. 验证 mysql 任务
-
安装一个 mysql 客户端
如果已经有安装过 mysql-server的可以直接从那里去验证
[root@centos7-189 test-mysql]# yum install mysql -y -
连接测试
[root@centos7-189 test-mysql]# mysql -h 192.168.1.189 -u root -P 30001 -pMySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) MySQL [(none)]> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MySQL [mysql]> show tables; ... MySQL [mysql]> desc user; ... MySQL [mysql]> select User,Host from user; +---------------+-----------+ | User | Host | +---------------+-----------+ | root | % | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-----------+ 4 rows in set (0.00 sec) MySQL [mysql]> exit
清理
kubectl get rc
NAME DESIRED CURRENT READY AGE
mysql 1 1 1 4d1h
kubectl get rs
No resources found in default namespace.
kubectl delete rc mysql
replicationcontroller "mysql" deleted
更多推荐
所有评论(0)