官术网_书友最值得收藏!

Pod example

Let's take a quick look at a pod in action. We'll spin up a Node.js application on the cluster. You'll need a GCE cluster running for this; if you don't already have one started, refer to the Our first cluster section in Chapter 1, Introduction to Kubernetes.

Now, let's make a directory for our definitions. In this example, I'll create a folder in the /book-examples subfolder under our home directory:

$ mkdir book-examples
$ cd book-examples
$ mkdir 02_example
$ cd 02_example
You can download the example code files from your account at http://www.packtpub.com for all of the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

Use your favorite editor to create the following file and name it as nodejs-pod.yaml:

apiVersion: v1 
kind: Pod
metadata:
name: node-js-pod
spec:
containers:
- name: node-js-pod
image: bitnami/apache:latest
ports:
- containerPort: 80

This file creates a pod named node-js-pod with the latest bitnami/apache container running on port 80. We can check this using the following command:

$ kubectl create -f nodejs-pod.yaml
pod "node-js-pod" created

This gives us a pod running the specified container. We can see more information on the pod by running the following command:

$ kubectl describe pods/node-js-pod

You'll see a good deal of information, such as the pod's status, IP address, and even relevant log events. You'll note the pod IP address is a private IP address, so we cannot access it directly from our local machine. Not to worry, as the kubectl exec command mirrors Docker's exec functionality. You can get the pod IP address in a number of ways. A simple get of the pod will show you the IP where we use a template output that looks up the IP address in the status output:

$ kubectl get pod node-js-pod --template={{.status.podIP}}

You can use that IP address directly, or execute that command within backticks to exec into the pod. Once the pod shows it's in a running state, we can use this feature to run a command inside a pod:

$ kubectl exec node-js-pod -- curl <private ip address>

--or--


$ kubectl exec node-js-pod -- curl `kubectl get pod node-js-pod --template={{.status.podIP}}`
By default, this runs a command in the first container it finds, but you can select a specific one using the -c argument.

After running the command, you should see some HTML code. We'll have a prettier view later in this chapter, but for now, we can see that our pod is indeed running as expected.

If you have experience with containers, you've probably also exec 'd into a container. You can do something very similar with Kubernetes:

master $ kubectl exec -it node-js-pod -- /bin/bash
root@node-js-pod:/opt/bitnami/apache/htdocs# exit
master $

You can also run other command directly into the container with the exec command. Note that you'll need to use two dashes to separate your command's argument in case it has the same in kubectl:

$ kubectl exec node-js-pod ls / 
$ kubectl exec node-js-pod ps aux
$ kubectl exec node-js-pod -- uname -a
主站蜘蛛池模板: 青川县| 海南省| 永城市| 林芝县| 福安市| 蓝田县| 油尖旺区| 高邮市| 合川市| 永善县| 南华县| 涞水县| 南开区| 宜昌市| 武宁县| 郯城县| 盐亭县| 曲阜市| 葫芦岛市| 桂东县| 广东省| 宜宾市| 疏附县| 遂宁市| 封开县| 湖南省| 逊克县| 朝阳区| 浦东新区| 临海市| 嫩江县| 莎车县| 济源市| 唐山市| 华池县| 鄢陵县| 长阳| 华池县| 河池市| 汤阴县| 临汾市|