第一章 k8s入门(简单的Java web项目)
环境: VMware Workstation在本机虚拟一个64位的Centos7虚拟机
环境准备
-
关闭防火墙
systemctl disable firewalld systemctl stop firewalld
-
安装etcd和Kubernetes软件(会自动安装Docker软件)
yum install -y etcd kubernetes
-
按顺序启动所有服务
systemctl start etcd systemctl start docker systemctl start kube-apiserver systemctl start kube-controller-manager systemctl start kube-scheduler systemctl start kubelet systemctl start kube-proxy
启动MySQL服务
-
MySQL镜像
docker pull daocloud.io/library/mysql:latest
-
构建Mysql RC定义文件(可以使用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: mysql ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "123456"
-
发布到kubernetes集群中,在master节点执行命令(注:此时可能出现问题,如:问题1:no resource found,问题2:status的状态是ContainerCreating而不是running,如果出现此问题请看关于创建rc和pod出现的问问题)
$ kubectl create -f mysql-rc.yaml replicationcontroller "mysql" created
查看创建的rc
$ kubectl get rc NAME DESIRED CURRENT READY AGE mysql 1 1 1 14s
查看pod创建情况
$ kubectl get pods NAME READY STATUS RESTARTS AGE mysql-srd1m 1/1 Running 0 41s
-
创建一个与之关联的k8s Service-Mysql的定义文件(文件名为mysql-svc.yaml),内容如下:
apiVersion: v1 kind: Service metadata: name: mysql spec: ports: - port: 3306 selector: app: mysql
运行kubectl命令,创建service
kubectl create -f mysql-svc.yaml
查看刚刚创建的service
$ kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 8d mysql 10.254.55.41 <none> 3306/TCP 13s
启动Tomcat服务
- 拉取Tomcat镜像
docker pull kubeguide/tomcat-app:v2
- 构建Tomcat RC定义文件,命名myweb-rc.yaml 内容如下:
完成rc创建和验证apiVersion: v1 kind: ReplicationController metadata: name: myweb spec: replicas: 2 selector: app: myweb template: metadata: labels: app: myweb spec: containers: - name: myweb image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080
$ kubectl create -f myweb-rc.yaml replicationcontroller "myweb" created $ kubectl get rc NAME DESIRED CURRENT READY AGE mysql 1 1 1 23h myweb 2 2 2 12s $ kubectl get pods NAME READY STATUS RESTARTS AGE mysql-srd1m 1/1 Running 1 23h myweb-fqlvz 1/1 Running 0 40s myweb-ggrlg 1/1 Running 0 40s
- 创建对应的Service,文件名(myweb-svc.yaml),内容如下
创建并验证apiVersion: v1 kind: Service metadata: name: myweb spec: type: NodePort ports: - port: 8080 nodePort: 30001 selector: app: myweb
$ kubectl create -f myweb-svc.yaml service "myweb" created $ kubectl get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 8d mysql 10.254.55.41 <none> 3306/TCP 42m myweb 10.254.237.187 <nodes> 8080:30001/TCP 21s
本文转移开源中国-K8s权威指南学习