如何从kubernetes中的网络新部署的pod访问?

时间:2017-06-06 08:17:15

标签: docker kubernetes

我是kubernetes的新手,我知道我错过了一些小但却看不清楚的东西。 我正在创建一个包含文件的pod:kubectl create -f mysql.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  containers:
    - resources:
        limits :
          cpu: 2
      image: mysql
      name: mysql
      env:
        - name: MYSQL_ROOT_PASSWORD
          # change this
          value: TestingDB1
      ports:
        - containerPort: 3306
          name: mysql

和一个服务:kubectl create -f mysql_service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: mysql
  name: mysql
spec:
  externalIPs:
    - 10.19.13.127
  ports:
    - port: 3306
  selector:
    name: mysql

输出" kubectl版本"

Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"d33fd89e399396658aed4e48dfe7d5d8d50ac6e8", GitTreeState:"clean", BuildDate:"2017-05-26T17:08:24Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"d33fd89e399396658aed4e48dfe7d5d8d50ac6e8", GitTreeState:"clean", BuildDate:"2017-05-26T17:08:24Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}

输出" kubectl cluster-info"

Kubernetes master is running at http://localhost:8080

输出" kubectl get pods"

NAME                    READY     STATUS    RESTARTS   AGE
mysql                   1/1       Running   0          20m

输出" kubectl获取svc"

NAME         CLUSTER-IP       EXTERNAL-IP    PORT(S)    AGE
kubernetes   10.254.0.1       <none>         443/TCP    18h
mysql        10.254.129.206   10.19.13.127   3306/TCP   1h

输出&#34; kubectl得不到&#34;

NAME           STATUS    AGE
10.19.13.127   Ready     19h

&#34; docker ps&#34;

的输出
CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS               NAMES
74ea1fb2b383        mysql                                      "docker-entrypoint.sh"   3 minutes ago       Up 3 minutes                            k8s_mysql.ae7893ad_mysql_default_e58d1c09-4a8e-11e7-9baf-fa163ee3f5d9_793d8d7c

我可以看到正在创建pod。即使我连接到容器,我也可以使用凭据登录到mysql。

我的问题是: 如何从我的网络访问/公开我的kubernetes节点上运行的端口?例如,我想从我的PC上运行telnet到运行mysql pod的kubernetes节点。

谢谢!

2 个答案:

答案 0 :(得分:0)

在命令下验证Redis服务器是否在pod中运行并侦听哪个端口(通常它在6379端口上运行):

kubectl get pods redis-master --template ='{{(index(index .spec.containers 0).ports 0).containerPort}} {{“\ n”}}'

输出:6739

答案 1 :(得分:0)

以下命令为您提供端口监听,因此您可以创建路由或端口转发以访问服务

kubectl get pod <pod_name> -o "go-template={{(index (index .spec.containers 0).ports 0).containerPort}}