Kubernetes单Pod启动sonarqube
使用kubernetes可以很方便的对Docker进行管理,下面通过一个非常简单的例子来看一下Kubernetes怎样用大家所熟悉的方式通过创建ReplicationController和service对容器进行管理。
使用kubernetes可以很方便的对容器进行管理,下面通过一个非常简单的例子来看一下Kubernetes怎样用大家所熟悉的方式通过创建ReplicationController和service对容器进行管理。
在这个例子中,我们将在3个node一个master的集群构成上,运行质量分析软件sonarqube的2个实例.下面来看一下如何具体操作。
集群构成
[root@host131 ~]# kubectl get nodes
NAME STATUS AGE
host132 Ready 6h
host133 Ready 6h
host134 Ready 6h
[root@host131 ~]#
事前准备
在3个node上都事先pull下来sonarqube的镜像。不准备的话在kubernetes创建RC的时候也会去自动的pull。
[root@host131 ~]# for host in host132 host133 host134
> do
> echo "[$host]"
> ssh $host docker images |grep sonarqube
> done
[host132]
docker.io/sonarqube latest eea2f3093d50 Less than a second ago 790.9 MB
[host133]
docker.io/sonarqube latest eea2f3093d50 Less than a second ago 790.9 MB
[host134]
docker.io/sonarqube latest eea2f3093d50 Less than a second ago 790.9 MB
[root@host131 ~]#
定义sonar的Pod:sonar-rc.yaml
[root@host131 ~]# cat sonar-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: sonar
labels:
name: sonar
spec:
replicas: 2
selector:
name: sonar
template:
metadata:
labels:
name: sonar
spec:
containers:
- name: sonar
image: docker.io/sonarqube
ports:
- containerPort: 9000
[root@host131 ~]#
创建RC
[root@host131 ~]# kubectl create -f sonar-rc.yaml
replicationcontroller “sonar” created
[root@host131 ~]#
确认Pod
这里的status提示ContainerCreating的是指正在创建pod
[root@host131 ~]# kubectl get pods |grep sonar
sonar-3fh9s 0/1 ContainerCreating 0 19s
sonar-ohjcg 0/1 ContainerCreating 0 19s
[root@host131 ~]#
如何分配定义文件中的2个replica到3个node上的呢
[root@host131 ~]# kubectl get pods -o wide |grep sonar
sonar-3fh9s 0/1 ContainerCreating 0 1m host133
sonar-ohjcg 0/1 ContainerCreating 0 1m host134
[root@host131 ~]#
从这里可以清楚地看到host133和host134上被各分一个。我们可以通过describe命令来确认一下,describe命令对于我们在使用kubernetes的troubleshooting时候确认第一手的信息很有帮助,尤其是学习的时候。下面来看一下kubectl get pods中的其中一个pod的sonar-ohjcg的信息。
[root@host131 ~]# kubectl describe pod sonar-ohjcg
Name: sonar-ohjcg
Namespace: default
Node: host134/192.168.32.134
Start Time: Tue, 30 Aug 2016 03:35:30 -0400
Labels: name=sonar
Status: Running
IP: 172.16.64.5
Controllers: ReplicationController/sonar
Containers:
sonar:
Container ID: docker://551813e1c33ffb45247f2974aa89ea566b44e96ddac19232cf5c9463693c1e53
Image: docker.io/sonarqube
Image ID: docker://sha256:eea2f3093d50fde1a5cf90faea4e20b6cc966768885ee9b264ec2c616d803cd2
Port: 9000/TCP
QoS Tier:
cpu: BestEffort
memory: BestEffort
State: Running
Started: Tue, 30 Aug 2016 03:41:19 -0400
Ready: True
Restart Count: 0
Environment Variables:
Conditions:
Type Status
Ready True
Volumes:
default-token-8209b:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8209b
Events:
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
6m 6m 1 {default-scheduler } Normal Scheduled Successfully assigned sonar-ohjcg to host134
1m 1m 1 {kubelet host134} spec.containers{sonar} Normal Pulling pulling image "docker.io/sonarqube"
1m 1m 1 {kubelet host134} spec.containers{sonar} Normal Pulled Successfully pulled image "docker.io/sonarqube"
1m 1m 1 {kubelet host134} spec.containers{sonar} Normal Created Created container with docker id 551813e1c33f
1m 1m 1 {kubelet host134} spec.containers{sonar} Normal Started Started container with docker id 551813e1c33f
[root@host131 ~]#
解读describe pod的内容
sonar-ohjcg:定义文件中的sonar,在kubernetes将实例跑起来的时候为了避免重复会自动加上后面的名称,确切地说sonar只是前缀而已。
State:容器的状态处于Running的状态. 创建的pod的时候会处于waiting状态。
在Event的描述中可以清晰的看到kubernetes中pod的启动过程:
项番 | 详细信息 |
---|---|
No.1 | Successfully assigned sonar-ohjcg to host134 |
No.2 | pulling image “docker.io/sonarqube” |
No.3 | Successfully pulled image “docker.io/sonarqube” |
No.4 | Created container with docker id 551813e1c33f |
No.5 | Started container with docker id 551813e1c33f |
还是围绕着container进行管理,其所作的无非是,一分(分配)二拖(pull镜像)三运行(启动镜像)
接下来我们看一下另外一个pod的信息,基本上大同小异。
[root@host131 ~]# kubectl describe pod sonar-3fh9s
Name: sonar-3fh9s
Namespace: default
Node: host133/192.168.32.133
Start Time: Tue, 30 Aug 2016 03:35:35 -0400
Labels: name=sonar
Status: Running
IP: 172.16.78.6
Controllers: ReplicationController/sonar
Containers:
sonar:
Container ID: docker://57cb74fe81ea0104fdfd71735c1d0253e1c1d723c722a65a6c0e0ceea8205e4c
Image: docker.io/sonarqube
Image ID: docker://sha256:eea2f3093d50fde1a5cf90faea4e20b6cc966768885ee9b264ec2c616d803cd2
Port: 9000/TCP
QoS Tier:
cpu: BestEffort
memory: BestEffort
State: Running
Started: Tue, 30 Aug 2016 03:52:44 -0400
Ready: True
Restart Count: 0
Environment Variables:
Conditions:
Type Status
Ready True
Volumes:
default-token-8209b:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8209b
Events:
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
26m 26m 1 {default-scheduler } Normal Scheduled Successfully assigned sonar-3fh9s to host133
9m 9m 1 {kubelet host133} spec.containers{sonar} Normal Pulling pulling image "docker.io/sonarqube"
9m 9m 1 {kubelet host133} spec.containers{sonar} Normal Pulled Successfully pulled image "docker.io/sonarqube"
9m 9m 1 {kubelet host133} spec.containers{sonar} Normal Created Created container with docker id 57cb74fe81ea
9m 9m 1 {kubelet host133} spec.containers{sonar} Normal Started Started container with docker id 57cb74fe81ea
[root@host131 ~]#
确认pod运行状态
确认通过RC,pod都已经能正常运行。
[root@host131 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE NODE
sonar-3fh9s 1/1 Running 0 29m host133
sonar-ohjcg 1/1 Running 0 29m host134
[root@host131 ~]#
定义Service:sonar-service.yaml
apiVersion: v1
kind: Service
metadata:
name: sonar
labels:
name: sonar
spec:
type: NodePort
ports:
- port: 9000
nodePort: 31001
selector:
name: sonar
创建Service
[root@host131 ~]# kubectl create -f sonar-service.yaml
You have exposed your service on an external port on all nodes in your
cluster. If you want to expose this service to the external internet, you may
need to set up firewall rules for the service port(s) (tcp:31001) to serve traffic.
See http://releases.k8s.io/release-1.2/docs/user-guide/services-firewalls.md for more details.
service "sonar" created
[root@host131 ~]#
确认Service
[root@host131 ~]# kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 7h
sonar 10.254.141.6 nodes 9000/TCP 20s
[root@host131 ~]#
确认连接
利用host133的IP能够直接访问到该服务:确认OK
利用host134的IP能够直接访问到该服务:确认OK
更多推荐
所有评论(0)