kubernetes demo

img.png

准备工作

Docker拉取mysql镜像和tomcat镜像

1
sudo docker pull mysql:5.7
1
sudo docker pull kubeguide/tomcat-app:v1

为了解决, kubernetes pod卡在ContainerCreating的问题

1
2
sudo docker pull registry.cn-hangzhou.aliyuncs.com/sunyuki/pod-infrastructure
sudo docker tag f66f4bd9b894 registry.access.redhat.com/rhel7/pod-infrastructure:latest

创建 rc, svc yaml

1
vim mysql-rc.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
1
vim mysql-svc.yaml
1
2
3
4
5
6
7
8
9
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
1
vim myweb-rc.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: v1
kind: ReplicationController
metadata:
name: myweb
spec:
replicas: 1
selector:
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: kubeguide/tomcat-app:v1
ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_HOST
value: "mysql"
- name: MYSQL_SERVICE_PORT
value: "3306"
1
vim myweb-svc.yaml
1
2
3
4
5
6
7
8
9
10
11
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30001
selector:
app: myweb
1
2
3
4
sudo kubectl create -f mysql-rc.yaml
sudo kubectl create -f mysql-svc.yaml
sudo kubectl create -f myweb-rc.yaml
sudo kubectl create -f myweb-svc.yaml
1
2
3
4
sudo kubectl delete -f mysql-rc.yaml
sudo kubectl delete -f mysql-svc.yaml
sudo kubectl delete -f myweb-rc.yaml
sudo kubectl delete -f myweb-svc.yaml
1
2
3
4
sudo kubectl get rc
sudo kubectl get svc
sudo kubectl get pods
sudo kubectl get ep
1
2
3
4
5
6
7
 kubectl get ep

[root@localhost ~]# kubectl exec -ti myweb-qrjsd -- /bin/bash
root@myweb-qrjsd:/usr/local/tomcat# echo $MYSQL_SERVICE_HOST
mysql
root@myweb-qrjsd:/usr/local/tomcat# echo "172.17.0.2 mysql" >> /etc/hosts
root@myweb-qrjsd:/usr/local/tomcat#

使用127.0.0.1:30001/demo打开页面

参考文章

评论