Kubernetes创建可外部访问的mysql容器

创建一个Mysql服务的容器;

我们还是使用内网的Docker私有镜像仓库,第一次运行时会从镜像仓库中拉取镜像到机器上,以后就直接在机器上运行加快速度;

直接上配置文件:mysql-rc.yaml

kind: ReplicationController
metadata:
  name: my-mysql-v1
  namespace: default
  labels:
    name: my-mysql
    version: v1
spec:
  replicas: 1
  selector:
    name: my-mysql
    version: v1
  template:
    metadata:
      labels:
        name: my-mysql
        version: v1
    spec:
      containers:
        - image: reg.docker.tb/harbor/mysql:latest
          name: my-mysql-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

mysql-svc.yaml 注意这里直接指定了宿主机的IP地址;

apiVersion: v1
kind: Service
metadata:
  name: my-mysql
  namespace: default
  labels:
    name: my-mysql
spec:
  externalIPs:
    - 10.0.10.135
  ports:
    - port: 3306
      targetPort: 3306
  selector:
    name: my-mysql

创建容器:

[root@node5 ~]# kubectl create -f mysql-rc.yaml
replicationcontroller "my-mysql-v1" created
[root@node5 ~]# kubectl create -f mysql-svc.yaml
service "my-mysql" created

查看容器是否已经运行:

[root@node5 ~]# kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   10.254.0.1              443/TCP    9d
my-mysql     10.254.93.211   10.0.10.135   3306/TCP   3m
[root@node5 ~]# kubectl get pods
NAME                READY     STATUS    RESTARTS   AGE
my-mysql-v1-t9s1p   1/1       Running   0          4m

验证是否可以外部访问:

Kubernetes创建可外部访问的mysql容器_第1张图片

表明成功运行了mysql容器,并且可以能过宿主机的IP来访问;

你可能感兴趣的:(mysql,K8S,Docker)