Kubernetes创建mysql容器

  • Kubernetes创建mysql容器已关闭评论
  • 76 views
  • A+
所属分类:Kubernetes

首先,创建mysql-rc.yaml,内容如下:

apiVersion: v1
kind: ReplicationController 
metadata: 
 name: mysql5-7-v1 
 namespace: default 
 labels: 
 name: mysql5-7 
 version: v1 
spec: 
 replicas: 1 
 selector: 
 name: mysql5-7 
 version: v1 
 template: 
 metadata: 
 labels: 
 name: mysql5-7 
 version: v1 
 spec: 
 containers: 
 - image: mysql:5.7 
 name: mysql5-7-v1 
 ports: 
 - containerPort: 3306
 env: 
 - name: MYSQL_ROOT_PASSWORD 
 value: abcd123 
 volumeMounts: 
 - name: mysql-storage 
 mountPath: /var/lib/mysql
 volumes: 
 - name: mysql-storage 
 hostPath: 
 path: /data/mysql

然后创建容器:

kubectl create -f mysql-rc.yaml

通过命令查看如下:

[root@k4152v /data/yaml]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql5-7-v1-rzz6j 1/1 Running 0 29m 10.244.2.6 k4614v.add.bjyt.qihoo.net

查看配置信息如下:

[root@k4152v /data/yaml]# kubectl describe pod mysql5-7-v1-rzz6j
Name: mysql5-7-v1-rzz6j
Namespace: default
Node: k4614v.add.bjyt.qihoo.net/10.209.5.35
Start Time: Mon, 09 Apr 2018 20:50:45 +0800
Labels: name=mysql5-7
 version=v1
Annotations: <none>
Status: Running
IP: 10.244.2.6
Controlled By: ReplicationController/mysql5-7-v1
Containers:
 mysql5-7-v1:
 Container ID: docker://6fa8bbc8148c585fa6613c82444cea1751a9063c366bd820d2ba2e959a5fd230
 Image: mysql:5.7
 Image ID: docker-pullable://mysql@sha256:691c55aabb3c4e3b89b953dd2f022f7ea845e5443954767d321d5f5fa394e28c
 Port: 3306/TCP
 State: Running
 Started: Mon, 09 Apr 2018 20:51:47 +0800
 Ready: True
 Restart Count: 0
 Environment:
 MYSQL_ROOT_PASSWORD: abcd123
 Mounts:
 /var/lib/mysql from mysql-storage (rw)
 /var/run/secrets/kubernetes.io/serviceaccount from default-token-8bsjw (ro)
Conditions:
 Type Status
 Initialized True 
 Ready True 
 PodScheduled True 
Volumes:
 mysql-storage:
 Type: HostPath (bare host directory volume)
 Path: /data/mysql
 HostPathType: 
 default-token-8bsjw:
 Type: Secret (a volume populated by a Secret)
 SecretName: default-token-8bsjw
 Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
 Type Reason Age From Message
 ---- ------ ---- ---- -------
 Normal Scheduled 30m default-scheduler Successfully assigned mysql5-7-v1-rzz6j to k4614v.add.bjyt.qihoo.net
 Normal SuccessfulMountVolume 30m kubelet, k4614v.add.bjyt.qihoo.net MountVolume.SetUp succeeded for volume "mysql-storage"
 Normal SuccessfulMountVolume 30m kubelet, k4614v.add.bjyt.qihoo.net MountVolume.SetUp succeeded for volume "default-token-8bsjw"
 Normal Pulling 30m kubelet, k4614v.add.bjyt.qihoo.net pulling image "mysql:5.7"
 Normal Pulled 29m kubelet, k4614v.add.bjyt.qihoo.net Successfully pulled image "mysql:5.7"
 Normal Created 29m kubelet, k4614v.add.bjyt.qihoo.net Created container
 Normal Started 29m kubelet, k4614v.add.bjyt.qihoo.net Started container

然后通过kubernetes管理端就可以看到,如下:

Kubernetes创建mysql容器

通过管理端命令端登录进去,如图:

Kubernetes创建mysql容器

接下来,我们配置个mysql-svc.yaml,直接指定映射到宿主机ip,这样就可以对外访问了,内容如下:

apiVersion: v1 
kind: Service 
metadata: 
 name: mysql5-7 
 namespace: default 
 labels: 
 name: mysql5-7 
spec: 
 externalIPs: 
 - 10.209.5.43 
 ports: 
 - port: 3306 
 targetPort: 3306 
 selector: 
 name: mysql5-7

创建容器:

kubectl create -f mysql-svc.yaml

查看容器是否已经运行:

 

[root@k4152v /data/yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h
mysql5-7 ClusterIP 10.109.170.208 10.209.5.43 3306/TCP 29m

或者通过管理端查看,如下图:

Kubernetes创建mysql容器

详情如下:

Kubernetes创建mysql容器

然后我们测试一下,看看是否可以外连,如下:

Kubernetes创建mysql容器

ok了

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar