https://kubernetes.io/docs/tutorials/kubernetes-basics/ Learn Kubernetes Basics
https://kubernetes.io/docs/tutorials/kubernetes-basics/deploy-app/deploy-interactive/
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9a996c0f147 k8s.gcr.io/k8s-dns-sidecar-amd64@sha256:9aab42bf6a2a068b797fe7d91a5d8d915b10dbbc3d6f2b10492848debfba6044 "/sidecar --v=2 --..." About a minute ago Up About a minute k8s_sidecar_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
016d0913197c k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64@sha256:46b933bb70270c8a02fa6b6f87d440f6f1fce1a5a2a719e164f83f7b109f7544 "/dnsmasq-nanny -v..." About a minute ago Up About a minute k8s_dnsmasq_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
467d815959ff k8s.gcr.io/k8s-dns-kube-dns-amd64@sha256:1a3fc069de481ae690188f6f1ba4664b5cc7760af37120f70c86505c79eea61d "/kube-dns --domai..." About a minute ago Up About a minute k8s_kubedns_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
091393c7862f e94d2f21bc0c "/dashboard --inse..." 2 minutes ago Up 2 minutes k8s_kubernetes-dashboard_kubernetes-dashboard-5498ccf677-b79k4_kube-system_dfedb486-13ec-11e9-8e8e-0242ac11002f_0
1072418f5779 k8s.gcr.io/pause-amd64:3.1 "/pause" 2 minutes ago Up 2 minutes k8s_POD_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
a6f80b3060f0 k8s.gcr.io/pause-amd64:3.1 "/pause" 2 minutes ago Up 2 minutes k8s_POD_kubernetes-dashboard-5498ccf677-b79k4_kube-system_dfedb486-13ec-11e9-8e8e-0242ac11002f_0
cce1b94f6656 4689081edb10 "/storage-provisioner" 2 minutes ago Up 2 minutes k8s_storage-provisioner_storage-provisioner_kube-system_e0a96d0e-13ec-11e9-8e8e-0242ac11002f_0
2c67b8656b49 k8s.gcr.io/pause-amd64:3.1 "/pause" 2 minutes ago Up 2 minutes k8s_POD_storage-provisioner_kube-system_e0a96d0e-13ec-11e9-8e8e-0242ac11002f_0
b44e23384e1d 9c16409588eb "/opt/kube-addons.sh" 2 minutes ago Up 2 minutes k8s_kube-addon-manager_kube-addon-manager-minikube_kube-system_3afaf06535cc3b85be93c31632b765da_0
1ab907291f64 k8s.gcr.io/pause-amd64:3.1 "/pause" 2 minutes ago Up 2 minutes k8s_POD_kube-addon-manager-minikube_kube-system_3afaf06535cc3b85be93c31632b765da_0
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.0", GitCommit:"91e7b4fd31fcd3d5f436da26c980becec37ceefe", GitTreeState:"clean", BuildDate:"2018-06-27T20:17:28Z", GoVersion:"go1.10.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-04-10T12:46:31Z", GoVersion:"go1.9.4", Compiler:"gc", Platform:"linux/amd64"}
Let’s run our first app on Kubernetes with the kubectl run command. The run command creates a new deployment.
kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
176ade40fc98 8fafd8af70e9 "/bin/sh -c 'node ..." 2 minutes ago Up 2 minutes k8s_kubernetes-bootcamp_kubernetes-bootcamp-5c69669756-px485_default_910e933d-13ed-11e9-8e8e-0242ac11002f_0
a8403b0894f6 k8s.gcr.io/pause-amd64:3.1 "/pause" 3 minutes ago Up 2 minutes k8s_POD_kubernetes-bootcamp-5c69669756-px485_default_910e933d-13ed-11e9-8e8e-0242ac11002f_0
c9a996c0f147 k8s.gcr.io/k8s-dns-sidecar-amd64@sha256:9aab42bf6a2a068b797fe7d91a5d8d915b10dbbc3d6f2b10492848debfba6044 "/sidecar --v=2 --..." 7 minutes ago Up 7 minutes k8s_sidecar_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
016d0913197c k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64@sha256:46b933bb70270c8a02fa6b6f87d440f6f1fce1a5a2a719e164f83f7b109f7544 "/dnsmasq-nanny -v..." 7 minutes ago Up 7 minutes k8s_dnsmasq_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
467d815959ff k8s.gcr.io/k8s-dns-kube-dns-amd64@sha256:1a3fc069de481ae690188f6f1ba4664b5cc7760af37120f70c86505c79eea61d "/kube-dns --domai..." 7 minutes ago Up 7 minutes k8s_kubedns_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
091393c7862f e94d2f21bc0c "/dashboard --inse..." 7 minutes ago Up 7 minutes k8s_kubernetes-dashboard_kubernetes-dashboard-5498ccf677-b79k4_kube-system_dfedb486-13ec-11e9-8e8e-0242ac11002f_0
1072418f5779 k8s.gcr.io/pause-amd64:3.1 "/pause" 7 minutes ago Up 7 minutes k8s_POD_kube-dns-6dcb57bcc8-2bmwt_kube-system_dffc0ae1-13ec-11e9-8e8e-0242ac11002f_0
a6f80b3060f0 k8s.gcr.io/pause-amd64:3.1 "/pause" 7 minutes ago Up 7 minutes k8s_POD_kubernetes-dashboard-5498ccf677-b79k4_kube-system_dfedb486-13ec-11e9-8e8e-0242ac11002f_0
cce1b94f6656 4689081edb10 "/storage-provisioner" 7 minutes ago Up 7 minutes k8s_storage-provisioner_storage-provisioner_kube-system_e0a96d0e-13ec-11e9-8e8e-0242ac11002f_0
2c67b8656b49 k8s.gcr.io/pause-amd64:3.1 "/pause" 7 minutes ago Up 7 minutes k8s_POD_storage-provisioner_kube-system_e0a96d0e-13ec-11e9-8e8e-0242ac11002f_0
b44e23384e1d 9c16409588eb "/opt/kube-addons.sh" 8 minutes ago Up 8 minutes k8s_kube-addon-manager_kube-addon-manager-minikube_kube-system_3afaf06535cc3b85be93c31632b765da_0
1ab907291f64 k8s.gcr.io/pause-amd64:3.1 "/pause" 8 minutes ago Up 8 minutes k8s_POD_kube-addon-manager-minikube_kube-system_3afaf06535cc3b85be93c31632b765da_0
増えたのは:
176ade40fc98 8fafd8af70e9 "/bin/sh -c 'node ..." 2 minutes ago Up 2 minutes k8s_kubernetes-bootcamp_kubernetes-bootcamp-5c69669756-px485_default_910e933d-13ed-11e9-8e8e-0242ac11002f_0
Great! You just deployed your first application by creating a deployment. This performed a few things for you:
- searched for a suitable node where an instance of the application could be run (we have only 1 available node)
- scheduled the application to run on that Node
- configured the cluster to reschedule the instance on a new Node when needed
To list your deployments use the get deployments command:
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1 1 1 1 7m
We see that there is 1 deployment running a single instance of your app. The instance is running inside a Docker container on your node.
今まで 内部ネットワークで動いています。 kubectl proxy を使って API endpoint を出します
Pods that are running inside Kubernetes are running on a private, isolated network. By default they are visible from other pods and services within the same kubernetes cluster, but not outside that network. When we use kubectl, we're interacting through an API endpoint to communicate with our application.
kubectl proxy
(別 terminal window で)
curl http://localhost:8001/version
{
"major": "1",
"minor": "10",
"gitVersion": "v1.10.0",
"gitCommit": "fc32d2f3698e36b93322a3465f63a14e9f0eaead",
"gitTreeState": "clean",
"buildDate": "2018-04-10T12:46:31Z",
"goVersion": "go1.9.4",
"compiler": "gc",
"platform": "linux/amd64"
}
The API server will automatically create an endpoint for each pod, based on the pod name, that is also accessible through the proxy. Pod ごとに API endpoint が作られる
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5c69669756-px485 1/1 Running 0 24m
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-5c69669756-px485
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-px485 | v=1
https://kubernetes.io/docs/tutorials/kubernetes-basics/explore/explore-intro/
A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker or rkt), and some shared resources for those containers. Those resources include:
- Shared storage, as Volumes
- Networking, as a unique cluster IP address
- Information about how to run each container, such as the container image version or specific ports to use
Pods are the atomic unit on the Kubernetes platform. When we create a Deployment on Kubernetes, that Deployment creates Pods with containers inside them (as opposed to creating containers directly).
https://kubernetes.io/docs/tutorials/kubernetes-basics/explore/explore-intro/
A Pod always runs on a Node. A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster. The Master's automatic scheduling takes into account the available resources on each Node.
$ kubectl exec $POD_NAME env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-5c69669756-nbqkf
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
kubectl exec -ti $POD_NAME bash
cat server.js
var http = require('http');
var requests=0;
var podname= process.env.HOSTNAME;
var startTime;
var host;
var handleRequest = function(request, response) {
response.setHeader('Content-Type', 'text/plain');
response.writeHead(200);
response.write("Hello Kubernetes bootcamp! | Running on: ");
response.write(host);
response.end(" | v=1\n");
console.log("Running On:" ,host, "| Total Requests:", ++requests,"| App Uptime:", (new Date() - startTime)/1000 , "seconds
", "| Log Time:",new Date());
}
var www = http.createServer(handleRequest);
www.listen(8080,function () {
startTime = new Date();;
host = process.env.HOSTNAME;
console.log ("Kubernetes Bootcamp App Started At:",startTime, "| Running On: " ,host, "\n" );
});
root@kubernetes-bootcamp-5c69669756-nbqkf:/# curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-nbqkf | v=1
root@kubernetes-bootcamp-5c69669756-nbqkf:/# hostname
kubernetes-bootcamp-5c69669756-nbqkf
root@kubernetes-bootcamp-5c69669756-nbqkf:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe12:2/64 scope link
valid_lft forever preferred_lft forever
https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/