mysql small example
- Create a mysql-rc.yaml file
apiVersion: v1 kind: ReplicationController # Replica controller RC metadata: name: mysql # Name of RC, globally unique spec: replicas: 1 # Expected number of copies selector: app: mysql # Pod that matches the target has this label template: # Create a copy (instance) of Pod according to this grinding bar metadata: labels: app: mysql # Label owned by the Pod copy, corresponding to the Selector of RC spec: containers: # Definition part of Pod content - name: mysql # Name of the container image: mysql # Docker Image corresponding to container ports: - containerPort: 3306 # Port number of container application listening env: # Environment variables injected into the container - name: MYSQL_ROOT_PASSWORD # This is the first time that I wrote MySQL root password incorrectly value: "root"
Issue RC file:
kubectl create -f mysql-rc.yaml
View created RC
kubectl get rc
View the creation of Pod
kubectl get pods
Failed to create, see more failure information
kubectl describe pod example
kubectl get events
View logs, error messages
kubectl logs -p <pod name>
error
➜ k8s kubectl logs -p mysql-lvq7v error: database is uninitialized and password option is not specified You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
This is the mysql password in the rc file. There's something wrong with it. Just change it
- Create a service with the file name mysql-svc.yaml
apiVersion: v1 kind: Service # Indicates Kubernetes Service metadata: name: mysql # Globally unique name of the Service spec: ports: - port: 3306 # The port number of the Service. It is written here as - port:3306 without spaces selector: # The Pod corresponding to the Service has the label defined here app: mysql
Establish
kubectl create -f mysql-svc.yaml
View the service you just created
kubectl get svc
An error was encountered due to indentation
error: error validating "mysql-svc.yaml": error validating data: ValidationError(Service.spec.ports[0]): invalid type for io.k8s.api.core.v1.ServicePort: got "string", expected "map"; if you choose to ignore these errors, turn validation off with --validate=false
According to the unique name of the Service, the container can obtain the Cluster IP address and port corresponding to the Service from the environment variable, and then it can initiate a TCP/IP connection request.
This creates a mysql service.