kubernetes-create-pods
根据前一篇文章,安装好kubernetes集群,集群正常在运行,下面开始创建pods吧!
你可以选择,从github下载kubernetes源码,切换到kubernetes/examples/guestbook参考官网文档搭建一个完整的应用来验证集群的基本功能。
1 | [root@kubernetes-controller ~]# kubectl get nodes |
创建Namespace:kube-system1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[root@kubernetes-controller ~]# vi namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: kube-system
[root@kubernetes-controller ~]# kubectl get namespace
NAME STATUS AGE
default Active 5h
[root@kubernetes-controller ~]# kubectl create -f namespace.yaml
namespace "kube-system" created
[root@kubernetes-controller ~]# kubectl get namespace
NAME STATUS AGE
default Active 5h
kube-system Active 3s
启动一个nginx
新建一个pod-nginx.yaml文件,用于描述 pod.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[root@kubernetes-controller ~]# kubectl api-versions
autoscaling/v1
batch/v1
extensions/v1beta1
v1
[root@kubernetes-controller ~]# vi pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
然后启动这个pod,检查状态1
2
3
4
5
6
7
8
9# create Pod
[root@kubernetes-controller ~]# kubectl create -f pod-nginx.yaml
pod "nginx" created
[root@kubernetes-controller ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 0/1 ContainerCreating 0 15s
[root@kubernetes-controller ~]# kubectl describe pods nginx
可以看到,这个nginx-pod被调度到kubernetes-node3启动。1
2
3
4
5
6
7
8[root@kubernetes-controller ~]# kubectl describe pods nginx|tail -5
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
2m 2m 1 {default-scheduler } Normal Scheduled Successfully assigned nginx to kubernetes-node-3
[root@kubernetes-controller ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 11m
接下来我们可以去kubernetes-node-3节点,看看是否有对应的镜像被拉取并启动,发现没用镜像也没有启动容器,额,再次检查发现,连接超时,国内网络环境,不得不说一句fuck!到那个节点search一下。等待好久都没结果,我们去配置文件找找为啥会有这样一个容器被pull!1
2
3
4
5
6 <invalid> <invalid> 1 {kubelet kubernetes-node-3} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request. details: (unable to ping registry endpoint https://registry.access.redhat.com/v0/\nv2 ping attempt failed with error: https://registry.access.redhat.com/v2/ does not appear to be a v2 registry endpoint\n v1 ping attempt failed with error: Get https://registry.access.redhat.com/v1/_ping: read tcp 192.168.2.123:42613->209.132.182.63:443: read: connection timed out)"
日志:
No such image: registry.access.redhat.com/rhel7/pod-infrastructure:latest"
[root@kubernetes-node-3 ~]# docker search registry.access.redhat.com/rhel7/pod-infrastructure:latest
在kubernetes-node的节点上都会有这样的一个基础镜像,registry.access.redhat.com/rhel7/pod-infrastructure:latest
这个镜像是Pod 启动时的一个基础容器,你可以通过dokcer ps -a命令看到这个容器,类似windows系统服务,供kubenetes内部使用。pods无法运行,好可恶,只能提前下载下来导入进去啦!docker.io没用提供,google源又被封,只能考虑其他方式了!发现时间不同步,待我同步所有节点时间后,居然镜像就自己下载下来了。会自动run起这个容器。
http://kubernetes.io/docs/user-guide/images/
1 | [root@kubernetes-node-2 kubernetes]# cat /etc/kubernetes/kubelet |grep KUBELET_POD_INFRA_CONTAINER |
经过我多次重试,终于把nginx下载下来了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20[root@kubernetes-node-3 ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
51f5c6a04d83: Already exists
a3ed95caeb02: Pulling fs layer
51d229e136d0: Pull complete
bcd41daec8cc: Pull complete
Digest: sha256:0fe6413f3e30fcc5920bc8fa769280975b10b1c26721de956e1428b9e2f29d04
Status: Image is up to date for docker.io/nginx:latest
[root@kubernetes-node-3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 0d409d33b27e 10 weeks ago 182.7 MB
registry.access.redhat.com/rhel7/pod-infrastructure latest ee020ceeef01 3 months ago 215.4 MB
[root@kubernetes-node-3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f2de065fd05 nginx "nginx -g 'daemon off" 21 seconds ago Up 20 seconds k8s_nginx.fa5127aa_nginx_default_e7bd8714-616f-11e6-a406-000c2980ea46_f5a8b2ec
3c00d5e7d1e9 registry.access.redhat.com/rhel7/pod-infrastructure:latest "/pod" 53 minutes ago Up 53 minutes k8s_POD.c36b0a77_nginx_default_e7bd8714-616f-11e6-a406-000c2980ea46_b03d011b
如上最后一条命了执行完成,可以看到up了两个容器,一个是pod系统基础容器,另一个是我创建的容器!
主节点查看pod状态,pod从创建经历了几个过程:Scheduled->Pulling->Pulled->Created->Started1
2
3
4
5
6
7[root@kubernetes-controller ~]# kubectl describe pod|tail -8
58m 58m 1 {default-scheduler } Normal Scheduled Successfully assigned nginx to kubernetes-node-3
58m 58m 1 {kubelet kubernetes-node-3} spec.containers{nginx} Normal Pulling pulling image "nginx"
58m 5m 2 {kubelet kubernetes-node-3} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
5m 5m 1 {kubelet kubernetes-node-3} spec.containers{nginx} Normal Pulled Successfully pulled image "nginx"
5m 5m 1 {kubelet kubernetes-node-3} spec.containers{nginx} Normal Created Created container with docker id 2f2de065fd05
5m 5m 1 {kubelet kubernetes-node-3} spec.containers{nginx} Normal Started Started container with docker id 2f2de065fd05
查看pod日志内容,这个容器木有日志可看1
2
3
4
5[root@kubernetes-controller ~]# kubectl logs --tail=10 nginx
[root@kubernetes-controller ~]# kubectl logs -f nginx
[root@kubernetes-node-3 ~]# docker logs 2f2de065fd05
进入容器验证nginx,挺智能的,我进入容器失败,容器不支持,卡住,容器自动退出,docker ps -a里面立马启动一个新nginx容器。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88[root@kubernetes-controller ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 1h
[root@kubernetes-node-3 ~]# docker inspect 3c00d5e7d1e9|grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.16.60.2",
"IPAddress": "172.16.60.2",
[root@kubernetes-node-3 ~]# yum install util-linux
[root@kubernetes-node-3 ~]# docker inspect -f '{{.State.Pid}}' d1ea6b36b77e
24765
[root@kubernetes-node-3 ~]# nsenter --target 24765 --mount --uts --ipc --net --pid
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8)
root@nginx:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 31680 2868 ? Ss 17:28 0:00 nginx: master process nginx -g daemon off;
nginx 10 0.0 0.0 32072 1692 ? S 17:28 0:00 nginx: worker process
root 11 0.1 0.0 20288 1912 ? S 17:49 0:00 -bash
root 15 0.0 0.0 17492 1164 ? R+ 17:49 0:00 ps aux
root@nginx:/# ps -ef|grep nginx
root 1 0 0 17:28 ? 00:00:00 nginx: master process nginx -g daemon off;
nginx 10 1 0 17:28 ? 00:00:00 nginx: worker process
root 17 11 0 17:50 ? 00:00:00 grep nginx
root@nginx:/# cat /etc/hosts|tail -1
172.16.60.2 nginx
root@nginx:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
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
5: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
link/ether 02:42:ac:10:3c:02 brd ff:ff:ff:ff:ff:ff
inet 172.16.60.2/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe10:3c02/64 scope link
valid_lft forever preferred_lft forever
root@nginx:/# exit
logout
退出不会影响容器状态
[root@kubernetes-node-3 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1ea6b36b77e nginx "nginx -g 'daemon off" 24 minutes ago Up 24 minutes k8s_nginx.fa5127aa_nginx_default_e7bd8714-616f-11e6-a406-000c2980ea46_831516c2
# show Pods list
[root@kubernetes-controller ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE NODE
nginx 1/1 Running 1 1h kubernetes-node-3
# display assigned IP address on the Pod
[root@kubernetes-controller ~]# kubectl get pods nginx -o yaml | grep "podIP"
podIP: 172.16.60.2
# access to the Pod
[root@kubernetes-controller ~]# curl http://172.16.60.2/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
kubernetes-dashboard 下载被墙,只能找个hosts映射下,如下:1
2
3[root@kubernetes-controller ~]# tail -2 /etc/hosts
220.255.2.153 www.gcr.io
220.255.2.153 gcr.io
时速云,国内镜像站,挺不错,不过这里我没有采用 - https://hub.tenxcloud.com/search?q=kubernetes-dashboard&source=tenxcloud
kubernetes-dashboard UI pods service
1 | [root@kubernetes-controller ~]# wget https://rawgit.com/kubernetes/dashboard/master/src/deploy/kubernetes-dashboard.yaml |
最后通过http://192.168.2.120:8080/ui 即可访问kubernetes-dashboard
验证kubernetes集群基本功能
快速启动,一键部署kubernetes 提供的guestbook,redis services example.
1 | $ git clone https://github.com/kubernetes/kubernetes.git |
列表所有的kubernetes services1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103[root@kubernetes-controller kubernetes]# kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend 10.254.210.70 <none> 80/TCP 1m
kubernetes 10.254.0.1 <none> 443/TCP 2h
redis-master 10.254.177.239 <none> 6379/TCP 1m
redis-slave 10.254.234.101 <none> 6379/TCP 1m
[root@kubernetes-controller ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-440143558-0iiy1 1/1 Running 0 8m
frontend-440143558-u5uja 1/1 Running 0 8m
frontend-440143558-xtlh3 1/1 Running 0 8m
nginx 1/1 Running 0 1h
redis-master-2353460263-fqdoc 1/1 Running 0 8m
redis-slave-1691881626-fs9m6 1/1 Running 0 8m
redis-slave-1691881626-xex67 1/1 Running 0 8m
[root@kubernetes-controller ~]# curl http://172.16.49.3/
<html ng-app="redis">
<head>
<title>Guestbook</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="controllers.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.js"></script>
</head>
<body ng-controller="RedisCtrl">
<div style="width: 50%; margin-left: 20px">
<h2>Guestbook</h2>
<form>
<fieldset>
<input ng-model="msg" placeholder="Messages" class="form-control" type="text" name="input"><br>
<button type="button" class="btn btn-primary" ng-click="controller.onRedis()">Submit</button>
</fieldset>
</form>
<div>
<div ng-repeat="msg in messages track by $index">
{{msg}}
</div>
</div>
</div>
</body>
</html>
[root@kubernetes-controller ~]# kubectl describe pods redis-master-2353460263-fqdoc
[root@kubernetes-controller ~]# kubectl logs redis-master-2353460263-fqdoc
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.19 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[1] 17 Aug 10:40:38.156 # Server started, Redis version 2.8.19
[1] 17 Aug 10:40:38.157 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
[1] 17 Aug 10:40:38.157 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[1] 17 Aug 10:40:38.157 * The server is now ready to accept connections on port 6379
[root@kubernetes-controller ~]# kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend 10.254.210.70 <none> 80/TCP 15h
kubernetes 10.254.0.1 <none> 443/TCP 18h
redis-master 10.254.177.239 <none> 6379/TCP 15h
redis-slave 10.254.234.101 <none> 6379/TCP 15h
[root@kubernetes-controller ~]# kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
frontend 3 3 3 3 15h
redis-master 1 1 1 1 15h
redis-slave 2 2 2 2 15h
[root@kubernetes-controller ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-440143558-0iiy1 1/1 Running 0 15h
frontend-440143558-u5uja 1/1 Running 0 15h
frontend-440143558-xtlh3 1/1 Running 0 15h
nginx 1/1 Running 0 17h
redis-master-2353460263-fqdoc 1/1 Running 0 15h
redis-slave-1691881626-fs9m6 1/1 Running 0 15h
redis-slave-1691881626-xex67 1/1 Running 0 15h
[root@kubernetes-controller ~]# kubectl get pods -L tier
NAME READY STATUS RESTARTS AGE TIER
frontend-440143558-0iiy1 1/1 Running 0 15h frontend
frontend-440143558-u5uja 1/1 Running 0 15h frontend
frontend-440143558-xtlh3 1/1 Running 0 15h frontend
nginx 1/1 Running 0 17h <none>
redis-master-2353460263-fqdoc 1/1 Running 0 15h backend
redis-slave-1691881626-fs9m6 1/1 Running 0 15h backend
redis-slave-1691881626-xex67 1/1 Running 0 15h backend
清理redis相关服务1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23[root@kubernetes-controller ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE NODE
apache-httpd 1/1 Running 0 41m kubernetes-node-3
frontend-440143558-0iiy1 1/1 Running 0 4d kubernetes-node-1
frontend-440143558-u5uja 1/1 Running 1 4d kubernetes-node-3
frontend-440143558-xtlh3 1/1 Running 1 4d kubernetes-node-2
nginx 1/1 Running 0 4d kubernetes-node-3
redis-master-2353460263-fqdoc 1/1 Running 0 4d kubernetes-node-2
redis-slave-1691881626-fs9m6 1/1 Running 1 4d kubernetes-node-3
redis-slave-1691881626-xex67 1/1 Running 0 4d kubernetes-node-1
[root@kubernetes-controller ~]# kubectl delete deployments,services -l "app in (redis, guestbook)"
deployment "frontend" deleted
deployment "redis-master" deleted
deployment "redis-slave" deleted
service "frontend" deleted
service "redis-master" deleted
service "redis-slave" deleted
[root@kubernetes-controller ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE NODE
apache-httpd 1/1 Running 0 42m kubernetes-node-3
nginx 1/1 Running 0 4d kubernetes-node-3