该文章参考《kubernetes 权威指南》
环境:
[root@master tomcat-mysql]# kubectl get nodes
NAME STATUS AGE
node1 Ready 9d
node2 Ready 9d
需要启动两个容器myweb和mysql。
myweb配置文件:
ReplicationController配置文件
[root@master tomcat-mysql]# vim myweb-rc.yaml
kind: ReplicationController
metadata:
name: myweb
spec:
replicas: 1
selector:
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: docker.io/kubeguide/tomcat-app:v1
ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_PORT
value: '3306'
Service配置文件
[root@master tomcat-mysql]# vim myweb-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30001
selector:
app: myweb
启动:
[root@master tomcat-mysql]# kubectl create -f myweb-rc.yaml
[root@master tomcat-mysql]# kubectl create -f myweb-svc.yaml
[root@master tomcat-mysql]# kubectl get rc
NAME DESIRED CURRENT READY AGE
myweb 1 1 1 5h
[root@master tomcat-mysql]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 9d
myweb 10.254.255.33 <nodes> 8080:30001/TCP 5h
mysql配置文件:
[root@master tomcat-mysql]# vim mysql-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: docker.io/mysql:5.6
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"